searchd.cpp 592 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191
  1. //
  2. // Copyright (c) 2017-2022, Manticore Software LTD (https://manticoresearch.com)
  3. // Copyright (c) 2001-2016, Andrew Aksyonoff
  4. // Copyright (c) 2008-2016, Sphinx Technologies Inc
  5. // 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 "sphinxutils.h"
  13. #include "fileutils.h"
  14. #include "sphinxexcerpt.h"
  15. #include "sphinxrt.h"
  16. #include "sphinxpq.h"
  17. #include "sphinxint.h"
  18. #include "sphinxquery.h"
  19. #include "sphinxsort.h"
  20. #include "sphinxjson.h"
  21. #include "sphinxjsonquery.h"
  22. #include "sphinxplugin.h"
  23. #include "sphinxqcache.h"
  24. #include "accumulator.h"
  25. #include "searchdaemon.h"
  26. #include "searchdha.h"
  27. #include "searchdreplication.h"
  28. #include "threadutils.h"
  29. #include "searchdtask.h"
  30. #include "global_idf.h"
  31. #include "docstore.h"
  32. #include "searchdssl.h"
  33. #include "searchdexpr.h"
  34. #include "indexsettings.h"
  35. #include "searchdddl.h"
  36. #include "networking_daemon.h"
  37. #include "query_status.h"
  38. #include "sphinxql_debug.h"
  39. #include "stackmock.h"
  40. #include "binlog.h"
  41. #include "indexfiles.h"
  42. #include "digest_sha1.h"
  43. #include "tokenizer/charset_definition_parser.h"
  44. #include "client_session.h"
  45. #include "sphinx_alter.h"
  46. #include "docs_collector.h"
  47. #include "index_rotator.h"
  48. #include "config_reloader.h"
  49. #include "secondarylib.h"
  50. #include "task_dispatcher.h"
  51. #include "tracer.h"
  52. // services
  53. #include "taskping.h"
  54. #include "taskmalloctrim.h"
  55. #include "taskoptimize.h"
  56. #include "taskglobalidf.h"
  57. #include "tasksavestate.h"
  58. #include "taskflushbinlog.h"
  59. #include "taskflushattrs.h"
  60. #include "taskflushmutable.h"
  61. #include "taskpreread.h"
  62. #include "coroutine.h"
  63. #include "dynamic_idx.h"
  64. #include "netreceive_ql.h"
  65. extern "C"
  66. {
  67. #include "sphinxudf.h"
  68. }
  69. #include <csignal>
  70. #include <clocale>
  71. #include <cmath>
  72. #include <ctime>
  73. #define SEARCHD_BACKLOG 5
  74. // don't shutdown on SIGKILL (debug purposes)
  75. // 1 - SIGKILL will shut down the whole daemon; 0 - watchdog will reincarnate the daemon
  76. #define WATCHDOG_SIGKILL 1
  77. /////////////////////////////////////////////////////////////////////////////
  78. #if _WIN32
  79. // Win-specific headers and calls
  80. #include <io.h>
  81. #else
  82. // UNIX-specific headers and calls
  83. #include <sys/wait.h>
  84. #include <netdb.h>
  85. #include <netinet/in.h>
  86. #include <netinet/tcp.h>
  87. #endif
  88. #if USE_SYSLOG
  89. #include <syslog.h>
  90. #endif
  91. #if HAVE_GETRLIMIT & HAVE_SETRLIMIT
  92. #include <sys/resource.h>
  93. #endif
  94. /////////////////////////////////////////////////////////////////////////////
  95. using namespace Threads;
  96. static bool g_bService = false;
  97. #if _WIN32
  98. static bool g_bServiceStop = false;
  99. static const char * g_sServiceName = "searchd";
  100. static HANDLE g_hPipe = INVALID_HANDLE_VALUE;
  101. #endif
  102. static StrVec_t g_dArgs;
  103. enum LogFormat_e
  104. {
  105. LOG_FORMAT_PLAIN,
  106. LOG_FORMAT_SPHINXQL
  107. };
  108. #define LOG_COMPACT_IN 128 // upto this many IN(..) values allowed in query_log
  109. static int g_iLogFile = STDOUT_FILENO; // log file descriptor
  110. static auto& g_iParentPID = getParentPID (); // set by watchdog
  111. static bool g_bLogSyslog = false;
  112. static bool g_bQuerySyslog = false;
  113. static CSphString g_sLogFile; // log file name
  114. static bool g_bLogTty = false; // cached isatty(g_iLogFile)
  115. static bool g_bLogStdout = true; // extra copy of startup log messages to stdout; true until around "accepting connections", then MUST be false
  116. static LogFormat_e g_eLogFormat = LOG_FORMAT_SPHINXQL;
  117. static bool g_bLogCompactIn = false; // whether to cut list in IN() clauses.
  118. static int g_iQueryLogMinMs = 0; // log 'slow' threshold for query
  119. static char g_sLogFilter[SPH_MAX_FILENAME_LEN+1] = "\0";
  120. static int g_iLogFilterLen = 0;
  121. static int g_iLogFileMode = 0;
  122. static CSphBitvec g_tLogStatements;
  123. int g_iReadTimeoutS = 5; // sec
  124. int g_iWriteTimeoutS = 5; // sec
  125. int g_iClientTimeoutS = 300;
  126. int g_iClientQlTimeoutS = 900; // sec
  127. static int g_iMaxConnection = 0; // unlimited
  128. static int g_iThreads; // defined in config, or =cpu cores
  129. static bool g_bWatchdog = true;
  130. static int g_iExpansionLimit = 0;
  131. static int g_iShutdownTimeoutUs = 3000000; // default timeout on daemon shutdown and stopwait is 3 seconds
  132. static int g_iBacklog = SEARCHD_BACKLOG;
  133. static int g_iThdQueueMax = 0;
  134. static bool g_bGroupingInUtc = false;
  135. static auto& g_iTFO = sphGetTFO ();
  136. static CSphString g_sShutdownToken;
  137. static int g_iServerID = 0;
  138. static bool g_bServerID = false;
  139. static bool g_bJsonConfigLoadedOk = false;
  140. static auto& g_iAutoOptimizeCutoffMultiplier = AutoOptimizeCutoffMultiplier();
  141. static constexpr bool AUTOOPTIMIZE_NEEDS_VIP = false; // whether non-VIP can issue 'SET GLOBAL auto_optimize = X'
  142. static constexpr bool THREAD_EX_NEEDS_VIP = false; // whether non-VIP can issue 'SET GLOBAL auto_optimize = X'
  143. static bool g_bSplit = true;
  144. static CSphVector<Listener_t> g_dListeners;
  145. static int g_iQueryLogFile = -1;
  146. static CSphString g_sQueryLogFile;
  147. static CSphString g_sPidFile;
  148. static bool g_bPidIsMine = false; // if PID is not mine, don't unlink it on fail
  149. static int g_iPidFD = -1;
  150. static int g_iMaxCachedDocs = 0; // in bytes
  151. static int g_iMaxCachedHits = 0; // in bytes
  152. int g_iMaxPacketSize = 8*1024*1024; // in bytes; for both query packets from clients and response packets from agents
  153. static int g_iMaxFilters = 256;
  154. static int g_iMaxFilterValues = 4096;
  155. static int g_iMaxBatchQueries = 32;
  156. static int64_t g_iDocstoreCache = 0;
  157. static int64_t g_iSkipCache = 0;
  158. static auto & g_iDistThreads = getDistThreads();
  159. int g_iAgentConnectTimeoutMs = 1000;
  160. int g_iAgentQueryTimeoutMs = 3000; // global (default). May be override by index-scope values, if one specified
  161. const int MAX_RETRY_COUNT = 8;
  162. const int MAX_RETRY_DELAY = 1000;
  163. int g_iAgentRetryCount = 0;
  164. int g_iAgentRetryDelayMs = MAX_RETRY_DELAY/2; // global (default) values. May be override by the query options 'retry_count' and 'retry_timeout'
  165. bool g_bHostnameLookup = false;
  166. CSphString g_sMySQLVersion = szMANTICORE_VERSION;
  167. CSphString g_sDbName = "Manticore";
  168. CSphString g_sBannerVersion { szMANTICORE_NAME };
  169. CSphString g_sBanner;
  170. CSphString g_sStatusVersion = szMANTICORE_VERSION;
  171. CSphString g_sSecondaryError;
  172. bool g_bSecondaryError { false };
  173. // for CLang thread-safety analysis
  174. ThreadRole MainThread; // functions which called only from main thread
  175. ThreadRole HandlerThread; // thread which serves clients
  176. //////////////////////////////////////////////////////////////////////////
  177. static CSphString g_sConfigFile;
  178. static bool g_bCleanLoadedConfig = true; // whether to clean config when it parsed and no more necessary
  179. static bool LOG_LEVEL_SHUTDOWN = val_from_env("MANTICORE_TRACK_DAEMON_SHUTDOWN",false); // verbose logging when daemon shutdown, ruled by this env variable
  180. static CSphString g_sConfigPath; // for resolve paths to absolute
  181. static auto& g_bSeamlessRotate = sphGetSeamlessRotate ();
  182. static bool g_bIOStats = false;
  183. static auto& g_bCpuStats = sphGetbCpuStat ();
  184. static bool g_bOptNoDetach = false;
  185. static bool g_bOptNoLock = false;
  186. static bool g_bSafeTrace = false;
  187. static bool g_bStripPath = false;
  188. static bool g_bCoreDump = false;
  189. static auto& g_bGotSighup = sphGetGotSighup(); // we just received SIGHUP; need to log
  190. static auto& g_bGotSigusr1 = sphGetGotSigusr1(); // we just received SIGUSR1; need to reopen logs
  191. static auto& g_bGotSigusr2 = sphGetGotSigusr2(); // we just received SIGUSR2; need to dump daemon's bt
  192. // pipe to watchdog to inform that daemon is going to close, so no need to restart it in case of crash
  193. struct SharedData_t
  194. {
  195. bool m_bDaemonAtShutdown;
  196. bool m_bHaveTTY;
  197. };
  198. static SharedData_t* g_pShared = nullptr;
  199. volatile bool g_bMaintenance = false;
  200. std::unique_ptr<ReadOnlyServedHash_c> g_pLocalIndexes = std::make_unique<ReadOnlyServedHash_c>(); // served (local) indexes hash
  201. std::unique_ptr<ReadOnlyDistrHash_c> g_pDistIndexes = std::make_unique<ReadOnlyDistrHash_c>(); // distributed indexes hash
  202. // this is internal deal of the daemon; don't expose it outside!
  203. // fixme! move all this stuff to dedicated file.
  204. static RwLock_t g_tRotateConfigMutex;
  205. static CSphConfig g_hCfg GUARDED_BY ( g_tRotateConfigMutex );
  206. static volatile bool g_bNeedRotate = false; // true if there were pending HUPs to handle (they could fly in during previous rotate)
  207. static volatile bool g_bInRotate = false; // true while we are rotating
  208. static volatile bool g_bReloadForced = false; // true in case reload issued via SphinxQL
  209. static WorkerSharedPtr_t g_pTickPoolThread;
  210. static CSphVector<CSphNetLoop*> g_dNetLoops;
  211. /// command names
  212. static const char * g_dApiCommands[] =
  213. {
  214. "search", "excerpt", "update", "keywords", "persist", "status", "query", "flushattrs", "query", "ping", "delete", "set", "insert", "replace", "commit", "suggest", "json",
  215. "callpq", "clusterpq", "getfield"
  216. };
  217. STATIC_ASSERT ( sizeof(g_dApiCommands)/sizeof(g_dApiCommands[0])==SEARCHD_COMMAND_TOTAL, SEARCHD_COMMAND_SHOULD_BE_SAME_AS_SEARCHD_COMMAND_TOTAL );
  218. //////////////////////////////////////////////////////////////////////////
  219. const char * sAgentStatsNames[eMaxAgentStat+ehMaxStat]=
  220. { "query_timeouts", "connect_timeouts", "connect_failures",
  221. "network_errors", "wrong_replies", "unexpected_closings",
  222. "warnings", "succeeded_queries", "total_query_time",
  223. "connect_count", "connect_avg", "connect_max" };
  224. static RwLock_t g_tLastMetaLock;
  225. static CSphQueryResultMeta g_tLastMeta GUARDED_BY ( g_tLastMetaLock );
  226. /////////////////////////////////////////////////////////////////////////////
  227. // MISC
  228. /////////////////////////////////////////////////////////////////////////////
  229. static void ReleaseTTYFlag()
  230. {
  231. if ( g_pShared )
  232. g_pShared->m_bHaveTTY = true;
  233. }
  234. /////////////////////////////////////////////////////////////////////////////
  235. // LOGGING
  236. /////////////////////////////////////////////////////////////////////////////
  237. /// physically emit log entry
  238. /// buffer must have 1 extra byte for linefeed
  239. #if _WIN32
  240. static void sphLogEntry ( ESphLogLevel eLevel, char * sBuf, char * sTtyBuf )
  241. #else
  242. static void sphLogEntry ( ESphLogLevel , char * sBuf, char * sTtyBuf )
  243. #endif
  244. {
  245. #if _WIN32
  246. if ( g_bService && g_iLogFile==STDOUT_FILENO )
  247. {
  248. HANDLE hEventSource;
  249. LPCTSTR lpszStrings[2];
  250. hEventSource = RegisterEventSource ( NULL, g_sServiceName );
  251. if ( hEventSource )
  252. {
  253. lpszStrings[0] = g_sServiceName;
  254. lpszStrings[1] = sBuf;
  255. WORD eType;
  256. switch ( eLevel )
  257. {
  258. case SPH_LOG_FATAL: eType = EVENTLOG_ERROR_TYPE; break;
  259. case SPH_LOG_WARNING: eType = EVENTLOG_WARNING_TYPE; break;
  260. case SPH_LOG_INFO: eType = EVENTLOG_INFORMATION_TYPE; break;
  261. default: eType = EVENTLOG_INFORMATION_TYPE; break;
  262. }
  263. ReportEvent ( hEventSource, // event log handle
  264. eType, // event type
  265. 0, // event category
  266. 0, // event identifier
  267. NULL, // no security identifier
  268. 2, // size of lpszStrings array
  269. 0, // no binary data
  270. lpszStrings, // array of strings
  271. NULL ); // no binary data
  272. DeregisterEventSource ( hEventSource );
  273. }
  274. } else
  275. #endif
  276. {
  277. strcat ( sBuf, "\n" ); // NOLINT
  278. sphSeek ( g_iLogFile, 0, SEEK_END );
  279. if ( g_bLogTty )
  280. {
  281. memmove ( sBuf+20, sBuf+15, 9);
  282. sTtyBuf = sBuf + 19;
  283. *sTtyBuf = '[';
  284. sphWrite ( g_iLogFile, sTtyBuf, strlen(sTtyBuf) );
  285. }
  286. else
  287. sphWrite ( g_iLogFile, sBuf, strlen(sBuf) );
  288. if ( g_bLogStdout && g_iLogFile!=STDOUT_FILENO )
  289. sphWrite ( STDOUT_FILENO, sTtyBuf, strlen(sTtyBuf) );
  290. }
  291. }
  292. /// log entry (with log levels, dupe catching, etc)
  293. /// call with NULL format for dupe flushing
  294. void sphLog ( ESphLogLevel eLevel, const char * sFmt, va_list ap )
  295. {
  296. // dupe catcher state
  297. static const int FLUSH_THRESH_TIME = 1000000; // in microseconds
  298. static const int FLUSH_THRESH_COUNT = 100;
  299. static ESphLogLevel eLastLevel = SPH_LOG_INFO;
  300. static DWORD uLastEntry = 0;
  301. static int64_t tmLastStamp = -1000000-FLUSH_THRESH_TIME;
  302. static int iLastRepeats = 0;
  303. // only if we can
  304. if ( sFmt && eLevel>g_eLogLevel )
  305. return;
  306. #if USE_SYSLOG
  307. if ( g_bLogSyslog && sFmt )
  308. {
  309. const int levels[SPH_LOG_MAX+1] = { LOG_EMERG, LOG_WARNING, LOG_INFO, LOG_DEBUG, LOG_DEBUG, LOG_DEBUG, LOG_DEBUG };
  310. vsyslog ( levels[eLevel], sFmt, ap );
  311. return;
  312. }
  313. #endif
  314. if ( g_iLogFile<0 && !g_bService )
  315. return;
  316. // format the banner
  317. char sTimeBuf[128];
  318. sphFormatCurrentTime ( sTimeBuf, sizeof(sTimeBuf) );
  319. const char * sBanner = "";
  320. if ( sFmt==NULL ) eLevel = eLastLevel;
  321. if ( eLevel==SPH_LOG_FATAL ) sBanner = "FATAL: ";
  322. if ( eLevel==SPH_LOG_WARNING ) sBanner = "WARNING: ";
  323. if ( eLevel>=SPH_LOG_DEBUG ) sBanner = "DEBUG: ";
  324. if ( eLevel==SPH_LOG_RPL_DEBUG ) sBanner = "RPL: ";
  325. char sBuf [ 1024 ];
  326. snprintf ( sBuf, sizeof(sBuf)-1, "[%s] [%d] ", sTimeBuf, GetOsThreadId() );
  327. char * sTtyBuf = sBuf + strlen(sBuf);
  328. strncpy ( sTtyBuf, sBanner, 32 ); // 32 is arbitrary; just something that is enough and keeps lint happy
  329. auto iLen = (int) strlen(sBuf);
  330. // format the message
  331. if ( sFmt )
  332. {
  333. // need more space for tail zero and "\n" that added at sphLogEntry
  334. int iSafeGap = 4;
  335. int iBufSize = sizeof(sBuf)-iLen-iSafeGap;
  336. vsnprintf ( sBuf+iLen, iBufSize, sFmt, ap );
  337. sBuf[ sizeof(sBuf)-iSafeGap ] = '\0';
  338. }
  339. if ( sFmt && eLevel>SPH_LOG_INFO && g_iLogFilterLen )
  340. {
  341. if ( strncmp ( sBuf+iLen, g_sLogFilter, g_iLogFilterLen )!=0 )
  342. return;
  343. }
  344. // catch dupes
  345. DWORD uEntry = sFmt ? sphCRC32 ( sBuf+iLen ) : 0;
  346. int64_t tmNow = sphMicroTimer();
  347. // accumulate while possible
  348. if ( sFmt && eLevel==eLastLevel && uEntry==uLastEntry && iLastRepeats<FLUSH_THRESH_COUNT && tmNow<tmLastStamp+FLUSH_THRESH_TIME )
  349. {
  350. tmLastStamp = tmNow;
  351. iLastRepeats++;
  352. return;
  353. }
  354. // flush if needed
  355. if ( iLastRepeats!=0 && ( sFmt || tmNow>=tmLastStamp+FLUSH_THRESH_TIME ) )
  356. {
  357. // flush if we actually have something to flush, and
  358. // case 1: got a message we can't accumulate
  359. // case 2: got a periodic flush and been otherwise idle for a thresh period
  360. char sLast[256];
  361. iLen = Min ( iLen, 256 );
  362. strncpy ( sLast, sBuf, iLen );
  363. if ( iLen < 256 )
  364. snprintf ( sLast+iLen, sizeof(sLast)-iLen, "last message repeated %d times", iLastRepeats );
  365. sphLogEntry ( eLastLevel, sLast, sLast + ( sTtyBuf-sBuf ) );
  366. tmLastStamp = tmNow;
  367. iLastRepeats = 0;
  368. eLastLevel = SPH_LOG_INFO;
  369. uLastEntry = 0;
  370. }
  371. // was that a flush-only call?
  372. if ( !sFmt )
  373. return;
  374. tmLastStamp = tmNow;
  375. iLastRepeats = 0;
  376. eLastLevel = eLevel;
  377. uLastEntry = uEntry;
  378. // do the logging
  379. sphLogEntry ( eLevel, sBuf, sTtyBuf );
  380. }
  381. void Shutdown (); // forward
  382. bool DieOrFatalWithShutdownCb ( bool bDie, const char * sFmt, va_list ap )
  383. {
  384. if ( bDie )
  385. g_pLogger () ( SPH_LOG_FATAL, sFmt, ap );
  386. else
  387. Shutdown ();
  388. return false; // don't lot to stdout
  389. }
  390. bool DieOrFatalCb ( bool bDie, const char * sFmt, va_list ap )
  391. {
  392. if ( bDie )
  393. g_pLogger () ( SPH_LOG_FATAL, sFmt, ap );
  394. return false; // don't lot to stdout
  395. }
  396. #if !_WIN32
  397. static CSphString GetNamedPipeName ( int iPid )
  398. {
  399. CSphString sRes;
  400. sRes.SetSprintf ( "/tmp/searchd_%d", iPid );
  401. return sRes;
  402. }
  403. #endif
  404. void LogChangeMode ( int iFile, int iMode )
  405. {
  406. if ( iFile<0 || iMode==0 || iFile==STDOUT_FILENO || iFile==STDERR_FILENO )
  407. return;
  408. #if !_WIN32
  409. fchmod ( iFile, iMode );
  410. #endif
  411. }
  412. /////////////////////////////////////////////////////////////////////////////
  413. static int CmpString ( const CSphString & a, const CSphString & b )
  414. {
  415. if ( !a.cstr() && !b.cstr() )
  416. return 0;
  417. if ( !a.cstr() || !b.cstr() )
  418. return a.cstr() ? -1 : 1;
  419. return strcmp ( a.cstr(), b.cstr() );
  420. }
  421. struct SearchFailure_t
  422. {
  423. CSphString m_sParentIndex;
  424. CSphString m_sIndex; ///< searched index name
  425. CSphString m_sError; ///< search error message
  426. bool operator == ( const SearchFailure_t & r ) const
  427. {
  428. return m_sIndex==r.m_sIndex && m_sError==r.m_sError && m_sParentIndex==r.m_sParentIndex;
  429. }
  430. bool operator < ( const SearchFailure_t & r ) const
  431. {
  432. int iRes = CmpString ( m_sError.cstr(), r.m_sError.cstr() );
  433. if ( !iRes )
  434. iRes = CmpString ( m_sParentIndex.cstr (), r.m_sParentIndex.cstr () );
  435. if ( !iRes )
  436. iRes = CmpString ( m_sIndex.cstr(), r.m_sIndex.cstr() );
  437. return iRes<0;
  438. }
  439. SearchFailure_t & operator = ( const SearchFailure_t & r )
  440. {
  441. if ( this!=&r )
  442. {
  443. m_sParentIndex = r.m_sParentIndex;
  444. m_sIndex = r.m_sIndex;
  445. m_sError = r.m_sError;
  446. }
  447. return *this;
  448. }
  449. };
  450. static void ReportIndexesName ( int iSpanStart, int iSpandEnd, const CSphVector<SearchFailure_t> & dLog, StringBuilder_c & sOut );
  451. class SearchFailuresLog_c
  452. {
  453. CSphVector<SearchFailure_t> m_dLog;
  454. public:
  455. void Submit ( const CSphString& sIndex, const char * sParentIndex , const char * sError )
  456. {
  457. SearchFailure_t & tEntry = m_dLog.Add ();
  458. tEntry.m_sParentIndex = sParentIndex;
  459. tEntry.m_sIndex = sIndex;
  460. tEntry.m_sError = sError;
  461. }
  462. void SubmitVa ( const char * sIndex, const char * sParentIndex, const char * sTemplate, va_list ap )
  463. {
  464. StringBuilder_c tError;
  465. tError.vAppendf ( sTemplate, ap );
  466. SearchFailure_t &tEntry = m_dLog.Add ();
  467. tEntry.m_sParentIndex = sParentIndex;
  468. tEntry.m_sIndex = sIndex;
  469. tError.MoveTo ( tEntry.m_sError );
  470. }
  471. inline void Append ( const SearchFailuresLog_c& rhs )
  472. {
  473. m_dLog.Append ( rhs.m_dLog );
  474. }
  475. void SubmitEx ( const char * sIndex, const char * sParentIndex, const char * sTemplate, ... ) __attribute__ ( ( format ( printf, 4, 5 ) ) )
  476. {
  477. va_list ap;
  478. va_start ( ap, sTemplate );
  479. SubmitVa ( sIndex, sParentIndex, sTemplate, ap);
  480. va_end ( ap );
  481. }
  482. void SubmitEx ( const CSphString &sIndex, const char * sParentIndex, const char * sTemplate, ... ) __attribute__ ( ( format ( printf, 4, 5 ) ) )
  483. {
  484. va_list ap;
  485. va_start ( ap, sTemplate );
  486. SubmitVa ( sIndex.cstr(), sParentIndex, sTemplate, ap );
  487. va_end ( ap );
  488. }
  489. bool IsEmpty ()
  490. {
  491. return m_dLog.GetLength()==0;
  492. }
  493. int GetReportsCount()
  494. {
  495. return m_dLog.GetLength();
  496. }
  497. void BuildReport ( StringBuilder_c & sReport )
  498. {
  499. if ( IsEmpty() )
  500. return;
  501. // collapse same messages
  502. m_dLog.Uniq ();
  503. int iSpanStart = 0;
  504. Comma_c sColon( { ";\n", 2 } );
  505. for ( int i=1; i<=m_dLog.GetLength(); ++i )
  506. {
  507. // keep scanning while error text is the same
  508. if ( i!=m_dLog.GetLength() )
  509. if ( m_dLog[i].m_sError==m_dLog[i-1].m_sError )
  510. continue;
  511. sReport << sColon;
  512. ReportIndexesName ( iSpanStart, i, m_dLog, sReport );
  513. sReport << m_dLog[iSpanStart].m_sError;
  514. // done
  515. iSpanStart = i;
  516. }
  517. }
  518. };
  519. #define LOG_COMPONENT_SEARCHD __LINE__ << " "
  520. #define SHUTINFO LOGINFO (SHUTDOWN,SEARCHD)
  521. /////////////////////////////////////////////////////////////////////////////
  522. // SIGNAL HANDLERS
  523. /////////////////////////////////////////////////////////////////////////////
  524. void Shutdown () REQUIRES ( MainThread ) NO_THREAD_SAFETY_ANALYSIS
  525. {
  526. // force even long time searches to shut
  527. sphInterruptNow ();
  528. #if !_WIN32
  529. int fdStopwait = -1;
  530. #endif
  531. bool bAttrsSaveOk = true;
  532. if ( g_pShared )
  533. g_pShared->m_bDaemonAtShutdown = true;
  534. #if !_WIN32
  535. // stopwait handshake
  536. CSphString sPipeName = GetNamedPipeName ( getpid() );
  537. fdStopwait = ::open ( sPipeName.cstr(), O_WRONLY | O_NONBLOCK );
  538. if ( fdStopwait>=0 )
  539. {
  540. DWORD uHandshakeOk = 0;
  541. int VARIABLE_IS_NOT_USED iDummy = ::write ( fdStopwait, &uHandshakeOk, sizeof(DWORD) );
  542. }
  543. #endif
  544. int64_t tmShutStarted = sphMicroTimer ();
  545. // release all planned/scheduled tasks
  546. SHUTINFO << "Shut down mini timer ...";
  547. sph::ShutdownMiniTimer();
  548. SHUTINFO << "Shut down flushing mutable ...";
  549. ShutdownFlushingMutable();
  550. // stop search threads; up to shutdown_timeout seconds
  551. SHUTINFO << "Wait preread (if any) finished ...";
  552. WaitPrereadFinished ( g_iShutdownTimeoutUs );
  553. // save attribute updates for all local indexes
  554. SHUTINFO << "Finally save indexes ...";
  555. bAttrsSaveOk = FinallySaveIndexes();
  556. // right before unlock loop
  557. if ( g_bJsonConfigLoadedOk )
  558. {
  559. CSphString sError;
  560. SHUTINFO << "Save json config ...";
  561. SaveConfigInt(sError);
  562. }
  563. // stop netloop processing
  564. SHUTINFO << "Stop netloop processing ...";
  565. for ( auto & pNetLoop : g_dNetLoops )
  566. {
  567. pNetLoop->StopNetLoop ();
  568. SafeRelease ( pNetLoop );
  569. }
  570. // stop netloop threads
  571. SHUTINFO << "Stop netloop pool ...";
  572. if ( g_pTickPoolThread )
  573. g_pTickPoolThread->StopAll ();
  574. // call scheduled callbacks:
  575. // shutdown replication,
  576. // shutdown ssl,
  577. // shutdown tick threads,
  578. SHUTINFO << "Invoke shutdown callbacks ...";
  579. searchd::FireShutdownCbs ();
  580. SHUTINFO << "Waiting clients to finish ... (" << myinfo::CountClients() << ")";
  581. while ( ( myinfo::CountClients ()>0 ) && ( sphMicroTimer ()-tmShutStarted )<g_iShutdownTimeoutUs )
  582. sphSleepMsec ( 50 );
  583. if ( myinfo::CountClients ()>0 )
  584. {
  585. int64_t tmDelta = sphMicroTimer ()-tmShutStarted;
  586. sphWarning ( "still %d alive tasks during shutdown, after %d.%03d sec", myinfo::CountClients (), (int) ( tmDelta
  587. / 1000000 ), (int) ( ( tmDelta / 1000 ) % 1000 ) );
  588. }
  589. // unlock indexes and release locks if needed
  590. SHUTINFO << "Unlock indexes ...";
  591. {
  592. ServedSnap_t hLocal = g_pLocalIndexes->GetHash();
  593. for ( const auto& tIt : *hLocal )
  594. RWIdx_c ( tIt.second )->Unlock();
  595. }
  596. Threads::CallCoroutine ( [] {
  597. SHUTINFO << "Abandon local indexes list ...";
  598. g_pLocalIndexes->ReleaseAndClear();
  599. // unlock Distr indexes automatically done by d-tr
  600. SHUTINFO << "Abandon distr indexes list ...";
  601. g_pDistIndexes->ReleaseAndClear();
  602. } );
  603. SHUTINFO << "Shutdown alone threads (if any) ...";
  604. Detached::ShutdownAllAlones();
  605. SHUTINFO << "Shutdown main work pool ...";
  606. auto pPool = GlobalWorkPool();
  607. if ( pPool )
  608. pPool->StopAll();
  609. SHUTINFO << "Remove local indexes list ...";
  610. g_pLocalIndexes.reset();
  611. SHUTINFO << "Remove distr indexes list ...";
  612. g_pDistIndexes.reset();
  613. // clear shut down of rt indexes + binlog
  614. SHUTINFO << "Finish IO stats collecting ...";
  615. sphDoneIOStats();
  616. SHUTINFO << "Finish RT serving ...";
  617. Binlog::Deinit();
  618. SHUTINFO << "Shutdown docstore ...";
  619. ShutdownDocstore();
  620. SHUTINFO << "Shutdown skip cache ...";
  621. ShutdownSkipCache();
  622. SHUTINFO << "Shutdown global IDFs ...";
  623. sph::ShutdownGlobalIDFs ();
  624. SHUTINFO << "Shutdown aot ...";
  625. sphAotShutdown ();
  626. SHUTINFO << "Shutdown columnar ...";
  627. ShutdownColumnar();
  628. SHUTINFO << "Shutdown listeners ...";
  629. for ( auto& dListener : g_dListeners )
  630. if ( dListener.m_iSock>=0 )
  631. sphSockClose ( dListener.m_iSock );
  632. SHUTINFO << "Close persistent sockets ...";
  633. ClosePersistentSockets();
  634. // close pid
  635. SHUTINFO << "Release (close) pid file ...";
  636. if ( g_iPidFD!=-1 )
  637. ::close ( g_iPidFD );
  638. g_iPidFD = -1;
  639. // remove pid file, if we owned it
  640. if ( g_bPidIsMine && !g_sPidFile.IsEmpty() )
  641. ::unlink ( g_sPidFile.cstr() );
  642. SHUTINFO << "Shutdown hazard pointers ...";
  643. hazard::Shutdown ();
  644. // wordforms till there might be referenced from accum (rt-index), which, in turn, is part of client session.
  645. // so, shutdown them before will probably fail.
  646. // after hazard shutdown, all sessions are surely done, so wordforms is good to be destroyed at this point.
  647. SHUTINFO << "Shutdown wordforms ...";
  648. sphShutdownWordforms();
  649. sphInfo ( "shutdown daemon version '%s' ...", g_sStatusVersion.cstr() );
  650. sphInfo ( "shutdown complete" );
  651. Threads::Done ( g_iLogFile );
  652. #if _WIN32
  653. CloseHandle ( g_hPipe );
  654. #else
  655. if ( fdStopwait>=0 )
  656. {
  657. DWORD uStatus = bAttrsSaveOk;
  658. int VARIABLE_IS_NOT_USED iDummy = ::write ( fdStopwait, &uStatus, sizeof(DWORD) );
  659. ::close ( fdStopwait );
  660. }
  661. #endif
  662. }
  663. void sighup ( int )
  664. {
  665. g_bGotSighup = 1;
  666. }
  667. static void sigterm ( int )
  668. {
  669. // tricky bit
  670. // we can't call exit() here because malloc()/free() are not re-entrant
  671. // we could call _exit() but let's try to die gracefully on TERM
  672. // and let signal sender wait and send KILL as needed
  673. sphInterruptNow();
  674. }
  675. static void sigusr1 ( int )
  676. {
  677. g_bGotSigusr1 = true;
  678. }
  679. static void sigusr2 ( int )
  680. {
  681. g_bGotSigusr2 = true;
  682. }
  683. struct QueryCopyState_t
  684. {
  685. BYTE * m_pDst;
  686. BYTE * m_pDstEnd;
  687. const BYTE * m_pSrc;
  688. const BYTE * m_pSrcEnd;
  689. };
  690. // crash query handler
  691. static const int g_iQueryLineLen = 80;
  692. static const char g_dEncodeBase64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  693. static bool sphCopyEncodedBase64 ( QueryCopyState_t & tEnc )
  694. {
  695. BYTE * pDst = tEnc.m_pDst;
  696. const BYTE * pDstBase = tEnc.m_pDst;
  697. const BYTE * pSrc = tEnc.m_pSrc;
  698. const BYTE * pDstEnd = tEnc.m_pDstEnd-5;
  699. const BYTE * pSrcEnd = tEnc.m_pSrcEnd-3;
  700. while ( pDst<=pDstEnd && pSrc<=pSrcEnd )
  701. {
  702. // put line delimiter at max line length
  703. if ( ( ( pDst-pDstBase ) % g_iQueryLineLen )>( ( pDst-pDstBase+4 ) % g_iQueryLineLen ) )
  704. *pDst++ = '\n';
  705. // Convert to big endian
  706. DWORD uSrc = ( pSrc[0] << 16 ) | ( pSrc[1] << 8 ) | ( pSrc[2] );
  707. pSrc += 3;
  708. *pDst++ = g_dEncodeBase64 [ ( uSrc & 0x00FC0000 ) >> 18 ];
  709. *pDst++ = g_dEncodeBase64 [ ( uSrc & 0x0003F000 ) >> 12 ];
  710. *pDst++ = g_dEncodeBase64 [ ( uSrc & 0x00000FC0 ) >> 6 ];
  711. *pDst++ = g_dEncodeBase64 [ ( uSrc & 0x0000003F ) ];
  712. }
  713. // there is a tail in source data and a room for it at destination buffer
  714. if ( pSrc<tEnc.m_pSrcEnd && ( tEnc.m_pSrcEnd-pSrc<3 ) && ( pDst<=pDstEnd-4 ) )
  715. {
  716. int iLeft = ( tEnc.m_pSrcEnd - pSrc ) % 3;
  717. if ( iLeft==1 )
  718. {
  719. DWORD uSrc = pSrc[0]<<16;
  720. pSrc += 1;
  721. *pDst++ = g_dEncodeBase64 [ ( uSrc & 0x00FC0000 ) >> 18 ];
  722. *pDst++ = g_dEncodeBase64 [ ( uSrc & 0x0003F000 ) >> 12 ];
  723. *pDst++ = '=';
  724. *pDst++ = '=';
  725. } else if ( iLeft==2 )
  726. {
  727. DWORD uSrc = ( pSrc[0]<<16 ) | ( pSrc[1] << 8 );
  728. pSrc += 2;
  729. *pDst++ = g_dEncodeBase64 [ ( uSrc & 0x00FC0000 ) >> 18 ];
  730. *pDst++ = g_dEncodeBase64 [ ( uSrc & 0x0003F000 ) >> 12 ];
  731. *pDst++ = g_dEncodeBase64 [ ( uSrc & 0x00000FC0 ) >> 6 ];
  732. *pDst++ = '=';
  733. }
  734. }
  735. tEnc.m_pDst = pDst;
  736. tEnc.m_pSrc = pSrc;
  737. return ( tEnc.m_pSrc<tEnc.m_pSrcEnd );
  738. }
  739. static bool sphCopySphinxQL ( QueryCopyState_t & tState )
  740. {
  741. BYTE * pDst = tState.m_pDst;
  742. const BYTE * pSrc = tState.m_pSrc;
  743. BYTE * pNextLine = pDst+g_iQueryLineLen;
  744. while ( pDst<tState.m_pDstEnd && pSrc<tState.m_pSrcEnd )
  745. {
  746. if ( pDst>pNextLine && pDst+1<tState.m_pDstEnd && ( sphIsSpace ( *pSrc ) || *pSrc==',' ) )
  747. {
  748. *pDst++ = *pSrc++;
  749. *pDst++ = '\n';
  750. pNextLine = pDst + g_iQueryLineLen;
  751. } else
  752. {
  753. *pDst++ = *pSrc++;
  754. }
  755. }
  756. tState.m_pDst = pDst;
  757. tState.m_pSrc = pSrc;
  758. return ( tState.m_pSrc<tState.m_pSrcEnd );
  759. }
  760. static bool sphCopySphinxHttp ( QueryCopyState_t & tState )
  761. {
  762. BYTE * pDst = tState.m_pDst;
  763. const BYTE * pSrc = tState.m_pSrc;
  764. while ( pDst<tState.m_pDstEnd && pSrc<tState.m_pSrcEnd )
  765. {
  766. *pDst++ = *pSrc++;
  767. }
  768. tState.m_pDst = pDst;
  769. tState.m_pSrc = pSrc;
  770. return ( tState.m_pSrc<tState.m_pSrcEnd );
  771. }
  772. typedef bool CopyQuery_fn ( QueryCopyState_t & tState );
  773. #define SPH_TIME_PID_MAX_SIZE 256
  774. const char g_sCrashedBannerAPI[] = "\n--- crashed SphinxAPI request dump ---\n";
  775. const char g_sCrashedBannerMySQL[] = "\n--- crashed SphinxQL request dump ---\n";
  776. const char g_sCrashedBannerHTTP[] = "\n--- crashed HTTP request dump ---\n";
  777. const char g_sCrashedBannerBad[] = "\n--- crashed invalid query ---\n";
  778. const char g_sCrashedBannerTail[] = "\n--- request dump end ---\n";
  779. const char g_sCrashedIndex[] = "--- local index:";
  780. const char g_sEndLine[] = "\n";
  781. #if _WIN32
  782. const char g_sMinidumpBanner[] = "minidump located at: ";
  783. #endif
  784. #if SPH_ALLOCS_PROFILER
  785. const char g_sMemoryStatBanner[] = "\n--- memory statistics ---\n";
  786. #endif
  787. static BYTE g_dCrashQueryBuff [4096];
  788. static char g_sCrashInfo [SPH_TIME_PID_MAX_SIZE] = "[][]\n";
  789. static int g_iCrashInfoLen = 0;
  790. #if _WIN32
  791. static char g_sMinidump[SPH_TIME_PID_MAX_SIZE] = "";
  792. #endif
  793. #if !_WIN32
  794. void CrashLogger::HandleCrash ( int sig ) NO_THREAD_SAFETY_ANALYSIS
  795. #else
  796. LONG WINAPI CrashLogger::HandleCrash ( EXCEPTION_POINTERS * pExc )
  797. #endif // !_WIN32
  798. {
  799. sphSetDied();
  800. if ( g_iLogFile<0 )
  801. {
  802. if ( g_bCoreDump )
  803. {
  804. CRASH_EXIT_CORE;
  805. } else
  806. {
  807. CRASH_EXIT;
  808. }
  809. }
  810. // log [time][pid]
  811. sphSeek ( g_iLogFile, 0, SEEK_END );
  812. sphWrite ( g_iLogFile, g_sCrashInfo, g_iCrashInfoLen );
  813. // log query
  814. auto& tQuery = GlobalCrashQueryGetRef ();
  815. bool bValidQuery = IsFilled ( tQuery.m_dQuery );
  816. #if !_WIN32
  817. if ( bValidQuery )
  818. {
  819. size_t iPageSize = getpagesize();
  820. // FIXME! That is too complex way, remove all of this and just move query dump to the bottom
  821. // remove also mincore_test.cmake, it's invokation from CMakeLists.txt and HAVE_UNSIGNED_MINCORE
  822. // declatarion from config_cmake.h.in
  823. #if HAVE_UNSIGNED_MINCORE
  824. BYTE dPages = 0;
  825. #else
  826. char dPages = 0;
  827. #endif
  828. auto pPageStart = (uintptr_t) tQuery.m_dQuery.first;
  829. pPageStart &= ~( iPageSize - 1 );
  830. bValidQuery &= ( mincore ( ( void * ) pPageStart, 1, &dPages )==0 );
  831. auto pPageEnd = (uintptr_t) ( tQuery.m_dQuery.first+tQuery.m_dQuery.second-1 );
  832. pPageEnd &= ~( iPageSize - 1 );
  833. bValidQuery &= ( mincore ( ( void * ) pPageEnd, 1, &dPages )==0 );
  834. }
  835. #endif
  836. // request dump banner
  837. Str_t dBanner = { g_sCrashedBannerAPI, sizeof ( g_sCrashedBannerAPI )-1 };
  838. if ( tQuery.m_eType==QUERY_SQL )
  839. dBanner = { g_sCrashedBannerMySQL, sizeof ( g_sCrashedBannerMySQL )-1 };
  840. else if ( tQuery.m_eType==QUERY_JSON )
  841. dBanner = { g_sCrashedBannerHTTP, sizeof ( g_sCrashedBannerHTTP )-1 };
  842. if ( !bValidQuery )
  843. dBanner = { g_sCrashedBannerBad, sizeof ( g_sCrashedBannerBad )-1 };
  844. sphWrite ( g_iLogFile, dBanner );
  845. // query
  846. if ( bValidQuery )
  847. {
  848. QueryCopyState_t tCopyState;
  849. tCopyState.m_pDst = g_dCrashQueryBuff;
  850. tCopyState.m_pDstEnd = g_dCrashQueryBuff + sizeof(g_dCrashQueryBuff);
  851. tCopyState.m_pSrc = tQuery.m_dQuery.first;
  852. tCopyState.m_pSrcEnd = tQuery.m_dQuery.first + tQuery.m_dQuery.second;
  853. CopyQuery_fn * pfnCopy = NULL;
  854. if ( tQuery.m_eType==QUERY_API )
  855. {
  856. pfnCopy = &sphCopyEncodedBase64;
  857. // should be power of 3 to seamlessly convert to BASE64
  858. BYTE dHeader[] = {
  859. (BYTE)( ( tQuery.m_uCMD>>8 ) & 0xff ),
  860. (BYTE)( tQuery.m_uCMD & 0xff ),
  861. (BYTE)( ( tQuery.m_uVer>>8 ) & 0xff ),
  862. (BYTE)( tQuery.m_uVer & 0xff ),
  863. (BYTE)( ( tQuery.m_dQuery.second>>24 ) & 0xff ),
  864. (BYTE)( ( tQuery.m_dQuery.second>>16 ) & 0xff ),
  865. (BYTE)( ( tQuery.m_dQuery.second>>8 ) & 0xff ),
  866. (BYTE)( tQuery.m_dQuery.second & 0xff ),
  867. *tQuery.m_dQuery.first
  868. };
  869. QueryCopyState_t tHeaderState;
  870. tHeaderState.m_pDst = g_dCrashQueryBuff;
  871. tHeaderState.m_pDstEnd = g_dCrashQueryBuff + sizeof(g_dCrashQueryBuff);
  872. tHeaderState.m_pSrc = dHeader;
  873. tHeaderState.m_pSrcEnd = dHeader + sizeof(dHeader);
  874. pfnCopy ( tHeaderState );
  875. assert ( tHeaderState.m_pSrc==tHeaderState.m_pSrcEnd );
  876. tCopyState.m_pDst = tHeaderState.m_pDst;
  877. tCopyState.m_pSrc++;
  878. } else if ( tQuery.m_eType==QUERY_JSON )
  879. {
  880. pfnCopy = &sphCopySphinxHttp;
  881. } else
  882. {
  883. pfnCopy = &sphCopySphinxQL;
  884. }
  885. while ( pfnCopy ( tCopyState ) )
  886. {
  887. sphWrite ( g_iLogFile, g_dCrashQueryBuff, tCopyState.m_pDst-g_dCrashQueryBuff );
  888. tCopyState.m_pDst = g_dCrashQueryBuff; // reset the destination buffer
  889. }
  890. assert ( tCopyState.m_pSrc==tCopyState.m_pSrcEnd );
  891. int iLeft = int ( tCopyState.m_pDst-g_dCrashQueryBuff );
  892. if ( iLeft>0 )
  893. sphWrite ( g_iLogFile, g_dCrashQueryBuff, iLeft );
  894. }
  895. // tail
  896. sphWrite ( g_iLogFile, g_sCrashedBannerTail, sizeof(g_sCrashedBannerTail)-1 );
  897. // index name
  898. sphWrite ( g_iLogFile, g_sCrashedIndex, sizeof (g_sCrashedIndex)-1 );
  899. if ( IsFilled ( tQuery.m_dIndex ) )
  900. sphWrite ( g_iLogFile, tQuery.m_dIndex );
  901. sphWrite ( g_iLogFile, g_sEndLine, sizeof (g_sEndLine)-1 );
  902. sphSafeInfo ( g_iLogFile, g_sBannerVersion.cstr() );
  903. #if _WIN32
  904. // mini-dump reference
  905. int iMiniDumpLen = snprintf ( (char *)g_dCrashQueryBuff, sizeof(g_dCrashQueryBuff),
  906. "%s %s.%p.mdmp\n", g_sMinidumpBanner, g_sMinidump, tQuery.m_dQuery.first );
  907. sphWrite ( g_iLogFile, g_dCrashQueryBuff, iMiniDumpLen );
  908. snprintf ( (char *)g_dCrashQueryBuff, sizeof(g_dCrashQueryBuff), "%s.%p.mdmp",
  909. g_sMinidump, tQuery.m_dQuery.first );
  910. #endif
  911. // log trace
  912. #if !_WIN32
  913. sphSafeInfo ( g_iLogFile, "Handling signal %d", sig );
  914. // print message to stdout during daemon start
  915. if ( g_bLogStdout && g_iLogFile!=STDOUT_FILENO )
  916. sphSafeInfo ( STDOUT_FILENO, "Crash!!! Handling signal %d", sig );
  917. sphBacktrace ( g_iLogFile, g_bSafeTrace );
  918. #else
  919. sphBacktrace ( pExc, (char *)g_dCrashQueryBuff );
  920. #endif
  921. // threads table
  922. sphSafeInfo ( g_iLogFile, "--- active threads ---" );
  923. int iThd = 0;
  924. int iAllThd = 0;
  925. Threads::IterateActive ( [&iThd,&iAllThd] ( Threads::LowThreadDesc_t * pThread )
  926. {
  927. if ( pThread )
  928. {
  929. auto pSrc = (ClientTaskInfo_t *) pThread->m_pTaskInfo.load ( std::memory_order_relaxed );
  930. if ( pSrc ) ++iAllThd;
  931. for ( ; pSrc; pSrc = (ClientTaskInfo_t *) pSrc->m_pPrev.load ( std::memory_order_relaxed ) )
  932. if ( pSrc->m_eType==ClientTaskInfo_t::Task() )
  933. {
  934. sphSafeInfo ( g_iLogFile, "thd %d (%s), proto %s, state %s, command %s", iThd,
  935. pThread->m_sThreadName.cstr(),
  936. ProtoName (pSrc->GetProto()), TaskStateName ( pSrc->GetTaskState() ),
  937. pSrc->m_szCommand ? pSrc->m_szCommand : "-" );
  938. ++iThd;
  939. break;
  940. }
  941. }
  942. } );
  943. sphSafeInfo ( g_iLogFile, "--- Totally %d threads, and %d client-working threads ---", iAllThd, iThd );
  944. // memory info
  945. #if SPH_ALLOCS_PROFILER
  946. sphWrite ( g_iLogFile, g_sMemoryStatBanner, sizeof ( g_sMemoryStatBanner )-1 );
  947. sphMemStatDump ( g_iLogFile );
  948. #endif
  949. sphSafeInfo ( g_iLogFile, "------- CRASH DUMP END -------" );
  950. if ( g_bCoreDump )
  951. {
  952. CRASH_EXIT_CORE;
  953. } else
  954. {
  955. CRASH_EXIT;
  956. }
  957. }
  958. void CrashLogger::SetupTimePID ()
  959. {
  960. char sTimeBuf[SPH_TIME_PID_MAX_SIZE];
  961. sphFormatCurrentTime ( sTimeBuf, sizeof(sTimeBuf) );
  962. g_iCrashInfoLen = snprintf ( g_sCrashInfo, SPH_TIME_PID_MAX_SIZE-1,
  963. "------- FATAL: CRASH DUMP -------\n[%s] [%5d]\n", sTimeBuf, (int)getpid() );
  964. }
  965. #if _WIN32
  966. void SetSignalHandlers ( bool )
  967. {
  968. sphBacktraceInit ();
  969. snprintf ( g_sMinidump, SPH_TIME_PID_MAX_SIZE-1, "%s.%d", g_sPidFile.scstr(), (int)getpid() );
  970. SetUnhandledExceptionFilter ( CrashLogger::HandleCrash );
  971. }
  972. #else
  973. void SetSignalHandlers ( bool bAllowCtrlC=false ) REQUIRES ( MainThread )
  974. {
  975. sphBacktraceInit ();
  976. struct sigaction sa;
  977. sigfillset ( &sa.sa_mask );
  978. sa.sa_flags = SA_NOCLDSTOP;
  979. bool bSignalsSet = false;
  980. auto dFatalOnFail = AtScopeExit( [ &bSignalsSet ]
  981. {
  982. if ( !bSignalsSet )
  983. sphFatal( "sigaction(): %s", strerrorm(errno));
  984. } );
  985. sa.sa_handler = sigterm; if ( sigaction ( SIGTERM, &sa, NULL )!=0 ) return;
  986. if ( !bAllowCtrlC )
  987. {
  988. sa.sa_handler = sigterm;
  989. if ( sigaction ( SIGINT, &sa, NULL )!=0 )
  990. return;
  991. }
  992. sa.sa_handler = sighup; if ( sigaction ( SIGHUP, &sa, NULL )!=0 ) return;
  993. sa.sa_handler = sigusr1; if ( sigaction ( SIGUSR1, &sa, NULL )!=0 ) return;
  994. sa.sa_handler = sigusr2; if ( sigaction ( SIGUSR2, &sa, NULL )!=0 ) return;
  995. sa.sa_handler = SIG_IGN; if ( sigaction ( SIGPIPE, &sa, NULL )!=0 ) return;
  996. sa.sa_flags |= SA_RESETHAND;
  997. static CSphVector<BYTE> exception_handler_stack ( Max ( SIGSTKSZ, 65536 ) );
  998. stack_t ss;
  999. ss.ss_sp = exception_handler_stack.begin();
  1000. ss.ss_flags = 0;
  1001. ss.ss_size = exception_handler_stack.GetLength();
  1002. sigaltstack( &ss, 0 );
  1003. sa.sa_flags |= SA_ONSTACK;
  1004. sa.sa_handler = CrashLogger::HandleCrash; if ( sigaction ( SIGSEGV, &sa, NULL )!=0 ) return;
  1005. sa.sa_handler = CrashLogger::HandleCrash; if ( sigaction ( SIGBUS, &sa, NULL )!=0 ) return;
  1006. sa.sa_handler = CrashLogger::HandleCrash; if ( sigaction ( SIGABRT, &sa, NULL )!=0 ) return;
  1007. sa.sa_handler = CrashLogger::HandleCrash; if ( sigaction ( SIGILL, &sa, NULL )!=0 ) return;
  1008. sa.sa_handler = CrashLogger::HandleCrash; if ( sigaction ( SIGFPE, &sa, NULL )!=0 ) return;
  1009. bSignalsSet = true;
  1010. }
  1011. #endif
  1012. #if !_WIN32
  1013. int sphCreateUnixSocket ( const char * sPath ) REQUIRES ( MainThread )
  1014. {
  1015. static struct sockaddr_un uaddr;
  1016. size_t len = strlen ( sPath );
  1017. if ( len + 1 > sizeof( uaddr.sun_path ) )
  1018. sphFatal ( "UNIX socket path is too long (len=%d)", (int)len );
  1019. sphInfo ( "listening on UNIX socket %s", sPath );
  1020. memset ( &uaddr, 0, sizeof(uaddr) );
  1021. uaddr.sun_family = AF_UNIX;
  1022. memcpy ( uaddr.sun_path, sPath, len + 1 );
  1023. int iSock = socket ( AF_UNIX, SOCK_STREAM, 0 );
  1024. if ( iSock==-1 )
  1025. sphFatal ( "failed to create UNIX socket: %s", sphSockError() );
  1026. if ( unlink ( sPath )==-1 )
  1027. {
  1028. if ( errno!=ENOENT )
  1029. sphFatal ( "unlink() on UNIX socket file failed: %s", sphSockError() );
  1030. }
  1031. int iMask = umask ( 0 );
  1032. if ( bind ( iSock, (struct sockaddr *)&uaddr, sizeof(uaddr) )!=0 )
  1033. sphFatal ( "bind() on UNIX socket failed: %s", sphSockError() );
  1034. umask ( iMask );
  1035. return iSock;
  1036. }
  1037. #endif // !_WIN32
  1038. int sphCreateInetSocket ( const ListenerDesc_t & tDesc ) REQUIRES ( MainThread )
  1039. {
  1040. auto uAddr = tDesc.m_uIP;
  1041. auto iPort = tDesc.m_iPort;
  1042. char sAddress[SPH_ADDRESS_SIZE];
  1043. sphFormatIP ( sAddress, SPH_ADDRESS_SIZE, uAddr );
  1044. const char * sVip = tDesc.m_bVIP ? "VIP " : "";
  1045. const char * sRO = tDesc.m_bReadOnly ? "RO " : "";
  1046. if ( uAddr==htonl ( INADDR_ANY ) )
  1047. sphInfo ( "listening on all interfaces for %s%s%s, port=%d", sVip, sRO, RelaxedProtoName ( tDesc.m_eProto), iPort );
  1048. else
  1049. sphInfo ( "listening on %s:%d for %s%s%s", sAddress, iPort, sVip, sRO, RelaxedProtoName ( tDesc.m_eProto ) );
  1050. static struct sockaddr_in iaddr;
  1051. memset ( &iaddr, 0, sizeof(iaddr) );
  1052. iaddr.sin_family = AF_INET;
  1053. iaddr.sin_addr.s_addr = uAddr;
  1054. iaddr.sin_port = htons ( (short)iPort );
  1055. int iSock = socket ( AF_INET, SOCK_STREAM, 0 );
  1056. if ( iSock==-1 )
  1057. sphFatal ( "failed to create TCP socket: %s", sphSockError() );
  1058. sphSetSockReuseAddr ( iSock );
  1059. sphSetSockReusePort ( iSock );
  1060. sphSetSockNodelay ( iSock );
  1061. int iTries = 12;
  1062. int iRes;
  1063. do
  1064. {
  1065. iRes = bind ( iSock, (struct sockaddr *)&iaddr, sizeof(iaddr) );
  1066. if ( iRes==0 )
  1067. break;
  1068. sphInfo ( "bind() failed on %s, retrying...", sAddress );
  1069. sphLogDebug ( "bind() failed on %s:%d, error: %s", sAddress, iPort, sphSockError() );
  1070. sphSleepMsec ( 3000 );
  1071. } while ( --iTries>0 );
  1072. if ( iRes )
  1073. sphFatal ( "bind() failed on %s: %s", sAddress, sphSockError() );
  1074. return iSock;
  1075. }
  1076. ListenerDesc_t MakeAnyListener ( int iPort, Proto_e eProto=Proto_e::SPHINX )
  1077. {
  1078. ListenerDesc_t tDesc;
  1079. tDesc.m_eProto = eProto;
  1080. tDesc.m_uIP = htonl ( INADDR_ANY );
  1081. tDesc.m_iPort = iPort;
  1082. tDesc.m_iPortsCount = 0;
  1083. tDesc.m_bVIP = false;
  1084. tDesc.m_bReadOnly = false;
  1085. return tDesc;
  1086. }
  1087. ListenerDesc_t MakeLocalhostListener ( int iPort, Proto_e eProto )
  1088. {
  1089. ListenerDesc_t tDesc;
  1090. tDesc.m_eProto = eProto;
  1091. tDesc.m_uIP = htonl ( INADDR_LOOPBACK );
  1092. tDesc.m_iPort = iPort;
  1093. tDesc.m_iPortsCount = 0;
  1094. tDesc.m_bVIP = false;
  1095. tDesc.m_bReadOnly = false;
  1096. return tDesc;
  1097. }
  1098. // add any listener we will serve by our own (i.e. NO galera's since it is not our deal)
  1099. bool AddGlobalListener ( const ListenerDesc_t& tDesc ) REQUIRES ( MainThread )
  1100. {
  1101. if ( tDesc.m_eProto==Proto_e::REPLICATION )
  1102. return false;
  1103. Listener_t tListener;
  1104. tListener.m_eProto = tDesc.m_eProto;
  1105. tListener.m_bTcp = true;
  1106. tListener.m_bVIP = tDesc.m_bVIP;
  1107. tListener.m_bReadOnly = tDesc.m_bReadOnly;
  1108. #if !_WIN32
  1109. if ( !tDesc.m_sUnix.IsEmpty () )
  1110. {
  1111. tListener.m_iSock = sphCreateUnixSocket ( tDesc.m_sUnix.cstr () );
  1112. tListener.m_bTcp = false;
  1113. } else
  1114. #endif
  1115. tListener.m_iSock = sphCreateInetSocket ( tDesc );
  1116. g_dListeners.Add ( tListener );
  1117. return true;
  1118. }
  1119. struct ListenerPortRange_t
  1120. {
  1121. DWORD m_uIP { 0 };
  1122. int m_iPort { 0 };
  1123. int m_iCount { 0 };
  1124. static inline bool IsLess ( const ListenerPortRange_t & tA, const ListenerPortRange_t & tB )
  1125. {
  1126. if ( tA.m_uIP==tB.m_uIP )
  1127. return ( tA.m_iPort<tB.m_iPort );
  1128. return ( tA.m_uIP<tB.m_uIP );
  1129. }
  1130. CSphString Dump () const
  1131. {
  1132. char sAddress[SPH_ADDRESS_SIZE];
  1133. sphFormatIP ( sAddress, SPH_ADDRESS_SIZE, m_uIP );
  1134. CSphString sRes;
  1135. if ( m_iCount )
  1136. sRes.SetSprintf ( "%s:%d-%d", sAddress, m_iPort, ( m_iPort+m_iCount-1 ) );
  1137. else
  1138. sRes.SetSprintf ( "%s:%d", sAddress, m_iPort );
  1139. return sRes;
  1140. }
  1141. };
  1142. static bool ValidateListenerRanges ( const VecTraits_T<ListenerDesc_t> & dListeners, CSphString & sError )
  1143. {
  1144. CSphVector<ListenerPortRange_t> dPorts;
  1145. for ( const ListenerDesc_t & tDesc : dListeners )
  1146. {
  1147. if ( !tDesc.m_sUnix.IsEmpty() )
  1148. continue;
  1149. ListenerPortRange_t & tPort = dPorts.Add();
  1150. tPort.m_uIP = tDesc.m_uIP;
  1151. tPort.m_iPort = tDesc.m_iPort;
  1152. tPort.m_iCount = tDesc.m_iPortsCount;
  1153. }
  1154. dPorts.Sort ( ListenerPortRange_t() );
  1155. for ( int i=1; i<dPorts.GetLength(); i++ )
  1156. {
  1157. const ListenerPortRange_t & tPrev = dPorts[i-1];
  1158. const ListenerPortRange_t & tCur = dPorts[i];
  1159. if ( tPrev.m_uIP!=tCur.m_uIP )
  1160. continue;
  1161. if ( ( !tPrev.m_iCount && tPrev.m_iPort<tCur.m_iPort ) || ( tPrev.m_iCount && tPrev.m_iPort+tPrev.m_iCount-1<tCur.m_iPort ) )
  1162. continue;
  1163. sError.SetSprintf ( "invalid listener ports intersection %s -> %s", tPrev.Dump().cstr(), tCur.Dump().cstr() );
  1164. return false;
  1165. }
  1166. return true;
  1167. }
  1168. /////////////////////////////////////////////////////////////////////////////
  1169. // unpack Mysql Length-coded number
  1170. static int MysqlUnpack ( InputBuffer_c & tReq, DWORD * pSize )
  1171. {
  1172. assert ( pSize );
  1173. int iRes = tReq.GetByte();
  1174. --*pSize;
  1175. if ( iRes < 251 )
  1176. return iRes;
  1177. if ( iRes==0xFC )
  1178. {
  1179. *pSize -=2;
  1180. return tReq.GetByte() + ((int)tReq.GetByte()<<8);
  1181. }
  1182. if ( iRes==0xFD )
  1183. {
  1184. *pSize -= 3;
  1185. return tReq.GetByte() + ((int)tReq.GetByte()<<8) + ((int)tReq.GetByte()<<16);
  1186. }
  1187. if ( iRes==0xFE )
  1188. iRes = tReq.GetByte() + ((int)tReq.GetByte()<<8) + ((int)tReq.GetByte()<<16) + ((int)tReq.GetByte()<<24);
  1189. tReq.GetByte();
  1190. tReq.GetByte();
  1191. tReq.GetByte();
  1192. tReq.GetByte();
  1193. *pSize -= 8;
  1194. return iRes;
  1195. }
  1196. /////////////////////////////////////////////////////////////////////////////
  1197. void ISphOutputBuffer::SendBytes ( const void * pBuf, int iLen )
  1198. {
  1199. m_dBuf.Append ( pBuf, iLen );
  1200. }
  1201. void ISphOutputBuffer::SendBytes ( const char * pBuf )
  1202. {
  1203. if ( !pBuf )
  1204. return;
  1205. SendBytes ( pBuf, (int) strlen ( pBuf ) );
  1206. }
  1207. void ISphOutputBuffer::SendBytes ( const CSphString& sStr )
  1208. {
  1209. SendBytes ( sStr.cstr(), sStr.Length() );
  1210. }
  1211. void ISphOutputBuffer::SendBytes ( const Str_t& sStr )
  1212. {
  1213. m_dBuf.Append ( sStr );
  1214. }
  1215. void ISphOutputBuffer::SendBytes ( const VecTraits_T<BYTE> & dBuf )
  1216. {
  1217. m_dBuf.Append ( dBuf );
  1218. }
  1219. void ISphOutputBuffer::SendBytes ( const StringBuilder_c &dBuf )
  1220. {
  1221. SendBytes ( dBuf.begin(), dBuf.GetLength () );
  1222. }
  1223. void ISphOutputBuffer::SendBytes ( ByteBlob_t dData )
  1224. {
  1225. SendBytes ( dData.first, dData.second );
  1226. }
  1227. void ISphOutputBuffer::SendArray ( const ISphOutputBuffer &tOut )
  1228. {
  1229. int iLen = tOut.m_dBuf.GetLength();
  1230. SendInt ( iLen );
  1231. SendBytes ( tOut.m_dBuf.Begin(), iLen );
  1232. }
  1233. void ISphOutputBuffer::SendArray ( const VecTraits_T<BYTE> &dBuf, int iElems )
  1234. {
  1235. if ( iElems==-1 )
  1236. {
  1237. SendInt ( dBuf.GetLength () );
  1238. SendBytes ( dBuf );
  1239. return;
  1240. }
  1241. assert ( dBuf.GetLength() == (int) dBuf.GetLengthBytes() );
  1242. assert ( iElems<=dBuf.GetLength ());
  1243. SendInt ( iElems );
  1244. SendBytes ( dBuf.begin(), iElems );
  1245. }
  1246. void ISphOutputBuffer::SendArray ( const void * pBuf, int iLen )
  1247. {
  1248. if ( !pBuf )
  1249. iLen=0;
  1250. assert ( iLen>=0 );
  1251. SendInt ( iLen );
  1252. SendBytes ( pBuf, iLen );
  1253. }
  1254. void ISphOutputBuffer::SendArray ( const StringBuilder_c &dBuf )
  1255. {
  1256. SendArray ( dBuf.begin(), dBuf.GetLength () );
  1257. }
  1258. void ISphOutputBuffer::SendArray ( ByteBlob_t dData )
  1259. {
  1260. SendArray ( dData.first, dData.second );
  1261. }
  1262. void SendErrorReply ( ISphOutputBuffer & tOut, const char * sTemplate, ... )
  1263. {
  1264. CSphString sError;
  1265. va_list ap;
  1266. va_start ( ap, sTemplate );
  1267. sError.SetSprintfVa ( sTemplate, ap );
  1268. va_end ( ap );
  1269. auto tHdr = APIHeader ( tOut, SEARCHD_ERROR );
  1270. tOut.SendString ( sError.cstr() );
  1271. // --console logging
  1272. if ( g_bOptNoDetach && g_eLogFormat!=LOG_FORMAT_SPHINXQL )
  1273. sphInfo ( "query error: %s", sError.cstr() );
  1274. }
  1275. void DistributedIndex_t::GetAllHosts ( VectorAgentConn_t &dTarget ) const
  1276. {
  1277. for ( const auto& pMultiAgent : m_dAgents )
  1278. for ( const auto & dHost : *pMultiAgent )
  1279. {
  1280. auto * pAgent = new AgentConn_t;
  1281. pAgent->m_tDesc.CloneFrom ( dHost );
  1282. pAgent->m_iMyQueryTimeoutMs = m_iAgentQueryTimeoutMs;
  1283. pAgent->m_iMyConnectTimeoutMs = m_iAgentConnectTimeoutMs;
  1284. dTarget.Add ( pAgent );
  1285. }
  1286. }
  1287. DistributedIndex_t::~DistributedIndex_t ()
  1288. {
  1289. sphLogDebugv ( "DistributedIndex_t %p removed", this );
  1290. // cleanup global
  1291. m_dAgents.Reset();
  1292. MultiAgentDesc_c::CleanupOrphaned ();
  1293. };
  1294. /////////////////////////////////////////////////////////////////////////////
  1295. // SEARCH HANDLER
  1296. /////////////////////////////////////////////////////////////////////////////
  1297. class SearchRequestBuilder_c : public RequestBuilder_i
  1298. {
  1299. public:
  1300. SearchRequestBuilder_c ( const VecTraits_T<CSphQuery> & dQueries, int iDivideLimits )
  1301. : m_dQueries ( dQueries ), m_iDivideLimits ( iDivideLimits )
  1302. {}
  1303. void BuildRequest ( const AgentConn_t & tAgent, ISphOutputBuffer & tOut ) const final;
  1304. protected:
  1305. void SendQuery ( const char * sIndexes, ISphOutputBuffer & tOut, const CSphQuery & q, int iWeight, int iAgentQueryTimeout ) const;
  1306. protected:
  1307. const VecTraits_T<CSphQuery> & m_dQueries;
  1308. const int m_iDivideLimits;
  1309. };
  1310. class SearchReplyParser_c : public ReplyParser_i, public ISphNoncopyable
  1311. {
  1312. public:
  1313. explicit SearchReplyParser_c ( int iResults )
  1314. : m_iResults ( iResults )
  1315. {}
  1316. bool ParseReply ( MemInputBuffer_c & tReq, AgentConn_t & tAgent ) const final;
  1317. private:
  1318. int m_iResults;
  1319. static void ParseSchema ( OneResultset_t & tRes, MemInputBuffer_c & tReq );
  1320. static void ParseMatch ( CSphMatch & tMatch, MemInputBuffer_c & tReq, const CSphSchema & tSchema, bool bAgent64 );
  1321. };
  1322. /////////////////////////////////////////////////////////////////////////////
  1323. /// qflag means Query Flag
  1324. /// names are internal to searchd and may be changed for clarity
  1325. /// values are communicated over network between searchds and APIs and MUST NOT CHANGE
  1326. enum
  1327. {
  1328. QFLAG_REVERSE_SCAN = 1UL << 0, // deprecated
  1329. QFLAG_SORT_KBUFFER = 1UL << 1,
  1330. QFLAG_MAX_PREDICTED_TIME = 1UL << 2,
  1331. QFLAG_SIMPLIFY = 1UL << 3,
  1332. QFLAG_PLAIN_IDF = 1UL << 4,
  1333. QFLAG_GLOBAL_IDF = 1UL << 5,
  1334. QFLAG_NORMALIZED_TF = 1UL << 6,
  1335. QFLAG_LOCAL_DF = 1UL << 7,
  1336. QFLAG_LOW_PRIORITY = 1UL << 8,
  1337. QFLAG_FACET = 1UL << 9,
  1338. QFLAG_FACET_HEAD = 1UL << 10,
  1339. QFLAG_JSON_QUERY = 1UL << 11,
  1340. QFLAG_NOT_ONLY_ALLOWED = 1UL << 12
  1341. };
  1342. void operator<< ( ISphOutputBuffer & tOut, const CSphNamedInt & tValue )
  1343. {
  1344. tOut.SendString ( tValue.first.cstr () );
  1345. tOut.SendInt ( tValue.second );
  1346. }
  1347. void operator>> ( InputBuffer_c & dIn, CSphNamedInt & tValue )
  1348. {
  1349. tValue.first = dIn.GetString ();
  1350. tValue.second = dIn.GetInt ();
  1351. }
  1352. void SearchRequestBuilder_c::SendQuery ( const char * sIndexes, ISphOutputBuffer & tOut, const CSphQuery & q, int iWeight, int iAgentQueryTimeout ) const
  1353. {
  1354. bool bAgentWeight = ( iWeight!=-1 );
  1355. // starting with command version 1.27, flags go first
  1356. // reason being, i might add flags that affect *any* of the subsequent data (eg. qflag_pack_ints)
  1357. DWORD uFlags = 0;
  1358. uFlags |= QFLAG_SORT_KBUFFER * q.m_bSortKbuffer;
  1359. uFlags |= QFLAG_MAX_PREDICTED_TIME * ( q.m_iMaxPredictedMsec > 0 );
  1360. uFlags |= QFLAG_SIMPLIFY * q.m_bSimplify;
  1361. uFlags |= QFLAG_PLAIN_IDF * q.m_bPlainIDF;
  1362. uFlags |= QFLAG_GLOBAL_IDF * q.m_bGlobalIDF;
  1363. uFlags |= QFLAG_NORMALIZED_TF * q.m_bNormalizedTFIDF;
  1364. uFlags |= QFLAG_LOCAL_DF * q.m_bLocalDF;
  1365. uFlags |= QFLAG_LOW_PRIORITY * q.m_bLowPriority;
  1366. uFlags |= QFLAG_FACET * q.m_bFacet;
  1367. uFlags |= QFLAG_FACET_HEAD * q.m_bFacetHead;
  1368. uFlags |= QFLAG_NOT_ONLY_ALLOWED * q.m_bNotOnlyAllowed;
  1369. if ( q.m_eQueryType==QUERY_JSON )
  1370. uFlags |= QFLAG_JSON_QUERY;
  1371. tOut.SendDword ( uFlags );
  1372. // The Search Legacy
  1373. tOut.SendInt ( 0 ); // offset is 0
  1374. if ( !q.m_bHasOuter )
  1375. {
  1376. if ( m_iDivideLimits==1 )
  1377. tOut.SendInt ( q.m_iMaxMatches ); // OPTIMIZE? normally, agent limit is max_matches, even if master limit is less
  1378. else // FIXME!!! that is broken with offset + limit
  1379. tOut.SendInt ( 1 + ( ( q.m_iOffset + q.m_iLimit )/m_iDivideLimits) );
  1380. } else
  1381. {
  1382. // with outer order by, inner limit must match between agent and master
  1383. tOut.SendInt ( q.m_iLimit );
  1384. }
  1385. tOut.SendInt ( (DWORD)q.m_eMode ); // match mode
  1386. tOut.SendInt ( (DWORD)q.m_eRanker ); // ranking mode
  1387. if ( q.m_eRanker==SPH_RANK_EXPR || q.m_eRanker==SPH_RANK_EXPORT )
  1388. tOut.SendString ( q.m_sRankerExpr.cstr() );
  1389. tOut.SendInt ( q.m_eSort ); // sort mode
  1390. tOut.SendString ( q.m_sSortBy.cstr() ); // sort attr
  1391. if ( q.m_eQueryType==QUERY_JSON )
  1392. tOut.SendString ( q.m_sQuery.cstr() );
  1393. else
  1394. {
  1395. if ( q.m_sRawQuery.IsEmpty() )
  1396. tOut.SendString ( q.m_sQuery.cstr() );
  1397. else
  1398. tOut.SendString ( q.m_sRawQuery.cstr() ); // query
  1399. }
  1400. tOut.SendInt ( q.m_dWeights.GetLength() );
  1401. ARRAY_FOREACH ( j, q.m_dWeights )
  1402. tOut.SendInt ( q.m_dWeights[j] ); // weights
  1403. tOut.SendString ( sIndexes ); // indexes
  1404. tOut.SendInt ( 1 ); // id range bits
  1405. tOut.SendUint64 ( uint64_t(0) ); // default full id range (any client range must be in filters at this stage)
  1406. tOut.SendUint64 ( UINT64_MAX );
  1407. tOut.SendInt ( q.m_dFilters.GetLength() );
  1408. ARRAY_FOREACH ( j, q.m_dFilters )
  1409. {
  1410. const CSphFilterSettings & tFilter = q.m_dFilters[j];
  1411. tOut.SendString ( tFilter.m_sAttrName.cstr() );
  1412. tOut.SendInt ( tFilter.m_eType );
  1413. switch ( tFilter.m_eType )
  1414. {
  1415. case SPH_FILTER_VALUES:
  1416. tOut.SendInt ( tFilter.GetNumValues() );
  1417. for ( auto uValue : tFilter.GetValues () )
  1418. tOut.SendUint64 ( uValue );
  1419. break;
  1420. case SPH_FILTER_RANGE:
  1421. tOut.SendUint64 ( tFilter.m_iMinValue );
  1422. tOut.SendUint64 ( tFilter.m_iMaxValue );
  1423. break;
  1424. case SPH_FILTER_FLOATRANGE:
  1425. tOut.SendFloat ( tFilter.m_fMinValue );
  1426. tOut.SendFloat ( tFilter.m_fMaxValue );
  1427. break;
  1428. case SPH_FILTER_USERVAR:
  1429. case SPH_FILTER_STRING:
  1430. tOut.SendString ( tFilter.m_dStrings.GetLength()==1 ? tFilter.m_dStrings[0].cstr() : nullptr );
  1431. break;
  1432. case SPH_FILTER_NULL:
  1433. tOut.SendByte ( tFilter.m_bIsNull );
  1434. break;
  1435. case SPH_FILTER_STRING_LIST:
  1436. tOut.SendInt ( tFilter.m_dStrings.GetLength() );
  1437. ARRAY_FOREACH ( iString, tFilter.m_dStrings )
  1438. tOut.SendString ( tFilter.m_dStrings[iString].cstr() );
  1439. break;
  1440. case SPH_FILTER_EXPRESSION: // need only name and type
  1441. break;
  1442. }
  1443. tOut.SendInt ( tFilter.m_bExclude );
  1444. tOut.SendInt ( tFilter.m_bHasEqualMin );
  1445. tOut.SendInt ( tFilter.m_bHasEqualMax );
  1446. tOut.SendInt ( tFilter.m_bOpenLeft );
  1447. tOut.SendInt ( tFilter.m_bOpenRight );
  1448. tOut.SendInt ( tFilter.m_eMvaFunc );
  1449. }
  1450. tOut.SendInt ( q.m_eGroupFunc );
  1451. tOut.SendString ( q.m_sGroupBy.cstr() );
  1452. if ( m_iDivideLimits==1 )
  1453. tOut.SendInt ( q.m_iMaxMatches );
  1454. else
  1455. tOut.SendInt ( 1+(q.m_iMaxMatches/m_iDivideLimits) ); // Reduce the max_matches also.
  1456. tOut.SendString ( q.m_sGroupSortBy.cstr() );
  1457. tOut.SendInt ( q.m_iCutoff );
  1458. tOut.SendInt ( q.m_iRetryCount<0 ? 0 : q.m_iRetryCount ); // runaround for old clients.
  1459. tOut.SendInt ( q.m_iRetryDelay<0 ? 0 : q.m_iRetryDelay );
  1460. tOut.SendString ( q.m_sGroupDistinct.cstr() );
  1461. tOut.SendInt ( q.m_bGeoAnchor );
  1462. if ( q.m_bGeoAnchor )
  1463. {
  1464. tOut.SendString ( q.m_sGeoLatAttr.cstr() );
  1465. tOut.SendString ( q.m_sGeoLongAttr.cstr() );
  1466. tOut.SendFloat ( q.m_fGeoLatitude );
  1467. tOut.SendFloat ( q.m_fGeoLongitude );
  1468. }
  1469. if ( bAgentWeight )
  1470. {
  1471. tOut.SendInt ( 1 );
  1472. tOut.SendString ( "*" );
  1473. tOut.SendInt ( iWeight );
  1474. } else
  1475. {
  1476. tOut.SendInt ( q.m_dIndexWeights.GetLength() );
  1477. for ( const auto& dWeight : q.m_dIndexWeights )
  1478. tOut << dWeight;
  1479. }
  1480. DWORD iQueryTimeout = ( q.m_uMaxQueryMsec ? q.m_uMaxQueryMsec : iAgentQueryTimeout );
  1481. tOut.SendDword ( iQueryTimeout );
  1482. tOut.SendInt ( q.m_dFieldWeights.GetLength() );
  1483. for ( const auto & dWeight : q.m_dFieldWeights )
  1484. tOut << dWeight;
  1485. tOut.SendString ( q.m_sComment.cstr() );
  1486. tOut.SendInt ( 0 ); // WAS: overrides
  1487. tOut.SendString ( q.m_sSelect.cstr() );
  1488. if ( q.m_iMaxPredictedMsec>0 )
  1489. tOut.SendInt ( q.m_iMaxPredictedMsec );
  1490. // emulate empty sud-select for agent (client ver 1.29) as master sends fixed outer offset+limits
  1491. tOut.SendString ( NULL );
  1492. tOut.SendInt ( 0 );
  1493. tOut.SendInt ( 0 );
  1494. tOut.SendInt ( q.m_bHasOuter );
  1495. // master-agent extensions
  1496. tOut.SendDword ( q.m_eCollation ); // v.1
  1497. tOut.SendString ( q.m_sOuterOrderBy.cstr() ); // v.2
  1498. if ( q.m_bHasOuter )
  1499. tOut.SendInt ( q.m_iOuterOffset + q.m_iOuterLimit );
  1500. tOut.SendInt ( q.m_iGroupbyLimit );
  1501. tOut.SendString ( q.m_sUDRanker.cstr() );
  1502. tOut.SendString ( q.m_sUDRankerOpts.cstr() );
  1503. tOut.SendString ( q.m_sQueryTokenFilterLib.cstr() );
  1504. tOut.SendString ( q.m_sQueryTokenFilterName.cstr() );
  1505. tOut.SendString ( q.m_sQueryTokenFilterOpts.cstr() );
  1506. tOut.SendInt ( q.m_dFilterTree.GetLength() );
  1507. ARRAY_FOREACH ( i, q.m_dFilterTree )
  1508. {
  1509. tOut.SendInt ( q.m_dFilterTree[i].m_iLeft );
  1510. tOut.SendInt ( q.m_dFilterTree[i].m_iRight );
  1511. tOut.SendInt ( q.m_dFilterTree[i].m_iFilterItem );
  1512. tOut.SendInt ( q.m_dFilterTree[i].m_bOr );
  1513. }
  1514. tOut.SendInt( q.m_dItems.GetLength() );
  1515. ARRAY_FOREACH ( i, q.m_dItems )
  1516. {
  1517. const CSphQueryItem & tItem = q.m_dItems[i];
  1518. tOut.SendString ( tItem.m_sAlias.cstr() );
  1519. tOut.SendString ( tItem.m_sExpr.cstr() );
  1520. tOut.SendDword ( tItem.m_eAggrFunc );
  1521. }
  1522. tOut.SendInt( q.m_dRefItems.GetLength() );
  1523. ARRAY_FOREACH ( i, q.m_dRefItems )
  1524. {
  1525. const CSphQueryItem & tItem = q.m_dRefItems[i];
  1526. tOut.SendString ( tItem.m_sAlias.cstr() );
  1527. tOut.SendString ( tItem.m_sExpr.cstr() );
  1528. tOut.SendDword ( tItem.m_eAggrFunc );
  1529. }
  1530. tOut.SendDword ( q.m_eExpandKeywords );
  1531. tOut.SendInt ( q.m_dIndexHints.GetLength() );
  1532. for ( const auto & i : q.m_dIndexHints )
  1533. {
  1534. tOut.SendDword ( (DWORD)i.m_dHints[int(SecondaryIndexType_e::INDEX)] );
  1535. tOut.SendString ( i.m_sIndex.cstr() );
  1536. }
  1537. }
  1538. void SearchRequestBuilder_c::BuildRequest ( const AgentConn_t & tAgent, ISphOutputBuffer & tOut ) const
  1539. {
  1540. auto tHdr = APIHeader ( tOut, SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH ); // API header
  1541. tOut.SendInt ( VER_COMMAND_SEARCH_MASTER );
  1542. tOut.SendInt ( m_dQueries.GetLength() );
  1543. for ( auto& dQuery : m_dQueries )
  1544. SendQuery ( tAgent.m_tDesc.m_sIndexes.cstr (), tOut, dQuery, tAgent.m_iWeight, tAgent.m_iMyQueryTimeoutMs );
  1545. }
  1546. struct cSearchResult : public iQueryResult
  1547. {
  1548. CSphVector<AggrResult_t> m_dResults;
  1549. void Reset () final
  1550. {
  1551. m_dResults.Reset();
  1552. }
  1553. bool HasWarnings () const final
  1554. {
  1555. return m_dResults.any_of ( [] ( const AggrResult_t &dRes ) { return !dRes.m_sWarning.IsEmpty (); } );
  1556. }
  1557. };
  1558. /////////////////////////////////////////////////////////////////////////////
  1559. void SearchReplyParser_c::ParseMatch ( CSphMatch & tMatch, MemInputBuffer_c & tReq, const CSphSchema & tSchema, bool bAgent64 )
  1560. {
  1561. tMatch.Reset ( tSchema.GetRowSize() );
  1562. // WAS: docids
  1563. if ( bAgent64 )
  1564. tReq.GetUint64();
  1565. else
  1566. tReq.GetDword();
  1567. tMatch.m_iWeight = tReq.GetInt ();
  1568. for ( int i=0; i<tSchema.GetAttrsCount(); ++i )
  1569. {
  1570. const CSphColumnInfo & tAttr = tSchema.GetAttr(i);
  1571. assert ( sphPlainAttrToPtrAttr(tAttr.m_eAttrType)==tAttr.m_eAttrType );
  1572. switch ( tAttr.m_eAttrType )
  1573. {
  1574. case SPH_ATTR_UINT32SET_PTR:
  1575. case SPH_ATTR_INT64SET_PTR:
  1576. {
  1577. int iValues = tReq.GetDword ();
  1578. BYTE * pData = nullptr;
  1579. BYTE * pPacked = sphPackPtrAttr ( iValues*sizeof(DWORD), &pData );
  1580. tMatch.SetAttr ( tAttr.m_tLocator, (SphAttr_t)pPacked );
  1581. auto * pMVA = (DWORD *)pData;
  1582. if ( tAttr.m_eAttrType==SPH_ATTR_UINT32SET_PTR )
  1583. {
  1584. while ( iValues-- )
  1585. sphUnalignedWrite ( pMVA++, tReq.GetDword() );
  1586. } else
  1587. {
  1588. assert ( ( iValues%2 )==0 );
  1589. for ( ; iValues; iValues -= 2 )
  1590. {
  1591. uint64_t uMva = tReq.GetUint64();
  1592. sphUnalignedWrite ( pMVA, uMva );
  1593. pMVA += 2;
  1594. }
  1595. }
  1596. }
  1597. break;
  1598. case SPH_ATTR_STRINGPTR:
  1599. case SPH_ATTR_JSON_PTR:
  1600. case SPH_ATTR_FACTORS:
  1601. case SPH_ATTR_FACTORS_JSON:
  1602. {
  1603. int iLen = tReq.GetDword();
  1604. BYTE * pData = nullptr;
  1605. if (iLen)
  1606. {
  1607. tMatch.SetAttr ( tAttr.m_tLocator, (SphAttr_t)sphPackPtrAttr ( iLen, &pData ) );
  1608. tReq.GetBytes ( pData, iLen );
  1609. } else
  1610. tMatch.SetAttr ( tAttr.m_tLocator, (SphAttr_t) 0 );
  1611. }
  1612. break;
  1613. case SPH_ATTR_JSON_FIELD_PTR:
  1614. {
  1615. // FIXME: no reason for json_field to be any different from other *_PTR attributes
  1616. auto eJson = (ESphJsonType)tReq.GetByte();
  1617. if ( eJson==JSON_EOF )
  1618. tMatch.SetAttr ( tAttr.m_tLocator, 0 );
  1619. else
  1620. {
  1621. int iLen = tReq.GetDword();
  1622. BYTE * pData = nullptr;
  1623. tMatch.SetAttr ( tAttr.m_tLocator, (SphAttr_t)sphPackPtrAttr ( iLen+1, &pData ) );
  1624. *pData++ = (BYTE)eJson;
  1625. tReq.GetBytes ( pData, iLen );
  1626. }
  1627. }
  1628. break;
  1629. case SPH_ATTR_FLOAT:
  1630. tMatch.SetAttrFloat ( tAttr.m_tLocator, tReq.GetFloat() );
  1631. break;
  1632. case SPH_ATTR_DOUBLE:
  1633. tMatch.SetAttrDouble ( tAttr.m_tLocator, tReq.GetDouble() );
  1634. break;
  1635. case SPH_ATTR_BIGINT:
  1636. tMatch.SetAttr ( tAttr.m_tLocator, tReq.GetUint64() );
  1637. break;
  1638. default:
  1639. tMatch.SetAttr ( tAttr.m_tLocator, tReq.GetDword() );
  1640. break;
  1641. }
  1642. }
  1643. }
  1644. void SearchReplyParser_c::ParseSchema ( OneResultset_t & tRes, MemInputBuffer_c & tReq )
  1645. {
  1646. CSphSchema & tSchema = tRes.m_tSchema;
  1647. tSchema.Reset ();
  1648. int nFields = tReq.GetInt(); // FIXME! add a sanity check
  1649. for ( int j = 0; j < nFields; ++j )
  1650. tSchema.AddField ( tReq.GetString().cstr() );
  1651. int iNumAttrs = tReq.GetInt(); // FIXME! add a sanity check
  1652. for ( int j=0; j<iNumAttrs; ++j )
  1653. {
  1654. CSphColumnInfo tCol;
  1655. tCol.m_sName = tReq.GetString ();
  1656. tCol.m_eAttrType = (ESphAttr) tReq.GetDword (); // FIXME! add a sanity check
  1657. // we always work with plain attrs (not *_PTR) when working with agents
  1658. tCol.m_eAttrType = sphPlainAttrToPtrAttr ( tCol.m_eAttrType );
  1659. if ( tCol.m_eAttrType==SPH_ATTR_STORED_FIELD )
  1660. {
  1661. tCol.m_eAttrType = SPH_ATTR_STRINGPTR;
  1662. tCol.m_uFieldFlags = CSphColumnInfo::FIELD_STORED;
  1663. }
  1664. tSchema.AddAttr ( tCol, true ); // all attributes received from agents are dynamic
  1665. }
  1666. }
  1667. bool SearchReplyParser_c::ParseReply ( MemInputBuffer_c & tReq, AgentConn_t & tAgent ) const
  1668. {
  1669. const int iResults = m_iResults;
  1670. assert ( iResults>0 );
  1671. if ( !tAgent.m_pResult )
  1672. tAgent.m_pResult = std::make_unique<cSearchResult>();
  1673. auto pResult = (cSearchResult*)tAgent.m_pResult.get();
  1674. auto &dResults = pResult->m_dResults;
  1675. dResults.Resize ( iResults );
  1676. for ( auto & tRes : dResults )
  1677. {
  1678. tRes.m_iSuccesses = 0;
  1679. OneResultset_t tChunk;
  1680. tChunk.m_iTag = tAgent.m_iStoreTag;
  1681. tChunk.m_bTag = true;
  1682. tChunk.m_pAgent = &tAgent;
  1683. tRes.m_sError = "";
  1684. tRes.m_sWarning = "";
  1685. // get status and message
  1686. auto eStatus = ( SearchdStatus_e ) tReq.GetDword ();
  1687. switch ( eStatus )
  1688. {
  1689. case SEARCHD_ERROR: tRes.m_sError = tReq.GetString (); continue;
  1690. case SEARCHD_RETRY: tRes.m_sError = tReq.GetString (); break;
  1691. case SEARCHD_WARNING: tRes.m_sWarning = tReq.GetString (); break;
  1692. default: tAgent.m_sFailure.SetSprintf ( "internal error: unknown status %d, message %s", eStatus, tReq.GetString ().cstr() );
  1693. case SEARCHD_OK: break;
  1694. }
  1695. ParseSchema ( tChunk, tReq );
  1696. // get matches
  1697. int iMatches = tReq.GetInt ();
  1698. if ( iMatches<0 )
  1699. {
  1700. tAgent.m_sFailure.SetSprintf ( "invalid match count received (count=%d)", iMatches );
  1701. return false;
  1702. }
  1703. bool bAgent64 = !!tReq.GetInt();
  1704. if ( !bAgent64 )
  1705. {
  1706. tAgent.m_sFailure.SetSprintf ( "agent has 32-bit docids; no longer supported" );
  1707. return false;
  1708. }
  1709. tChunk.m_dMatches.Resize ( iMatches );
  1710. for ( auto & tMatch : tChunk.m_dMatches )
  1711. ParseMatch ( tMatch, tReq, tChunk.m_tSchema, bAgent64 );
  1712. // read totals (retrieved count, total count, query time, word count)
  1713. int iRetrieved = tReq.GetInt ();
  1714. tRes.m_iTotalMatches = tReq.GetInt ();
  1715. tRes.m_bTotalMatchesApprox = !!tReq.GetInt();
  1716. tRes.m_iQueryTime = tReq.GetInt ();
  1717. // agents always send IO/CPU stats to master
  1718. BYTE uStatMask = tReq.GetByte();
  1719. if ( uStatMask & 1U )
  1720. {
  1721. tRes.m_tIOStats.m_iReadTime = tReq.GetUint64();
  1722. tRes.m_tIOStats.m_iReadOps = tReq.GetDword();
  1723. tRes.m_tIOStats.m_iReadBytes = tReq.GetUint64();
  1724. tRes.m_tIOStats.m_iWriteTime = tReq.GetUint64();
  1725. tRes.m_tIOStats.m_iWriteOps = tReq.GetDword();
  1726. tRes.m_tIOStats.m_iWriteBytes = tReq.GetUint64();
  1727. }
  1728. if ( uStatMask & 2U )
  1729. tRes.m_iCpuTime = tReq.GetUint64();
  1730. if ( uStatMask & 4U )
  1731. tRes.m_iPredictedTime = tReq.GetUint64();
  1732. tRes.m_iAgentFetchedDocs = tReq.GetDword();
  1733. tRes.m_iAgentFetchedHits = tReq.GetDword();
  1734. tRes.m_iAgentFetchedSkips = tReq.GetDword();
  1735. const int iWordsCount = tReq.GetInt (); // FIXME! sanity check?
  1736. if ( iRetrieved!=iMatches )
  1737. {
  1738. tAgent.m_sFailure.SetSprintf ( "expected %d retrieved documents, got %d", iMatches, iRetrieved );
  1739. return false;
  1740. }
  1741. // read per-word stats
  1742. for ( int i=0; i<iWordsCount; ++i )
  1743. {
  1744. const CSphString sWord = tReq.GetString ();
  1745. const int64_t iDocs = (unsigned int)tReq.GetInt ();
  1746. const int64_t iHits = (unsigned int)tReq.GetInt ();
  1747. tReq.GetByte(); // statistics have no expanded terms for now
  1748. tRes.AddStat ( sWord, iDocs, iHits );
  1749. }
  1750. // mark this result as ok
  1751. auto& tNewChunk = tRes.m_dResults.Add ();
  1752. ::Swap ( tNewChunk, tChunk );
  1753. tRes.m_iSuccesses = 1;
  1754. }
  1755. // all seems OK (and buffer length checks are performed by caller)
  1756. return true;
  1757. }
  1758. /////////////////////////////////////////////////////////////////////////////
  1759. // returns true if incoming schema (src) is compatible with existing (dst); false otherwise
  1760. bool MinimizeSchema ( CSphSchema & tDst, const ISphSchema & tSrc )
  1761. {
  1762. // if dst is empty, result is also empty
  1763. if ( tDst.GetAttrsCount()==0 )
  1764. return tSrc.GetAttrsCount()==0;
  1765. // check for equality, and remove all dst attributes that are not present in src
  1766. CSphVector<CSphColumnInfo> dDst;
  1767. for ( int i = 0, iAttrsCount = tDst.GetAttrsCount (); i<iAttrsCount; ++i )
  1768. dDst.Add ( tDst.GetAttr(i) );
  1769. bool bEqual = ( tDst.GetAttrsCount()==tSrc.GetAttrsCount() );
  1770. ARRAY_FOREACH ( i, dDst )
  1771. {
  1772. auto& tDstAttr = dDst[i];
  1773. int iSrcIdx = tSrc.GetAttrIndex ( tDstAttr.m_sName.cstr() );
  1774. // check for index mismatch
  1775. if ( iSrcIdx!=i )
  1776. bEqual = false;
  1777. // check for type/size mismatch (and fixup if needed)
  1778. if ( iSrcIdx>=0 )
  1779. {
  1780. const CSphColumnInfo & tSrcAttr = tSrc.GetAttr ( iSrcIdx );
  1781. // should seamlessly convert ( bool > float ) | ( bool > int > bigint )
  1782. ESphAttr eDst = tDstAttr.m_eAttrType;
  1783. ESphAttr eSrc = tSrcAttr.m_eAttrType;
  1784. bool bSame = ( eDst==eSrc )
  1785. || ( ( eDst==SPH_ATTR_FLOAT && eSrc==SPH_ATTR_BOOL ) || ( eDst==SPH_ATTR_BOOL && eSrc==SPH_ATTR_FLOAT ) )
  1786. || ( ( eDst==SPH_ATTR_BOOL || eDst==SPH_ATTR_INTEGER || eDst==SPH_ATTR_BIGINT )
  1787. && ( eSrc==SPH_ATTR_BOOL || eSrc==SPH_ATTR_INTEGER || eSrc==SPH_ATTR_BIGINT ) );
  1788. int iDstBitCount = tDstAttr.m_tLocator.m_iBitCount;
  1789. int iSrcBitCount = tSrcAttr.m_tLocator.m_iBitCount;
  1790. if ( !bSame )
  1791. {
  1792. // different types? remove the attr
  1793. iSrcIdx = -1;
  1794. bEqual = false;
  1795. } else if ( iDstBitCount!=iSrcBitCount )
  1796. {
  1797. // different bit sizes? choose the max one
  1798. tDstAttr.m_tLocator.m_iBitCount = Max ( iDstBitCount, iSrcBitCount );
  1799. bEqual = false;
  1800. if ( iDstBitCount<iSrcBitCount )
  1801. tDstAttr.m_eAttrType = tSrcAttr.m_eAttrType;
  1802. }
  1803. if ( tSrcAttr.m_tLocator.m_iBitOffset!=tDstAttr.m_tLocator.m_iBitOffset )
  1804. {
  1805. // different offsets? have to force target dynamic then, since we can't use one locator for all matches
  1806. bEqual = false;
  1807. }
  1808. if ( tSrcAttr.m_tLocator.m_bDynamic!=tDstAttr.m_tLocator.m_bDynamic )
  1809. {
  1810. // different location? have to force target dynamic then
  1811. bEqual = false;
  1812. }
  1813. }
  1814. // check for presence
  1815. if ( iSrcIdx<0 )
  1816. {
  1817. dDst.Remove ( i );
  1818. --i;
  1819. }
  1820. }
  1821. if ( !bEqual )
  1822. {
  1823. CSphVector<CSphColumnInfo> dFields { tDst.GetFieldsCount() };
  1824. for ( int i = 0, iFieldsCount = tDst.GetFieldsCount (); i<iFieldsCount; ++i )
  1825. dFields[i] = tDst.GetField(i);
  1826. tDst.Reset();
  1827. for ( auto& dAttr : dDst )
  1828. tDst.AddAttr ( dAttr, true );
  1829. for ( auto& dField: dFields )
  1830. tDst.AddField ( dField );
  1831. } else
  1832. tDst.SwapAttrs ( dDst );
  1833. return bEqual;
  1834. }
  1835. static void CheckQuery ( const CSphQuery & tQuery, CSphString & sError )
  1836. {
  1837. #define LOC_ERROR( ... ) do { sError.SetSprintf (__VA_ARGS__); return; } while(0)
  1838. sError = nullptr;
  1839. if ( (int)tQuery.m_eMode<0 || tQuery.m_eMode>SPH_MATCH_TOTAL )
  1840. LOC_ERROR ( "invalid match mode %d", tQuery.m_eMode );
  1841. if ( (int)tQuery.m_eRanker<0 || tQuery.m_eRanker>SPH_RANK_TOTAL )
  1842. LOC_ERROR ( "invalid ranking mode %d", tQuery.m_eRanker );
  1843. if ( tQuery.m_iMaxMatches<1 )
  1844. LOC_ERROR ( "max_matches can not be less than one" );
  1845. if ( tQuery.m_iOffset<0 || tQuery.m_iOffset>=tQuery.m_iMaxMatches )
  1846. LOC_ERROR ( "offset out of bounds (offset=%d, max_matches=%d)", tQuery.m_iOffset, tQuery.m_iMaxMatches );
  1847. if ( tQuery.m_iLimit<0 )
  1848. LOC_ERROR ( "limit out of bounds (limit=%d)", tQuery.m_iLimit );
  1849. if ( tQuery.m_iCutoff<-1 )
  1850. LOC_ERROR ( "cutoff out of bounds (cutoff=%d)", tQuery.m_iCutoff );
  1851. if ( ( tQuery.m_iRetryCount!=-1 ) && ( tQuery.m_iRetryCount>MAX_RETRY_COUNT ) )
  1852. LOC_ERROR ( "retry count out of bounds (count=%d)", tQuery.m_iRetryCount );
  1853. if ( ( tQuery.m_iRetryDelay!=-1 ) && ( tQuery.m_iRetryDelay>MAX_RETRY_DELAY ) )
  1854. LOC_ERROR ( "retry delay out of bounds (delay=%d)", tQuery.m_iRetryDelay );
  1855. if ( tQuery.m_iOffset>0 && tQuery.m_bHasOuter )
  1856. LOC_ERROR ( "inner offset must be 0 when using outer order by (offset=%d)", tQuery.m_iOffset );
  1857. #undef LOC_ERROR
  1858. }
  1859. void PrepareQueryEmulation ( CSphQuery * pQuery )
  1860. {
  1861. if ( pQuery->m_eMode==SPH_MATCH_BOOLEAN )
  1862. pQuery->m_eRanker = SPH_RANK_NONE;
  1863. if ( pQuery->m_eMode==SPH_MATCH_FULLSCAN )
  1864. pQuery->m_sQuery = "";
  1865. if ( pQuery->m_eMode!=SPH_MATCH_ALL && pQuery->m_eMode!=SPH_MATCH_ANY && pQuery->m_eMode!=SPH_MATCH_PHRASE )
  1866. return;
  1867. const char * szQuery = pQuery->m_sRawQuery.cstr ();
  1868. int iQueryLen = szQuery ? (int) strlen(szQuery) : 0;
  1869. pQuery->m_sQuery.Reserve ( iQueryLen*2+8 );
  1870. char * szRes = (char*) pQuery->m_sQuery.cstr ();
  1871. char c;
  1872. if ( pQuery->m_eMode==SPH_MATCH_ANY || pQuery->m_eMode==SPH_MATCH_PHRASE )
  1873. *szRes++ = '\"';
  1874. if ( iQueryLen )
  1875. {
  1876. while ( ( c = *szQuery++ )!=0 )
  1877. {
  1878. // must be in sync with EscapeString (php api)
  1879. const char sMagics[] = "<\\()|-!@~\"&/^$=";
  1880. for ( const char * s = sMagics; *s; s++ )
  1881. if ( c==*s )
  1882. {
  1883. *szRes++ = '\\';
  1884. break;
  1885. }
  1886. *szRes++ = c;
  1887. }
  1888. }
  1889. switch ( pQuery->m_eMode )
  1890. {
  1891. case SPH_MATCH_ALL: pQuery->m_eRanker = SPH_RANK_PROXIMITY; *szRes = '\0'; break;
  1892. case SPH_MATCH_ANY: pQuery->m_eRanker = SPH_RANK_MATCHANY; strncpy ( szRes, "\"/1", 8 ); break;
  1893. case SPH_MATCH_PHRASE: pQuery->m_eRanker = SPH_RANK_PROXIMITY; *szRes++ = '\"'; *szRes = '\0'; break;
  1894. default: return;
  1895. }
  1896. }
  1897. static void FixupQuerySettings ( CSphQuery & tQuery )
  1898. {
  1899. // sort filters
  1900. for ( auto & i : tQuery.m_dFilters )
  1901. i.m_dValues.Sort();
  1902. if ( !tQuery.m_bHasOuter )
  1903. {
  1904. tQuery.m_sOuterOrderBy = "";
  1905. tQuery.m_iOuterOffset = 0;
  1906. tQuery.m_iOuterLimit = 0;
  1907. }
  1908. }
  1909. static bool ParseSearchFilter ( CSphFilterSettings & tFilter, InputBuffer_c & tReq, ISphOutputBuffer & tOut, int iMasterVer )
  1910. {
  1911. tFilter.m_sAttrName = tReq.GetString ();
  1912. sphColumnToLowercase ( const_cast<char *>( tFilter.m_sAttrName.cstr() ) );
  1913. tFilter.m_eType = (ESphFilter) tReq.GetDword ();
  1914. switch ( tFilter.m_eType )
  1915. {
  1916. case SPH_FILTER_RANGE:
  1917. tFilter.m_iMinValue = tReq.GetUint64();
  1918. tFilter.m_iMaxValue = tReq.GetUint64();
  1919. break;
  1920. case SPH_FILTER_FLOATRANGE:
  1921. tFilter.m_fMinValue = tReq.GetFloat ();
  1922. tFilter.m_fMaxValue = tReq.GetFloat ();
  1923. break;
  1924. case SPH_FILTER_VALUES:
  1925. {
  1926. int iGot = 0;
  1927. bool bRes = tReq.GetQwords ( tFilter.m_dValues, iGot, g_iMaxFilterValues );
  1928. if ( !bRes )
  1929. {
  1930. SendErrorReply ( tOut, "invalid attribute '%s' set length %d (should be in 0..%d range)", tFilter.m_sAttrName.cstr(), iGot, g_iMaxFilterValues );
  1931. return false;
  1932. }
  1933. }
  1934. break;
  1935. case SPH_FILTER_STRING:
  1936. tFilter.m_dStrings.Add ( tReq.GetString() );
  1937. break;
  1938. case SPH_FILTER_NULL:
  1939. tFilter.m_bIsNull = tReq.GetByte()!=0;
  1940. break;
  1941. case SPH_FILTER_USERVAR:
  1942. tFilter.m_dStrings.Add ( tReq.GetString() );
  1943. break;
  1944. case SPH_FILTER_STRING_LIST:
  1945. {
  1946. int iCount = tReq.GetDword();
  1947. if ( iCount<0 || iCount>g_iMaxFilterValues )
  1948. {
  1949. SendErrorReply ( tOut, "invalid attribute '%s' set length %d (should be in 0..%d range)", tFilter.m_sAttrName.cstr(), iCount, g_iMaxFilterValues );
  1950. return false;
  1951. }
  1952. tFilter.m_dStrings.Resize ( iCount );
  1953. ARRAY_FOREACH ( iString, tFilter.m_dStrings )
  1954. tFilter.m_dStrings[iString] = tReq.GetString();
  1955. }
  1956. break;
  1957. case SPH_FILTER_EXPRESSION: // need only name and type
  1958. break;
  1959. default:
  1960. SendErrorReply ( tOut, "unknown filter type (type-id=%d)", tFilter.m_eType );
  1961. return false;
  1962. }
  1963. if ( tFilter.m_sAttrName=="@id" )
  1964. {
  1965. // request coming from old master, need to fix attribute name
  1966. tFilter.m_sAttrName = "id";
  1967. // and clamp values from uint64_t to int64_t
  1968. if ( (uint64_t)tFilter.m_iMinValue > (uint64_t)LLONG_MAX )
  1969. tFilter.m_iMinValue = LLONG_MAX;
  1970. if ( (uint64_t)tFilter.m_iMaxValue > (uint64_t)LLONG_MAX )
  1971. tFilter.m_iMaxValue = LLONG_MAX;
  1972. }
  1973. tFilter.m_bExclude = !!tReq.GetDword ();
  1974. if ( iMasterVer>=15 )
  1975. {
  1976. tFilter.m_bHasEqualMin = !!tReq.GetDword();
  1977. tFilter.m_bHasEqualMax = !!tReq.GetDword();
  1978. } else if ( iMasterVer>=5 )
  1979. tFilter.m_bHasEqualMin = tFilter.m_bHasEqualMax = !!tReq.GetDword();
  1980. if ( iMasterVer>=15 )
  1981. {
  1982. tFilter.m_bOpenLeft = !!tReq.GetDword();
  1983. tFilter.m_bOpenRight = !!tReq.GetDword();
  1984. }
  1985. tFilter.m_eMvaFunc = SPH_MVAFUNC_ANY;
  1986. if ( iMasterVer>=13 )
  1987. tFilter.m_eMvaFunc = (ESphMvaFunc)tReq.GetDword();
  1988. return true;
  1989. }
  1990. static void AddDocids ( CSphVector<CSphQueryItem> & dItems )
  1991. {
  1992. if ( !dItems.GetLength() )
  1993. return;
  1994. bool bHaveDocID = false;
  1995. for ( const auto & i : dItems )
  1996. bHaveDocID |= i.m_sAlias==sphGetDocidName() || i.m_sExpr=="*";
  1997. if ( !bHaveDocID )
  1998. {
  1999. CSphQueryItem tId;
  2000. tId.m_sExpr = tId.m_sAlias = sphGetDocidName();
  2001. dItems.Insert ( 0, tId );
  2002. }
  2003. }
  2004. bool ParseSearchQuery ( InputBuffer_c & tReq, ISphOutputBuffer & tOut, CSphQuery & tQuery, WORD uVer, WORD uMasterVer )
  2005. {
  2006. // daemon-level defaults
  2007. tQuery.m_iRetryCount = -1;
  2008. tQuery.m_iRetryDelay = -1;
  2009. tQuery.m_iAgentQueryTimeoutMs = g_iAgentQueryTimeoutMs;
  2010. // v.1.27+ flags come first
  2011. DWORD uFlags = 0;
  2012. if ( uVer>=0x11B )
  2013. uFlags = tReq.GetDword();
  2014. // v.1.0. mode, limits, weights, ID/TS ranges
  2015. tQuery.m_iOffset = tReq.GetInt ();
  2016. tQuery.m_iLimit = tReq.GetInt ();
  2017. tQuery.m_eMode = (ESphMatchMode) tReq.GetInt ();
  2018. tQuery.m_eRanker = (ESphRankMode) tReq.GetInt ();
  2019. if ( tQuery.m_eRanker==SPH_RANK_EXPR || tQuery.m_eRanker==SPH_RANK_EXPORT )
  2020. tQuery.m_sRankerExpr = tReq.GetString();
  2021. tQuery.m_eSort = (ESphSortOrder) tReq.GetInt ();
  2022. tQuery.m_sSortBy = tReq.GetString ();
  2023. sphColumnToLowercase ( const_cast<char *>( tQuery.m_sSortBy.cstr() ) );
  2024. tQuery.m_sRawQuery = tReq.GetString ();
  2025. {
  2026. int iGot = 0;
  2027. if ( !tReq.GetDwords ( tQuery.m_dWeights, iGot, SPH_MAX_FIELDS ) )
  2028. {
  2029. SendErrorReply ( tOut, "invalid weight count %d (should be in 0..%d range)", iGot, SPH_MAX_FIELDS );
  2030. return false;
  2031. }
  2032. }
  2033. tQuery.m_sIndexes = tReq.GetString ();
  2034. // legacy id range filter
  2035. bool bIdrange64 = tReq.GetInt()!=0;
  2036. DocID_t tMinDocID = bIdrange64 ? (DocID_t)tReq.GetUint64 () : tReq.GetDword ();
  2037. DocID_t tMaxDocID = bIdrange64 ? (DocID_t)tReq.GetUint64 () : tReq.GetDword ();
  2038. if ( tMaxDocID==0 || (uint64_t)tMaxDocID==UINT64_MAX )
  2039. tMaxDocID = INT64_MAX;
  2040. int iAttrFilters = tReq.GetInt ();
  2041. if ( iAttrFilters>g_iMaxFilters )
  2042. {
  2043. SendErrorReply ( tOut, "too many attribute filters (req=%d, max=%d)", iAttrFilters, g_iMaxFilters );
  2044. return false;
  2045. }
  2046. tQuery.m_dFilters.Resize ( iAttrFilters );
  2047. for ( auto & i : tQuery.m_dFilters )
  2048. if ( !ParseSearchFilter ( i, tReq, tOut, uMasterVer ) )
  2049. return false;
  2050. // now add id range filter
  2051. if ( tMinDocID!=0 || tMaxDocID!=INT64_MAX )
  2052. {
  2053. CSphFilterSettings & tFilter = tQuery.m_dFilters.Add();
  2054. tFilter.m_sAttrName = sphGetDocidName();
  2055. tFilter.m_eType = SPH_FILTER_RANGE;
  2056. tFilter.m_iMinValue = tMinDocID;
  2057. tFilter.m_iMaxValue = tMaxDocID;
  2058. }
  2059. tQuery.m_eGroupFunc = (ESphGroupBy) tReq.GetDword ();
  2060. tQuery.m_sGroupBy = tReq.GetString ();
  2061. sphColumnToLowercase ( const_cast<char *>( tQuery.m_sGroupBy.cstr() ) );
  2062. tQuery.m_iMaxMatches = tReq.GetInt ();
  2063. tQuery.m_bExplicitMaxMatches = tQuery.m_iMaxMatches!=DEFAULT_MAX_MATCHES; // fixme?
  2064. tQuery.m_sGroupSortBy = tReq.GetString ();
  2065. tQuery.m_iCutoff = tReq.GetInt();
  2066. tQuery.m_iRetryCount = tReq.GetInt ();
  2067. tQuery.m_iRetryDelay = tReq.GetInt ();
  2068. tQuery.m_sGroupDistinct = tReq.GetString ();
  2069. sphColumnToLowercase ( const_cast<char *>( tQuery.m_sGroupDistinct.cstr() ) );
  2070. tQuery.m_bGeoAnchor = ( tReq.GetInt()!=0 );
  2071. if ( tQuery.m_bGeoAnchor )
  2072. {
  2073. tQuery.m_sGeoLatAttr = tReq.GetString ();
  2074. tQuery.m_sGeoLongAttr = tReq.GetString ();
  2075. tQuery.m_fGeoLatitude = tReq.GetFloat ();
  2076. tQuery.m_fGeoLongitude = tReq.GetFloat ();
  2077. }
  2078. tQuery.m_dIndexWeights.Resize ( tReq.GetInt() ); // FIXME! add sanity check
  2079. for ( auto& dIndexWeight : tQuery.m_dIndexWeights )
  2080. tReq >> dIndexWeight;
  2081. tQuery.m_uMaxQueryMsec = tReq.GetDword ();
  2082. tQuery.m_dFieldWeights.Resize ( tReq.GetInt() ); // FIXME! add sanity check
  2083. for ( auto & dFieldWeight : tQuery.m_dFieldWeights )
  2084. tReq >> dFieldWeight;
  2085. tQuery.m_sComment = tReq.GetString ();
  2086. int nOverrides = tReq.GetInt();
  2087. if ( nOverrides>0 )
  2088. {
  2089. SendErrorReply ( tOut, "overrides are now deprecated" );
  2090. return false;
  2091. }
  2092. tQuery.m_sSelect = tReq.GetString ();
  2093. tQuery.m_bAgent = ( uMasterVer>0 );
  2094. if ( tQuery.m_sSelect.Begins ( "*,*" ) ) // this is the legacy mark of agent for debug purpose
  2095. {
  2096. tQuery.m_bAgent = true;
  2097. int iSelectLen = tQuery.m_sSelect.Length();
  2098. tQuery.m_sSelect = ( iSelectLen>4 ? tQuery.m_sSelect.SubString ( 4, iSelectLen-4 ) : "*" );
  2099. }
  2100. // fixup select list
  2101. if ( tQuery.m_sSelect.IsEmpty () )
  2102. tQuery.m_sSelect = "*";
  2103. // master sends items to agents since master.version=15
  2104. CSphString sError;
  2105. if ( uMasterVer<15 && !ParseSelectList ( sError, tQuery ) )
  2106. {
  2107. // we want to see a parse error in query_log_format=sphinxql mode too
  2108. if ( g_eLogFormat==LOG_FORMAT_SPHINXQL && g_iQueryLogFile>=0 )
  2109. {
  2110. StringBuilder_c tBuf;
  2111. tBuf << "/* ";
  2112. sphFormatCurrentTime ( tBuf );
  2113. tBuf << "*/ " << tQuery.m_sSelect << " # error=" << sError << '\n';
  2114. sphSeek ( g_iQueryLogFile, 0, SEEK_END );
  2115. sphWrite ( g_iQueryLogFile, tBuf.cstr(), tBuf.GetLength() );
  2116. }
  2117. SendErrorReply ( tOut, "select: %s", sError.cstr () );
  2118. return false;
  2119. }
  2120. // v.1.27
  2121. if ( uVer>=0x11B )
  2122. {
  2123. // parse simple flags
  2124. tQuery.m_bSortKbuffer = !!( uFlags & QFLAG_SORT_KBUFFER );
  2125. tQuery.m_bSimplify = !!( uFlags & QFLAG_SIMPLIFY );
  2126. tQuery.m_bPlainIDF = !!( uFlags & QFLAG_PLAIN_IDF );
  2127. tQuery.m_bGlobalIDF = !!( uFlags & QFLAG_GLOBAL_IDF );
  2128. tQuery.m_bLocalDF = !!( uFlags & QFLAG_LOCAL_DF );
  2129. tQuery.m_bLowPriority = !!( uFlags & QFLAG_LOW_PRIORITY );
  2130. tQuery.m_bFacet = !!( uFlags & QFLAG_FACET );
  2131. tQuery.m_bFacetHead = !!( uFlags & QFLAG_FACET_HEAD );
  2132. tQuery.m_eQueryType = (uFlags & QFLAG_JSON_QUERY) ? QUERY_JSON : QUERY_API;
  2133. tQuery.m_bNotOnlyAllowed = !!( uFlags & QFLAG_NOT_ONLY_ALLOWED );
  2134. if ( uMasterVer>0 || uVer==0x11E )
  2135. tQuery.m_bNormalizedTFIDF = !!( uFlags & QFLAG_NORMALIZED_TF );
  2136. // fetch optional stuff
  2137. if ( uFlags & QFLAG_MAX_PREDICTED_TIME )
  2138. tQuery.m_iMaxPredictedMsec = tReq.GetInt();
  2139. }
  2140. // v.1.29
  2141. if ( uVer>=0x11D )
  2142. {
  2143. tQuery.m_sOuterOrderBy = tReq.GetString();
  2144. tQuery.m_iOuterOffset = tReq.GetDword();
  2145. tQuery.m_iOuterLimit = tReq.GetDword();
  2146. tQuery.m_bHasOuter = ( tReq.GetInt()!=0 );
  2147. }
  2148. // extension v.1
  2149. tQuery.m_eCollation = GlobalCollation ();
  2150. if ( uMasterVer>=1 )
  2151. tQuery.m_eCollation = (ESphCollation)tReq.GetDword();
  2152. // extension v.2
  2153. if ( uMasterVer>=2 )
  2154. {
  2155. tQuery.m_sOuterOrderBy = tReq.GetString();
  2156. if ( tQuery.m_bHasOuter )
  2157. tQuery.m_iOuterLimit = tReq.GetInt();
  2158. }
  2159. if ( uMasterVer>=6 )
  2160. tQuery.m_iGroupbyLimit = tReq.GetInt();
  2161. if ( uMasterVer>=14 )
  2162. {
  2163. tQuery.m_sUDRanker = tReq.GetString();
  2164. tQuery.m_sUDRankerOpts = tReq.GetString();
  2165. }
  2166. if ( uMasterVer>=14 || uVer>=0x120 )
  2167. {
  2168. tQuery.m_sQueryTokenFilterLib = tReq.GetString();
  2169. tQuery.m_sQueryTokenFilterName = tReq.GetString();
  2170. tQuery.m_sQueryTokenFilterOpts = tReq.GetString();
  2171. }
  2172. if ( uVer>=0x121 )
  2173. {
  2174. tQuery.m_dFilterTree.Resize ( tReq.GetInt() );
  2175. for ( FilterTreeItem_t &tItem : tQuery.m_dFilterTree )
  2176. {
  2177. tItem.m_iLeft = tReq.GetInt();
  2178. tItem.m_iRight = tReq.GetInt();
  2179. tItem.m_iFilterItem = tReq.GetInt();
  2180. tItem.m_bOr = ( tReq.GetInt()!=0 );
  2181. }
  2182. }
  2183. if ( uMasterVer>=15 )
  2184. {
  2185. tQuery.m_dItems.Resize ( tReq.GetInt() );
  2186. for ( CSphQueryItem &tItem : tQuery.m_dItems )
  2187. {
  2188. tItem.m_sAlias = tReq.GetString();
  2189. tItem.m_sExpr = tReq.GetString();
  2190. tItem.m_eAggrFunc = (ESphAggrFunc)tReq.GetDword();
  2191. }
  2192. tQuery.m_dRefItems.Resize ( tReq.GetInt() );
  2193. for ( CSphQueryItem &tItem : tQuery.m_dRefItems )
  2194. {
  2195. tItem.m_sAlias = tReq.GetString();
  2196. tItem.m_sExpr = tReq.GetString();
  2197. tItem.m_eAggrFunc = (ESphAggrFunc)tReq.GetDword();
  2198. }
  2199. }
  2200. if ( uMasterVer>=16 )
  2201. tQuery.m_eExpandKeywords = (QueryOption_e)tReq.GetDword();
  2202. if ( uMasterVer>=17 )
  2203. {
  2204. tQuery.m_dIndexHints.Resize ( tReq.GetDword() );
  2205. for ( auto & i : tQuery.m_dIndexHints )
  2206. {
  2207. i.m_dHints[int(SecondaryIndexType_e::INDEX)] = (IndexHint_e)tReq.GetDword();
  2208. i.m_sIndex = tReq.GetString();
  2209. }
  2210. }
  2211. /////////////////////
  2212. // additional checks
  2213. /////////////////////
  2214. // queries coming from API may not request docids
  2215. // but we still need docids when sending result sets
  2216. AddDocids ( tQuery.m_dItems );
  2217. AddDocids ( tQuery.m_dRefItems );
  2218. if ( tReq.GetError() )
  2219. {
  2220. SendErrorReply ( tOut, "invalid or truncated request" );
  2221. return false;
  2222. }
  2223. CheckQuery ( tQuery, sError );
  2224. if ( !sError.IsEmpty() )
  2225. {
  2226. SendErrorReply ( tOut, "%s", sError.cstr() );
  2227. return false;
  2228. }
  2229. // now prepare it for the engine
  2230. tQuery.m_sQuery = tQuery.m_sRawQuery;
  2231. if ( tQuery.m_eQueryType!=QUERY_JSON )
  2232. PrepareQueryEmulation ( &tQuery );
  2233. FixupQuerySettings ( tQuery );
  2234. // all ok
  2235. return true;
  2236. }
  2237. //////////////////////////////////////////////////////////////////////////
  2238. struct EscapeQuotator_t
  2239. {
  2240. static constexpr BYTE EscapingSpace ( BYTE c )
  2241. {
  2242. return ( c == '\\' || c == '\'' ) ? 1 : 0;
  2243. }
  2244. };
  2245. using QuotationEscapedBuilder = EscapedStringBuilder_T<BaseQuotation_T<EscapeQuotator_t>>;
  2246. void LogQueryPlain ( const CSphQuery & tQuery, const CSphQueryResultMeta & tMeta )
  2247. {
  2248. assert ( g_eLogFormat==LOG_FORMAT_PLAIN );
  2249. if ( ( !g_bQuerySyslog && g_iQueryLogFile<0 ) || !tMeta.m_sError.IsEmpty() )
  2250. return;
  2251. QuotationEscapedBuilder tBuf;
  2252. // [time]
  2253. #if USE_SYSLOG
  2254. if ( !g_bQuerySyslog )
  2255. {
  2256. #endif
  2257. tBuf << '[';
  2258. sphFormatCurrentTime ( tBuf );
  2259. tBuf << ']';
  2260. #if USE_SYSLOG
  2261. } else
  2262. tBuf += "[query]";
  2263. #endif
  2264. // querytime sec
  2265. int iQueryTime = Max ( tMeta.m_iQueryTime, 0 );
  2266. int iRealTime = Max ( tMeta.m_iRealQueryTime, 0 );
  2267. tBuf.Appendf ( " %d.%03d sec", iRealTime/1000, iRealTime%1000 );
  2268. tBuf.Appendf ( " %d.%03d sec", iQueryTime/1000, iQueryTime%1000 );
  2269. // optional multi-query multiplier
  2270. if ( tMeta.m_iMultiplier>1 )
  2271. tBuf.Appendf ( " x%d", tMeta.m_iMultiplier );
  2272. // [matchmode/numfilters/sortmode matches (offset,limit)
  2273. static const char * sModes [ SPH_MATCH_TOTAL ] = { "all", "any", "phr", "bool", "ext", "scan", "ext2" };
  2274. static const char * sSort [ SPH_SORT_TOTAL ] = { "rel", "attr-", "attr+", "tsegs", "ext", "expr" };
  2275. tBuf.Appendf ( " [%s/%d/%s " INT64_FMT " (%d,%d)",
  2276. sModes [ tQuery.m_eMode ], tQuery.m_dFilters.GetLength(), sSort [ tQuery.m_eSort ], tMeta.m_iTotalMatches,
  2277. tQuery.m_iOffset, tQuery.m_iLimit );
  2278. // optional groupby info
  2279. if ( !tQuery.m_sGroupBy.IsEmpty() )
  2280. tBuf.Appendf ( " @%s", tQuery.m_sGroupBy.cstr() );
  2281. // ] [indexes]
  2282. tBuf.Appendf ( "] [%s]", tQuery.m_sIndexes.cstr() );
  2283. // optional performance counters
  2284. if ( g_bIOStats || g_bCpuStats )
  2285. {
  2286. const CSphIOStats & IOStats = tMeta.m_tIOStats;
  2287. tBuf += " [";
  2288. if ( g_bIOStats )
  2289. tBuf.Appendf ( "ios=%d kb=%d.%d ioms=%d.%d",
  2290. IOStats.m_iReadOps, (int)( IOStats.m_iReadBytes/1024 ), (int)( IOStats.m_iReadBytes%1024 )*10/1024,
  2291. (int)( IOStats.m_iReadTime/1000 ), (int)( IOStats.m_iReadTime%1000 )/100 );
  2292. if ( g_bIOStats && g_bCpuStats )
  2293. tBuf += " ";
  2294. if ( g_bCpuStats )
  2295. tBuf.Sprintf ( "cpums=%.1D", tMeta.m_iCpuTime/100 );
  2296. tBuf += "]";
  2297. }
  2298. // optional query comment
  2299. if ( !tQuery.m_sComment.IsEmpty() )
  2300. tBuf.Appendf ( " [%s]", tQuery.m_sComment.cstr() );
  2301. // query
  2302. // (m_sRawQuery is empty when using MySQL handler)
  2303. const CSphString & sQuery = tQuery.m_sRawQuery.IsEmpty()
  2304. ? tQuery.m_sQuery
  2305. : tQuery.m_sRawQuery;
  2306. if ( !sQuery.IsEmpty() )
  2307. {
  2308. tBuf += " ";
  2309. tBuf.FixupSpacesAndAppend ( sQuery.cstr() );
  2310. }
  2311. #if USE_SYSLOG
  2312. if ( !g_bQuerySyslog )
  2313. {
  2314. #endif
  2315. // line feed
  2316. tBuf += "\n";
  2317. sphSeek ( g_iQueryLogFile, 0, SEEK_END );
  2318. sphWrite ( g_iQueryLogFile, tBuf.cstr(), tBuf.GetLength() );
  2319. #if USE_SYSLOG
  2320. } else
  2321. {
  2322. syslog ( LOG_INFO, "%s", tBuf.cstr() );
  2323. }
  2324. #endif
  2325. }
  2326. namespace {
  2327. CSphString RemoveBackQuotes ( const char * pSrc )
  2328. {
  2329. CSphString sResult;
  2330. if ( !pSrc )
  2331. return sResult;
  2332. size_t iLen = strlen ( pSrc );
  2333. if ( !iLen )
  2334. return sResult;
  2335. auto szResult = new char[iLen+1];
  2336. auto * sMax = pSrc+iLen;
  2337. auto d = szResult;
  2338. while ( pSrc<sMax )
  2339. {
  2340. auto sQuote = (const char *) memchr ( pSrc, '`', sMax-pSrc );
  2341. if ( !sQuote )
  2342. sQuote = sMax;
  2343. auto iChunk = sQuote-pSrc;
  2344. memmove ( d, pSrc, iChunk );
  2345. d += iChunk;
  2346. pSrc += iChunk+1; // +1 to skip the quote
  2347. }
  2348. *d = '\0';
  2349. if ( !*szResult ) // never return allocated, but empty str. Prefer to return nullptr instead.
  2350. SafeDeleteArray( szResult );
  2351. sResult.Adopt ( &szResult );
  2352. return sResult;
  2353. }
  2354. }
  2355. static void FormatOrderBy ( StringBuilder_c * pBuf, const char * sPrefix, ESphSortOrder eSort, const CSphString & sSort )
  2356. {
  2357. assert ( pBuf );
  2358. if ( eSort==SPH_SORT_EXTENDED && sSort=="@weight desc" )
  2359. return;
  2360. const char * sSubst = "@weight";
  2361. if ( sSort!="@relevance" )
  2362. sSubst = sSort.cstr();
  2363. auto sUnquoted = RemoveBackQuotes ( sSubst );
  2364. sSubst = sUnquoted.cstr();
  2365. // for simplicity check that sPrefix is already prefixed/suffixed by spaces.
  2366. assert ( sPrefix && sPrefix[0]==' ' && sPrefix[strlen ( sPrefix )-1]==' ' );
  2367. *pBuf << sPrefix;
  2368. switch ( eSort )
  2369. {
  2370. case SPH_SORT_ATTR_DESC: *pBuf << sSubst << " DESC"; break;
  2371. case SPH_SORT_ATTR_ASC: *pBuf << sSubst << " ASC"; break;
  2372. case SPH_SORT_TIME_SEGMENTS: *pBuf << "TIME_SEGMENT(" << sSubst << ")"; break;
  2373. case SPH_SORT_EXTENDED: *pBuf << sSubst; break;
  2374. case SPH_SORT_EXPR: *pBuf << "BUILTIN_EXPR()"; break;
  2375. case SPH_SORT_RELEVANCE: *pBuf << "weight() desc"; if ( sSubst ) *pBuf << ", " << sSubst; break;
  2376. default: pBuf->Appendf ( "mode-%d", (int)eSort ); break;
  2377. }
  2378. }
  2379. static const CSphQuery g_tDefaultQuery {};
  2380. static void FormatSphinxql ( const CSphQuery & q, int iCompactIN, QuotationEscapedBuilder & tBuf );
  2381. static void FormatList ( const CSphVector<CSphNamedInt> & dValues, StringBuilder_c & tBuf )
  2382. {
  2383. ScopedComma_c tComma ( tBuf, ", " );
  2384. for ( const auto& dValue : dValues )
  2385. tBuf << dValue;
  2386. }
  2387. static void FormatOption ( const CSphQuery & tQuery, StringBuilder_c & tBuf )
  2388. {
  2389. ScopedComma_c tOptionComma ( tBuf, ", ", " OPTION ");
  2390. if ( tQuery.m_iMaxMatches!=DEFAULT_MAX_MATCHES )
  2391. tBuf.Appendf ( "max_matches=%d", tQuery.m_iMaxMatches );
  2392. if ( !tQuery.m_sComment.IsEmpty() )
  2393. tBuf.Appendf ( "comment='%s'", tQuery.m_sComment.cstr() ); // FIXME! escape, replace newlines..
  2394. if ( tQuery.m_eRanker!=SPH_RANK_DEFAULT )
  2395. {
  2396. const char * sRanker = sphGetRankerName ( tQuery.m_eRanker );
  2397. if ( !sRanker )
  2398. sRanker = sphGetRankerName ( SPH_RANK_DEFAULT );
  2399. if ( tQuery.m_sRankerExpr.IsEmpty() )
  2400. tBuf.Appendf ( "ranker=%s", sRanker );
  2401. else
  2402. tBuf.Appendf ( "ranker=%s(\'%s\')", sRanker, tQuery.m_sRankerExpr.scstr() );
  2403. }
  2404. if ( tQuery.m_iAgentQueryTimeoutMs!=g_iAgentQueryTimeoutMs )
  2405. tBuf.Appendf ( "agent_query_timeout=%d", tQuery.m_iAgentQueryTimeoutMs );
  2406. if ( tQuery.m_iCutoff!=g_tDefaultQuery.m_iCutoff )
  2407. tBuf.Appendf ( "cutoff=%d", tQuery.m_iCutoff );
  2408. if ( tQuery.m_dFieldWeights.GetLength() )
  2409. {
  2410. tBuf.StartBlock (nullptr,"field_weights=(",")");
  2411. FormatList ( tQuery.m_dFieldWeights, tBuf );
  2412. tBuf.FinishBlock ();
  2413. }
  2414. if ( tQuery.m_bGlobalIDF!=g_tDefaultQuery.m_bGlobalIDF )
  2415. tBuf << "global_idf=1";
  2416. if ( tQuery.m_bPlainIDF || !tQuery.m_bNormalizedTFIDF )
  2417. {
  2418. tBuf.StartBlock(",","idf='","'");
  2419. tBuf << ( tQuery.m_bPlainIDF ? "plain" : "normalized" )
  2420. << ( tQuery.m_bNormalizedTFIDF ? "tfidf_normalized" : "tfidf_unnormalized" );
  2421. tBuf.FinishBlock ();
  2422. }
  2423. if ( tQuery.m_bLocalDF!=g_tDefaultQuery.m_bLocalDF )
  2424. tBuf << "local_df=1";
  2425. if ( tQuery.m_dIndexWeights.GetLength() )
  2426. {
  2427. tBuf.StartBlock ( nullptr, "index_weights=(", ")" );
  2428. FormatList ( tQuery.m_dIndexWeights, tBuf );
  2429. tBuf.FinishBlock ();
  2430. }
  2431. if ( tQuery.m_uMaxQueryMsec!=g_tDefaultQuery.m_uMaxQueryMsec )
  2432. tBuf.Appendf ( "max_query_time=%u", tQuery.m_uMaxQueryMsec );
  2433. if ( tQuery.m_iMaxPredictedMsec!=g_tDefaultQuery.m_iMaxPredictedMsec )
  2434. tBuf.Appendf ( "max_predicted_time=%d", tQuery.m_iMaxPredictedMsec );
  2435. if ( tQuery.m_iRetryCount!=-1 )
  2436. tBuf.Appendf ( "retry_count=%d", tQuery.m_iRetryCount );
  2437. if ( tQuery.m_iRetryDelay!=-1 )
  2438. tBuf.Appendf ( "retry_delay=%d", tQuery.m_iRetryDelay );
  2439. if ( tQuery.m_iRandSeed!=g_tDefaultQuery.m_iRandSeed )
  2440. tBuf.Appendf ( "rand_seed=" INT64_FMT, tQuery.m_iRandSeed );
  2441. if ( !tQuery.m_sQueryTokenFilterLib.IsEmpty() )
  2442. {
  2443. if ( tQuery.m_sQueryTokenFilterOpts.IsEmpty() )
  2444. tBuf.Appendf ( "token_filter = '%s:%s'", tQuery.m_sQueryTokenFilterLib.cstr(), tQuery.m_sQueryTokenFilterName.cstr() );
  2445. else
  2446. tBuf.Appendf ( "token_filter = '%s:%s:%s'", tQuery.m_sQueryTokenFilterLib.cstr(), tQuery.m_sQueryTokenFilterName.cstr(), tQuery.m_sQueryTokenFilterOpts.cstr() );
  2447. }
  2448. if ( tQuery.m_bIgnoreNonexistent )
  2449. tBuf << "ignore_nonexistent_columns=1";
  2450. if ( tQuery.m_bIgnoreNonexistentIndexes )
  2451. tBuf << "ignore_nonexistent_indexes=1";
  2452. if ( tQuery.m_bStrict )
  2453. tBuf << "strict=1";
  2454. if ( tQuery.m_eExpandKeywords!=QUERY_OPT_DEFAULT && tQuery.m_eExpandKeywords!=QUERY_OPT_MORPH_NONE )
  2455. tBuf.Appendf ( "expand_keywords=%d", ( tQuery.m_eExpandKeywords==QUERY_OPT_ENABLED ? 1 : 0 ) );
  2456. if ( tQuery.m_eExpandKeywords==QUERY_OPT_MORPH_NONE )
  2457. tBuf.Appendf ( "morphology=none" );
  2458. }
  2459. static void AppendHint ( const char * szHint, const StrVec_t & dIndexes, StringBuilder_c & tBuf )
  2460. {
  2461. if ( dIndexes.IsEmpty() )
  2462. return;
  2463. tBuf << " " << szHint;
  2464. ScopedComma_c tComma ( tBuf, ",", " INDEX (", ")" );
  2465. for ( const auto & sIndex : dIndexes )
  2466. tBuf << sIndex;
  2467. }
  2468. static void FormatIndexHints ( const CSphQuery & tQuery, StringBuilder_c & tBuf )
  2469. {
  2470. ScopedComma_c sMatch ( tBuf, nullptr );
  2471. StrVec_t dUse, dForce, dIgnore;
  2472. for ( const auto & i : tQuery.m_dIndexHints )
  2473. {
  2474. switch ( i.m_dHints[int(SecondaryIndexType_e::INDEX)] )
  2475. {
  2476. case IndexHint_e::USE:
  2477. dUse.Add(i.m_sIndex);
  2478. break;
  2479. case IndexHint_e::FORCE:
  2480. dForce.Add(i.m_sIndex);
  2481. break;
  2482. case IndexHint_e::IGNORE_:
  2483. dIgnore.Add(i.m_sIndex);
  2484. break;
  2485. default:
  2486. break;
  2487. }
  2488. }
  2489. AppendHint ( "USE", dUse, tBuf );
  2490. AppendHint ( "FORCE", dForce, tBuf );
  2491. AppendHint ( "IGNORE", dIgnore, tBuf );
  2492. }
  2493. static void LogQueryJson ( const CSphQuery & q, StringBuilder_c & tBuf )
  2494. {
  2495. tBuf << " /*" << q.m_sRawQuery << " */";
  2496. }
  2497. static void LogQuerySphinxql ( const CSphQuery & q, const CSphQueryResultMeta & tMeta, const CSphVector<int64_t> & dAgentTimes )
  2498. {
  2499. assert ( g_eLogFormat==LOG_FORMAT_SPHINXQL );
  2500. if ( g_iQueryLogFile<0 )
  2501. return;
  2502. QuotationEscapedBuilder tBuf;
  2503. int iCompactIN = ( g_bLogCompactIn ? LOG_COMPACT_IN : 0 );
  2504. // time, conn id, wall, found
  2505. int iQueryTime = Max ( tMeta.m_iQueryTime, 0 );
  2506. int iRealTime = Max ( tMeta.m_iRealQueryTime, 0 );
  2507. tBuf << "/* ";
  2508. sphFormatCurrentTime ( tBuf );
  2509. if ( tMeta.m_iMultiplier>1 )
  2510. tBuf << " conn " << session::GetConnID() << " real " << FixedFrac ( iRealTime )
  2511. << " wall " << FixedFrac ( iQueryTime ) << " x" << tMeta.m_iMultiplier << " found " << tMeta.m_iTotalMatches << " */ ";
  2512. else
  2513. tBuf << " conn " << session::GetConnID() << " real " << FixedFrac ( iRealTime )
  2514. << " wall " << FixedFrac ( iQueryTime ) << " found " << tMeta.m_iTotalMatches << " */ ";
  2515. ///////////////////////////////////
  2516. // format request as SELECT query
  2517. ///////////////////////////////////
  2518. if ( q.m_eQueryType==QUERY_JSON )
  2519. LogQueryJson ( q, tBuf );
  2520. else
  2521. FormatSphinxql ( q, iCompactIN, tBuf );
  2522. ///////////////
  2523. // query stats
  2524. ///////////////
  2525. // next block ecnlosed in /* .. */, space-separated
  2526. tBuf.StartBlock ( " ", " /*", " */" );
  2527. if ( !tMeta.m_sError.IsEmpty() )
  2528. {
  2529. // all we have is an error
  2530. tBuf.Appendf ( "error=%s", tMeta.m_sError.cstr() );
  2531. } else if ( g_bIOStats || g_bCpuStats || dAgentTimes.GetLength() || !tMeta.m_sWarning.IsEmpty() )
  2532. {
  2533. // performance counters
  2534. if ( g_bIOStats || g_bCpuStats )
  2535. {
  2536. const CSphIOStats & IOStats = tMeta.m_tIOStats;
  2537. if ( g_bIOStats )
  2538. tBuf.Sprintf ( "ios=%d kb=%d.%d ioms=%.1D",
  2539. IOStats.m_iReadOps, (int)( IOStats.m_iReadBytes/1024 ), (int)( IOStats.m_iReadBytes%1024 )*10/1024,
  2540. IOStats.m_iReadTime/100 );
  2541. if ( g_bCpuStats )
  2542. tBuf.Sprintf ( "cpums=%.1D", tMeta.m_iCpuTime/100 );
  2543. }
  2544. // per-agent times
  2545. if ( dAgentTimes.GetLength() )
  2546. {
  2547. ScopedComma_c dAgents ( tBuf, ", ", " agents=(",")");
  2548. for ( auto iTime : dAgentTimes )
  2549. tBuf.Appendf ( "%d.%03d",
  2550. (int)( iTime/1000),
  2551. (int)( iTime%1000) );
  2552. }
  2553. // warning
  2554. if ( !tMeta.m_sWarning.IsEmpty() )
  2555. tBuf.Appendf ( "warning=%s", tMeta.m_sWarning.cstr() );
  2556. }
  2557. tBuf.FinishBlock (); // close the comment
  2558. // line feed
  2559. tBuf += "\n";
  2560. sphSeek ( g_iQueryLogFile, 0, SEEK_END );
  2561. sphWrite ( g_iQueryLogFile, tBuf.cstr(), tBuf.GetLength() );
  2562. }
  2563. void FormatSphinxql ( const CSphQuery & q, int iCompactIN, QuotationEscapedBuilder & tBuf )
  2564. {
  2565. if ( q.m_bHasOuter )
  2566. tBuf << "SELECT * FROM (";
  2567. if ( q.m_sSelect.IsEmpty() )
  2568. tBuf << "SELECT * FROM " << q.m_sIndexes;
  2569. else
  2570. tBuf << "SELECT " << RemoveBackQuotes ( q.m_sSelect.cstr() ) << " FROM " << q.m_sIndexes;
  2571. // WHERE clause
  2572. // (m_sRawQuery is empty when using MySQL handler)
  2573. const CSphString & sQuery = q.m_sQuery;
  2574. if ( !sQuery.IsEmpty() || q.m_dFilters.GetLength() )
  2575. {
  2576. ScopedComma_c sWHERE ( tBuf, " AND ", " WHERE ");
  2577. if ( !sQuery.IsEmpty() )
  2578. {
  2579. ScopedComma_c sMatch (tBuf, nullptr, "MATCH(", ")");
  2580. tBuf.FixupSpacedAndAppendEscaped ( sQuery.cstr() );
  2581. }
  2582. FormatFiltersQL ( q.m_dFilters, q.m_dFilterTree, tBuf, iCompactIN );
  2583. }
  2584. // ORDER BY and/or GROUP BY clause
  2585. if ( q.m_sGroupBy.IsEmpty() )
  2586. {
  2587. if ( !q.m_sSortBy.IsEmpty() ) // case API SPH_MATCH_EXTENDED2 - SPH_SORT_RELEVANCE
  2588. FormatOrderBy ( &tBuf, " ORDER BY ", q.m_eSort, q.m_sSortBy );
  2589. } else
  2590. {
  2591. tBuf << " GROUP BY " << q.m_sGroupBy;
  2592. FormatOrderBy ( &tBuf, " WITHIN GROUP ORDER BY ", q.m_eSort, q.m_sSortBy );
  2593. if ( !q.m_tHaving.m_sAttrName.IsEmpty() )
  2594. {
  2595. ScopedComma_c sHawing ( tBuf, nullptr," HAVING ");
  2596. FormatFilterQL ( q.m_tHaving, tBuf, iCompactIN );
  2597. }
  2598. if ( q.m_sGroupSortBy!="@group desc" )
  2599. FormatOrderBy ( &tBuf, " ORDER BY ", SPH_SORT_EXTENDED, q.m_sGroupSortBy );
  2600. }
  2601. // LIMIT clause
  2602. if ( q.m_iOffset!=0 || q.m_iLimit!=20 )
  2603. tBuf << " LIMIT " << q.m_iOffset << ',' << q.m_iLimit;
  2604. // OPTION clause
  2605. FormatOption ( q, tBuf );
  2606. FormatIndexHints ( q, tBuf );
  2607. // outer order by, limit
  2608. if ( q.m_bHasOuter )
  2609. {
  2610. tBuf << ')';
  2611. if ( !q.m_sOuterOrderBy.IsEmpty() )
  2612. tBuf << " ORDER BY " << q.m_sOuterOrderBy;
  2613. if ( q.m_iOuterOffset>0 )
  2614. tBuf << " LIMIT " << q.m_iOuterOffset << ", " << q.m_iOuterLimit;
  2615. else if ( q.m_iOuterLimit>0 )
  2616. tBuf << " LIMIT " << q.m_iOuterLimit;
  2617. }
  2618. // finish SQL statement
  2619. tBuf << ';';
  2620. }
  2621. static void LogQuery ( const CSphQuery & q, const CSphQueryResultMeta & tMeta, const CSphVector<int64_t> & dAgentTimes )
  2622. {
  2623. if ( g_iQueryLogMinMs>0 && tMeta.m_iQueryTime<g_iQueryLogMinMs )
  2624. return;
  2625. switch ( g_eLogFormat )
  2626. {
  2627. case LOG_FORMAT_PLAIN: LogQueryPlain ( q, tMeta ); break;
  2628. case LOG_FORMAT_SPHINXQL: LogQuerySphinxql ( q, tMeta, dAgentTimes ); break;
  2629. }
  2630. }
  2631. void LogSphinxqlError ( const char * sStmt, const Str_t& sError )
  2632. {
  2633. if ( g_eLogFormat!=LOG_FORMAT_SPHINXQL || g_iQueryLogFile<0 || !sStmt || IsEmpty(sError) )
  2634. return;
  2635. StringBuilder_c tBuf;
  2636. tBuf << "/* ";
  2637. sphFormatCurrentTime ( tBuf );
  2638. tBuf << " conn " << session::GetConnID() << " */ " << sStmt << " # error=" << sError << '\n';
  2639. sphSeek ( g_iQueryLogFile, 0, SEEK_END );
  2640. sphWrite ( g_iQueryLogFile, tBuf.cstr(), tBuf.GetLength() );
  2641. }
  2642. void ReportIndexesName ( int iSpanStart, int iSpandEnd, const CSphVector<SearchFailure_t> & dLog, StringBuilder_c & sOut )
  2643. {
  2644. int iSpanLen = iSpandEnd - iSpanStart;
  2645. // report distributed index in case all failures are from their locals
  2646. if ( iSpanLen>1 && !dLog[iSpanStart].m_sParentIndex.IsEmpty ()
  2647. && dLog[iSpanStart].m_sParentIndex==dLog[iSpandEnd-1].m_sParentIndex )
  2648. {
  2649. auto pDist = GetDistr ( dLog[iSpanStart].m_sParentIndex );
  2650. if ( pDist && pDist->m_dLocal.GetLength ()==iSpanLen )
  2651. {
  2652. sOut << "index " << dLog[iSpanStart].m_sParentIndex << ": ";
  2653. return;
  2654. }
  2655. }
  2656. // report only first indexes up to 4
  2657. int iEndReport = ( iSpanLen>4 ) ? iSpanStart+3 : iSpandEnd;
  2658. sOut.StartBlock ( ",", "index " );
  2659. for ( int j = iSpanStart; j<iEndReport; ++j )
  2660. sOut << dLog[j].m_sIndex;
  2661. sOut.FinishBlock ();
  2662. // add total index count
  2663. if ( iEndReport!=iSpandEnd )
  2664. sOut.Sprintf ( " and %d more: ", iSpandEnd-iEndReport );
  2665. else
  2666. sOut += ": ";
  2667. }
  2668. static void LogStatementSphinxql ( Str_t sQuery, int iRealTime )
  2669. {
  2670. if ( g_iQueryLogFile<0 || g_eLogFormat!=LOG_FORMAT_SPHINXQL || !IsFilled ( sQuery ) )
  2671. return;
  2672. StringBuilder_c tBuf;
  2673. tBuf << "/* ";
  2674. sphFormatCurrentTime ( tBuf );
  2675. tBuf << " conn " << session::GetConnID() << " real " << FixedFrac ( iRealTime ) << " */ "
  2676. // query
  2677. << sQuery
  2678. // finish statement and line feed
  2679. << ";\n";
  2680. sphSeek ( g_iQueryLogFile, 0, SEEK_END );
  2681. sphWrite ( g_iQueryLogFile, tBuf.cstr(), tBuf.GetLength() );
  2682. }
  2683. static int64_t LogFilterStatementSphinxql ( Str_t sQuery, SqlStmt_e eStmt )
  2684. {
  2685. if ( g_tLogStatements.IsEmpty() )
  2686. return 0;
  2687. if ( !g_tLogStatements.BitGet ( eStmt ) )
  2688. return 0;
  2689. int64_t tmStarted = sphMicroTimer();
  2690. LogStatementSphinxql ( sQuery, 0 );
  2691. return tmStarted;
  2692. }
  2693. //////////////////////////////////////////////////////////////////////////
  2694. void sphGetAttrsToSend ( const ISphSchema & tSchema, bool bAgentMode, bool bNeedId, CSphBitvec & tAttrs )
  2695. {
  2696. int iCount = tSchema.GetAttrsCount();
  2697. tAttrs.Init ( iCount );
  2698. if ( !bAgentMode && iCount && IsSortStringInternal ( tSchema.GetAttr ( iCount-1 ).m_sName ) )
  2699. {
  2700. for ( int i=iCount-1; i>=0 && IsSortStringInternal ( tSchema.GetAttr(i).m_sName ); --i )
  2701. iCount = i;
  2702. }
  2703. for ( int i = 0; i < iCount; ++i )
  2704. if ( !sphIsInternalAttr ( tSchema.GetAttr(i) ) )
  2705. tAttrs.BitSet(i);
  2706. int iId = tSchema.GetAttrIndex ( sphGetDocidName() );
  2707. if ( !bAgentMode && iId!=-1 && !bNeedId )
  2708. tAttrs.BitClear(iId);
  2709. }
  2710. static void SendDataPtrAttr ( ISphOutputBuffer& tOut, const BYTE * pData )
  2711. {
  2712. auto dData = sphUnpackPtrAttr ( pData );
  2713. tOut.SendArray ( dData );
  2714. }
  2715. static char g_sJsonNull[] = "{}";
  2716. static void SendJsonAsString ( ISphOutputBuffer& tOut, const BYTE * pJSON )
  2717. {
  2718. if ( pJSON )
  2719. {
  2720. auto dData = sphUnpackPtrAttr ( pJSON );
  2721. JsonEscapedBuilder dJson;
  2722. dJson.GrowEnough ( dData.second * 2 );
  2723. sphJsonFormat ( dJson, dData.first );
  2724. tOut.SendArray ( dJson );
  2725. } else
  2726. // magic zero - "{}"
  2727. tOut.SendArray ( g_sJsonNull, sizeof ( g_sJsonNull )-1 );
  2728. }
  2729. static void SendJson ( ISphOutputBuffer& tOut, const BYTE * pJSON, bool bSendJson )
  2730. {
  2731. if ( bSendJson )
  2732. SendDataPtrAttr ( tOut, pJSON ); // send BSON
  2733. else
  2734. SendJsonAsString ( tOut, pJSON ); // send string
  2735. }
  2736. static void SendJsonFieldAsString ( ISphOutputBuffer& tOut, const BYTE * pJSON )
  2737. {
  2738. if ( !pJSON )
  2739. {
  2740. tOut.SendDword(0);
  2741. return;
  2742. }
  2743. auto dData = sphUnpackPtrAttr ( pJSON );
  2744. auto eJson = (ESphJsonType) *dData.first++;
  2745. JsonEscapedBuilder dJson;
  2746. dJson.GrowEnough ( dData.second * 2 );
  2747. sphJsonFieldFormat ( dJson, dData.first, eJson, false );
  2748. tOut.SendArray ( dJson );
  2749. }
  2750. static void SendJsonField ( ISphOutputBuffer& tOut, const BYTE * pJSON, bool bSendJsonField )
  2751. {
  2752. if ( !bSendJsonField )
  2753. {
  2754. SendJsonFieldAsString ( tOut, pJSON );
  2755. return;
  2756. }
  2757. auto dData = sphUnpackPtrAttr ( pJSON );
  2758. if ( IsEmpty ( dData ) || *dData.first==JSON_EOF )
  2759. tOut.SendByte ( JSON_EOF );
  2760. else
  2761. {
  2762. tOut.SendByte ( *dData.first );
  2763. tOut.SendArray ( dData.first+1, dData.second-1 );
  2764. }
  2765. }
  2766. static void SendMVA ( ISphOutputBuffer& tOut, const BYTE * pMVA, bool b64bit )
  2767. {
  2768. if ( !pMVA )
  2769. {
  2770. tOut.SendDword ( 0 );
  2771. return;
  2772. }
  2773. auto dMVA = sphUnpackPtrAttr ( pMVA );
  2774. DWORD uValues = dMVA.second / sizeof(DWORD);
  2775. tOut.SendDword(uValues);
  2776. const auto * pValues = (const DWORD *) dMVA.first;
  2777. if ( b64bit )
  2778. {
  2779. assert ( ( uValues%2 )==0 );
  2780. while ( uValues )
  2781. {
  2782. auto uMVA = MVA_BE ( pValues );
  2783. tOut.SendDword ( uMVA.first );
  2784. tOut.SendDword ( uMVA.second );
  2785. pValues += 2;
  2786. uValues -= 2;
  2787. }
  2788. } else
  2789. {
  2790. while ( uValues-- )
  2791. tOut.SendDword ( *pValues++ );
  2792. }
  2793. }
  2794. static ESphAttr FixupAttrForNetwork ( const CSphColumnInfo & tCol, const CSphSchema & tSchema, int iVer, WORD uMasterVer, bool bAgentMode )
  2795. {
  2796. bool bSendJson = ( bAgentMode && uMasterVer>=3 );
  2797. bool bSendJsonField = ( bAgentMode && uMasterVer>=4 );
  2798. switch ( tCol.m_eAttrType )
  2799. {
  2800. case SPH_ATTR_UINT32SET_PTR:
  2801. return SPH_ATTR_UINT32SET;
  2802. case SPH_ATTR_INT64SET_PTR:
  2803. return SPH_ATTR_INT64SET;
  2804. case SPH_ATTR_STRINGPTR:
  2805. {
  2806. if ( bAgentMode && uMasterVer>=18 && IsNotRealAttribute ( tCol ) )
  2807. return SPH_ATTR_STORED_FIELD;
  2808. else
  2809. return SPH_ATTR_STRING;
  2810. }
  2811. case SPH_ATTR_JSON:
  2812. case SPH_ATTR_JSON_PTR:
  2813. return bSendJson ? SPH_ATTR_JSON : SPH_ATTR_STRING;
  2814. case SPH_ATTR_JSON_FIELD:
  2815. case SPH_ATTR_JSON_FIELD_PTR:
  2816. return bSendJsonField ? SPH_ATTR_JSON_FIELD : SPH_ATTR_STRING;
  2817. case SPH_ATTR_DOUBLE:
  2818. return iVer<0x122 ? SPH_ATTR_FLOAT : SPH_ATTR_DOUBLE;
  2819. default: return tCol.m_eAttrType;
  2820. }
  2821. }
  2822. static void SendSchema ( ISphOutputBuffer & tOut, const AggrResult_t & tRes, const CSphBitvec & tAttrsToSend, int iVer, WORD uMasterVer, bool bAgentMode )
  2823. {
  2824. int iFieldsCount = tRes.m_tSchema.GetFieldsCount();
  2825. tOut.SendInt ( iFieldsCount );
  2826. for ( int i=0; i < iFieldsCount; ++i )
  2827. tOut.SendString ( tRes.m_tSchema.GetFieldName(i) );
  2828. tOut.SendInt ( tAttrsToSend.BitCount() );
  2829. for ( int i=0; i<tRes.m_tSchema.GetAttrsCount(); ++i )
  2830. {
  2831. if ( !tAttrsToSend.BitGet(i) )
  2832. continue;
  2833. const CSphColumnInfo & tCol = tRes.m_tSchema.GetAttr(i);
  2834. tOut.SendString ( tCol.m_sName.cstr() );
  2835. ESphAttr eCol = FixupAttrForNetwork ( tCol, tRes.m_tSchema, iVer, uMasterVer, bAgentMode );
  2836. tOut.SendDword ( (DWORD)eCol );
  2837. }
  2838. }
  2839. static void SendAttribute ( ISphOutputBuffer & tOut, const CSphMatch & tMatch, const CSphColumnInfo & tAttr, int iVer, WORD uMasterVer, bool bAgentMode )
  2840. {
  2841. // at this point we should not have any attributes that point to pooled data
  2842. assert ( sphPlainAttrToPtrAttr(tAttr.m_eAttrType)==tAttr.m_eAttrType );
  2843. // send binary json only to master
  2844. bool bSendJson = bAgentMode && uMasterVer>=3;
  2845. bool bSendJsonField = bAgentMode && uMasterVer>=4;
  2846. const CSphAttrLocator & tLoc = tAttr.m_tLocator;
  2847. switch ( tAttr.m_eAttrType )
  2848. {
  2849. case SPH_ATTR_UINT32SET_PTR:
  2850. case SPH_ATTR_INT64SET_PTR:
  2851. SendMVA ( tOut, (const BYTE*)tMatch.GetAttr(tLoc), tAttr.m_eAttrType==SPH_ATTR_INT64SET_PTR );
  2852. break;
  2853. case SPH_ATTR_JSON_PTR:
  2854. SendJson ( tOut, (const BYTE*)tMatch.GetAttr(tLoc), bSendJson );
  2855. break;
  2856. case SPH_ATTR_STRINGPTR:
  2857. SendDataPtrAttr ( tOut, (const BYTE*)tMatch.GetAttr(tLoc) );
  2858. break;
  2859. case SPH_ATTR_JSON_FIELD_PTR:
  2860. SendJsonField ( tOut, (const BYTE*)tMatch.GetAttr(tLoc), bSendJsonField );
  2861. break;
  2862. case SPH_ATTR_FACTORS:
  2863. case SPH_ATTR_FACTORS_JSON:
  2864. if ( iVer<0x11C )
  2865. {
  2866. tOut.SendDword ( 0 );
  2867. break;
  2868. }
  2869. SendDataPtrAttr ( tOut, (const BYTE*)tMatch.GetAttr(tLoc) );
  2870. break;
  2871. case SPH_ATTR_FLOAT:
  2872. tOut.SendFloat ( tMatch.GetAttrFloat(tLoc) );
  2873. break;
  2874. case SPH_ATTR_DOUBLE:
  2875. if ( iVer<0x122 )
  2876. tOut.SendFloat ( (float)tMatch.GetAttrDouble(tLoc) );
  2877. else
  2878. tOut.SendDouble ( tMatch.GetAttrDouble(tLoc) );
  2879. break;
  2880. case SPH_ATTR_BIGINT:
  2881. tOut.SendUint64 ( tMatch.GetAttr(tLoc) );
  2882. break;
  2883. default:
  2884. tOut.SendDword ( (DWORD)tMatch.GetAttr(tLoc) );
  2885. break;
  2886. }
  2887. }
  2888. void SendResult ( int iVer, ISphOutputBuffer & tOut, const AggrResult_t& tRes, bool bAgentMode, const CSphQuery & tQuery, WORD uMasterVer )
  2889. {
  2890. // multi-query status
  2891. bool bError = !tRes.m_sError.IsEmpty();
  2892. bool bWarning = !bError && !tRes.m_sWarning.IsEmpty();
  2893. assert ( bError || tRes.m_bSingle );
  2894. assert ( bError || tRes.m_bOneSchema );
  2895. if ( bError )
  2896. {
  2897. tOut.SendInt ( SEARCHD_ERROR ); // fixme! m.b. use APICommand_t and refactor to common API way
  2898. tOut.SendString ( tRes.m_sError.cstr() );
  2899. if ( g_bOptNoDetach && g_eLogFormat!=LOG_FORMAT_SPHINXQL )
  2900. sphInfo ( "query error: %s", tRes.m_sError.cstr() );
  2901. return;
  2902. } else if ( bWarning )
  2903. {
  2904. tOut.SendDword ( SEARCHD_WARNING );
  2905. tOut.SendString ( tRes.m_sWarning.cstr() );
  2906. if ( g_bOptNoDetach && g_eLogFormat!=LOG_FORMAT_SPHINXQL )
  2907. sphInfo ( "query warning: %s", tRes.m_sWarning.cstr() );
  2908. } else
  2909. tOut.SendDword ( SEARCHD_OK );
  2910. CSphBitvec tAttrsToSend;
  2911. sphGetAttrsToSend ( tRes.m_tSchema, bAgentMode, false, tAttrsToSend );
  2912. // send schema
  2913. SendSchema ( tOut, tRes, tAttrsToSend, iVer, uMasterVer, bAgentMode );
  2914. // send matches
  2915. tOut.SendInt ( tRes.m_iCount );
  2916. tOut.SendInt ( 1 ); // was USE_64BIT
  2917. CSphVector<BYTE> dJson ( 512 );
  2918. auto& dResult = tRes.m_dResults.First();
  2919. auto dMatches = dResult.m_dMatches.Slice ( tRes.m_iOffset, tRes.m_iCount );
  2920. for ( const CSphMatch & tMatch : dMatches )
  2921. {
  2922. Verify ( tRes.m_tSchema.GetAttr(sphGetDocidName()) );
  2923. tOut.SendUint64 ( sphGetDocID(tMatch.m_pDynamic) );
  2924. tOut.SendInt ( tMatch.m_iWeight );
  2925. assert ( tMatch.m_pStatic || !tRes.m_tSchema.GetStaticSize() );
  2926. #if 0
  2927. // not correct any more because of internal attrs (such as string sorting ptrs)
  2928. assert ( tMatch.m_pDynamic || !pRes->m_tSchema.GetDynamicSize() );
  2929. assert ( !tMatch.m_pDynamic || (int)tMatch.m_pDynamic[-1]==pRes->m_tSchema.GetDynamicSize() );
  2930. #endif
  2931. for ( int j=0; j<tRes.m_tSchema.GetAttrsCount(); ++j )
  2932. if ( tAttrsToSend.BitGet(j) )
  2933. SendAttribute ( tOut, tMatch, tRes.m_tSchema.GetAttr(j), iVer, uMasterVer, bAgentMode );
  2934. }
  2935. if ( tQuery.m_bAgent && tQuery.m_iLimit )
  2936. tOut.SendInt ( tRes.m_iCount );
  2937. else
  2938. tOut.SendInt ( dResult.m_dMatches.GetLength() );
  2939. tOut.SendAsDword ( tRes.m_iTotalMatches );
  2940. if ( bAgentMode && uMasterVer>=19 )
  2941. tOut.SendInt ( tRes.m_bTotalMatchesApprox ? 1 : 0 );
  2942. tOut.SendInt ( Max ( tRes.m_iQueryTime, 0 ) );
  2943. if ( iVer>=0x11A && bAgentMode )
  2944. {
  2945. bool bNeedPredictedTime = tQuery.m_iMaxPredictedMsec > 0;
  2946. BYTE uStatMask = ( bNeedPredictedTime ? 4U : 0U ) | ( g_bCpuStats ? 2U : 0U ) | ( g_bIOStats ? 1U : 0U );
  2947. tOut.SendByte ( uStatMask );
  2948. if ( g_bIOStats )
  2949. {
  2950. CSphIOStats tStats = tRes.m_tIOStats;
  2951. tStats.Add ( tRes.m_tAgentIOStats );
  2952. tOut.SendUint64 ( tStats.m_iReadTime );
  2953. tOut.SendDword ( tStats.m_iReadOps );
  2954. tOut.SendUint64 ( tStats.m_iReadBytes );
  2955. tOut.SendUint64 ( tStats.m_iWriteTime );
  2956. tOut.SendDword ( tStats.m_iWriteOps );
  2957. tOut.SendUint64 ( tStats.m_iWriteBytes );
  2958. }
  2959. if ( g_bCpuStats )
  2960. {
  2961. int64_t iCpuTime = tRes.m_iCpuTime + tRes.m_iAgentCpuTime;
  2962. tOut.SendUint64 ( iCpuTime );
  2963. }
  2964. if ( bNeedPredictedTime )
  2965. tOut.SendUint64 ( tRes.m_iPredictedTime + tRes.m_iAgentPredictedTime );
  2966. }
  2967. if ( bAgentMode && uMasterVer>=7 )
  2968. {
  2969. tOut.SendDword ( tRes.m_tStats.m_iFetchedDocs + tRes.m_iAgentFetchedDocs );
  2970. tOut.SendDword ( tRes.m_tStats.m_iFetchedHits + tRes.m_iAgentFetchedHits );
  2971. if ( uMasterVer>=8 )
  2972. tOut.SendDword ( tRes.m_tStats.m_iSkips + tRes.m_iAgentFetchedSkips );
  2973. }
  2974. auto dWords = tRes.MakeSortedWordStat ();
  2975. tOut.SendInt ( dWords.GetLength() );
  2976. for( auto * pWord : dWords )
  2977. {
  2978. assert ( pWord );
  2979. tOut.SendString ( pWord->first.cstr () );
  2980. tOut.SendAsDword ( pWord->second.first );
  2981. tOut.SendAsDword ( pWord->second.second );
  2982. if ( bAgentMode )
  2983. tOut.SendByte ( 0 ); // statistics have no expanded terms for now
  2984. }
  2985. }
  2986. /////////////////////////////////////////////////////////////////////////////
  2987. int AggrResult_t::GetLength () const
  2988. {
  2989. int iCount = 0;
  2990. m_dResults.Apply ( [&iCount] ( const OneResultset_t & a ) { iCount += a.m_dMatches.GetLength (); } );
  2991. return iCount;
  2992. }
  2993. bool AggrResult_t::AddResultset ( ISphMatchSorter * pQueue, const DocstoreReader_i * pDocstore, int iTag, int iCutoff )
  2994. {
  2995. assert ( pQueue );
  2996. if ( !pQueue->GetLength () )
  2997. {
  2998. m_tSchema = *pQueue->GetSchema ();
  2999. return false;
  3000. }
  3001. // extract matches from sorter
  3002. auto & tOneRes = m_dResults.Add ();
  3003. tOneRes.m_pDocstore = pDocstore;
  3004. tOneRes.m_iTag = iTag;
  3005. tOneRes.FillFromSorter ( pQueue );
  3006. // in MT case each thread has its own cutoff, so we have to enforce it again on the result set
  3007. if ( iCutoff>0 )
  3008. {
  3009. m_iTotalMatches = Min ( iCutoff, m_iTotalMatches );
  3010. tOneRes.ClampMatches(iCutoff);
  3011. }
  3012. return true;
  3013. }
  3014. void AggrResult_t::AddEmptyResultset ( const DocstoreReader_i * pDocstore, int iTag )
  3015. {
  3016. auto & tOneRes = m_dResults.Add();
  3017. tOneRes.m_pDocstore = pDocstore;
  3018. tOneRes.m_iTag = iTag;
  3019. }
  3020. void AggrResult_t::ClampMatches ( int iLimit )
  3021. {
  3022. assert ( m_bSingle );
  3023. m_dResults.First ().ClampMatches ( iLimit );
  3024. }
  3025. void AggrResult_t::ClampAllMatches ()
  3026. {
  3027. for ( auto& dResult : m_dResults )
  3028. dResult.ClampAllMatches();
  3029. }
  3030. int OneResultset_t::FillFromSorter ( ISphMatchSorter * pQueue )
  3031. {
  3032. if ( !pQueue )
  3033. return 0;
  3034. assert ( m_dMatches.IsEmpty () );
  3035. m_tSchema = *pQueue->GetSchema ();
  3036. if ( !pQueue->GetLength () )
  3037. return 0;
  3038. int iCopied = pQueue->Flatten ( m_dMatches.AddN ( pQueue->GetLength () ) );
  3039. m_dMatches.Resize ( iCopied );
  3040. return iCopied;
  3041. }
  3042. void OneResultset_t::ClampAllMatches ()
  3043. {
  3044. for ( auto& dMatch : m_dMatches )
  3045. {
  3046. m_tSchema.FreeDataPtrs ( dMatch );
  3047. dMatch.ResetDynamic();
  3048. }
  3049. m_dMatches.Reset();
  3050. }
  3051. void OneResultset_t::ClampMatches ( int iLimit )
  3052. {
  3053. assert ( iLimit>0 );
  3054. int iMatches = m_dMatches.GetLength ();
  3055. for ( int i = iLimit; i<iMatches; ++i )
  3056. {
  3057. m_tSchema.FreeDataPtrs ( m_dMatches[i] );
  3058. m_dMatches[i].ResetDynamic();
  3059. }
  3060. m_dMatches.Resize ( Min (iMatches, iLimit ) );
  3061. }
  3062. OneResultset_t::~OneResultset_t()
  3063. {
  3064. ClampAllMatches();
  3065. }
  3066. namespace { // static
  3067. void RemapResult ( AggrResult_t & dResult )
  3068. {
  3069. const ISphSchema & tSchema = dResult.m_tSchema;
  3070. int iAttrsCount = tSchema.GetAttrsCount();
  3071. CSphVector<int> dMapFrom(iAttrsCount);
  3072. CSphVector<int> dRowItems(iAttrsCount);
  3073. static const int SIZE_OF_ROW = 8 * sizeof ( CSphRowitem );
  3074. for ( auto & tRes : dResult.m_dResults )
  3075. {
  3076. if ( tRes.m_dMatches.IsEmpty() )
  3077. continue;
  3078. dMapFrom.Resize ( 0 );
  3079. dRowItems.Resize ( 0 );
  3080. CSphSchema & dSchema = tRes.m_tSchema;
  3081. for ( int i = 0; i<iAttrsCount; ++i )
  3082. {
  3083. auto iSrcCol = dSchema.GetAttrIndex ( tSchema.GetAttr ( i ).m_sName.cstr () );
  3084. dMapFrom.Add ( iSrcCol );
  3085. dRowItems.Add ( dSchema.GetAttr ( iSrcCol ).m_tLocator.m_iBitOffset / SIZE_OF_ROW );
  3086. assert ( dMapFrom[i]>=0
  3087. || IsSortStringInternal ( tSchema.GetAttr(i).m_sName )
  3088. || IsSortJsonInternal ( tSchema.GetAttr(i).m_sName )
  3089. );
  3090. }
  3091. // inverse dRowItems - we'll free only those NOT enumerated yet
  3092. dRowItems = dSchema.SubsetPtrs ( dRowItems );
  3093. for ( auto& tMatch : tRes.m_dMatches )
  3094. {
  3095. // create new and shiny (and properly sized) match
  3096. CSphMatch tNewMatch;
  3097. tNewMatch.Reset ( tSchema.GetDynamicSize () );
  3098. tNewMatch.m_tRowID = tMatch.m_tRowID;
  3099. tNewMatch.m_iWeight = tMatch.m_iWeight;
  3100. // remap attrs
  3101. for ( int j = 0; j<iAttrsCount; ++j )
  3102. {
  3103. const CSphColumnInfo & tDst = tSchema.GetAttr ( j );
  3104. // we could keep some of the rows static
  3105. // and so, avoid the duplication of the data.
  3106. int iMapFrom = dMapFrom[j];
  3107. const CSphColumnInfo & tSrc = dSchema.GetAttr ( iMapFrom );
  3108. if ( !tDst.m_tLocator.m_bDynamic )
  3109. {
  3110. assert ( iMapFrom<0 || !dSchema.GetAttr ( iMapFrom ).m_tLocator.m_bDynamic );
  3111. tNewMatch.m_pStatic = tMatch.m_pStatic;
  3112. } else if ( iMapFrom>=0 )
  3113. {
  3114. if ( tDst.m_eAttrType==SPH_ATTR_FLOAT && tSrc.m_eAttrType==SPH_ATTR_BOOL )
  3115. {
  3116. tNewMatch.SetAttrFloat ( tDst.m_tLocator, ( tMatch.GetAttr ( tSrc.m_tLocator )>0 ? 1.0f : 0.0f ) );
  3117. } else
  3118. {
  3119. tNewMatch.SetAttr ( tDst.m_tLocator, tMatch.GetAttr ( tSrc.m_tLocator ) );
  3120. }
  3121. }
  3122. }
  3123. // swap out old (most likely wrong sized) match
  3124. Swap ( tMatch, tNewMatch );
  3125. CSphSchemaHelper::FreeDataSpecial ( tNewMatch, dRowItems );
  3126. }
  3127. }
  3128. }
  3129. bool GetIndexSchemaItems ( const ISphSchema & tSchema, const CSphVector<CSphQueryItem> & dItems, CSphVector<int> & dAttrs )
  3130. {
  3131. bool bHaveAsterisk = false;
  3132. for ( const auto & i : dItems )
  3133. {
  3134. if ( i.m_sAlias.cstr() )
  3135. {
  3136. int iAttr = tSchema.GetAttrIndex ( i.m_sAlias.cstr() );
  3137. if ( iAttr>=0 )
  3138. dAttrs.Add(iAttr);
  3139. }
  3140. bHaveAsterisk |= i.m_sExpr=="*";
  3141. }
  3142. dAttrs.Sort();
  3143. return bHaveAsterisk;
  3144. }
  3145. bool GetItemsLeftInSchema ( const ISphSchema & tSchema, bool bOnlyPlain, const CSphVector<int> & dAttrs, CSphVector<int> & dAttrsInSchema )
  3146. {
  3147. bool bHaveExprs = false;
  3148. for ( int i = 0, iAttrsCount = tSchema.GetAttrsCount (); i<iAttrsCount; ++i )
  3149. {
  3150. const CSphColumnInfo & tAttr = tSchema.GetAttr(i);
  3151. if ( tAttr.m_pExpr )
  3152. {
  3153. bHaveExprs = true;
  3154. // need to keep post-limit expression (stored field) for multi-query \ facet
  3155. // also keep columnar attributes (with expressions)
  3156. if ( bOnlyPlain && !tAttr.m_pExpr->IsColumnar() && tAttr.m_eStage!=SPH_EVAL_POSTLIMIT )
  3157. continue;
  3158. }
  3159. if ( tAttr.m_sName.cstr()[0]!='@' && !dAttrs.BinarySearch(i) )
  3160. dAttrsInSchema.Add(i);
  3161. }
  3162. return bHaveExprs;
  3163. }
  3164. void DoExpansion ( const ISphSchema & tSchema, const CSphVector<int> & dAttrsInSchema, const CSphVector<CSphQueryItem> & dItems, CSphVector<CSphQueryItem> & dExpanded )
  3165. {
  3166. bool bExpandedAsterisk = false;
  3167. for ( const auto & i : dItems )
  3168. {
  3169. if ( i.m_sExpr=="*" )
  3170. {
  3171. if ( bExpandedAsterisk )
  3172. continue;
  3173. bExpandedAsterisk = true;
  3174. for ( auto iAttr : dAttrsInSchema )
  3175. {
  3176. const CSphColumnInfo & tCol = tSchema.GetAttr(iAttr);
  3177. CSphQueryItem & tExpanded = dExpanded.Add();
  3178. tExpanded.m_sExpr = tCol.m_sName;
  3179. if ( tCol.m_pExpr ) // stored fields
  3180. tExpanded.m_sAlias = tCol.m_sName;
  3181. }
  3182. }
  3183. else
  3184. dExpanded.Add(i);
  3185. }
  3186. }
  3187. // rebuild the results itemlist expanding stars
  3188. const CSphVector<CSphQueryItem> & ExpandAsterisk ( const ISphSchema & tSchema, const CSphVector<CSphQueryItem> & dItems, CSphVector<CSphQueryItem> & tExpanded, bool bOnlyPlain, bool & bHaveExprs )
  3189. {
  3190. // the result schema usually is the index schema + calculated items + @-items
  3191. // we need to extract the index schema only
  3192. CSphVector<int> dIndexSchemaAttrs;
  3193. bool bHaveAsterisk = GetIndexSchemaItems ( tSchema, dItems, dIndexSchemaAttrs );
  3194. // no stars? Nothing to do.
  3195. if ( !bHaveAsterisk )
  3196. return dItems;
  3197. // find items that are in index schema but not in our requested item list
  3198. // not do not include @-items
  3199. CSphVector<int> dAttrsLeftInSchema;
  3200. bHaveExprs = GetItemsLeftInSchema ( tSchema, bOnlyPlain, dIndexSchemaAttrs, dAttrsLeftInSchema );
  3201. DoExpansion ( tSchema, dAttrsLeftInSchema, dItems, tExpanded );
  3202. return tExpanded;
  3203. }
  3204. // in MatchIterator_c we need matches sorted assending by DocID.
  3205. // also we don't want to sort matches themselves; sorted vec of indexes quite enough
  3206. // also we wont to avoid allocating vec for the matches as it may be huge.
  3207. // There are several possible solutions to have vec of indexes:
  3208. // 1. Use matches tags, as they're not used in this part of code. With intensive working it is however not a good in
  3209. // terms of cache misses (i.e. 'min' match is match[N] where N is match[0].tag, then match[M] where M is match[1] tag.
  3210. // So each time we make about random jumps.
  3211. // 2. Use space between last match and end of the vector (assuming reserved space > used space). If it is enough space,
  3212. // we can use it either as vec or WORDS, or as vec or DWORDS, depending from N of matches. First case need at most 128K
  3213. // of RAM, second needs more, but that RAM is compact.
  3214. // So, let's try with tail space first, but if it is not available (no, or not enough space), use tags.
  3215. // That is to sort tags in matches without moving rest of them.
  3216. class MatchTagSortAccessor_c
  3217. {
  3218. const VecTraits_T<CSphMatch> & m_dTagOrder;
  3219. public:
  3220. explicit MatchTagSortAccessor_c ( const VecTraits_T<CSphMatch> & dTagOrder) : m_dTagOrder ( dTagOrder ) {}
  3221. using T = CSphMatch;
  3222. using MEDIAN_TYPE = int;
  3223. static MEDIAN_TYPE Key ( T * a ) { return a->m_iTag; }
  3224. static void Swap ( T * a, T * b ) { ::Swap ( a->m_iTag, b->m_iTag ); }
  3225. static T * Add ( T * p, int i ) { return p+i; }
  3226. static int Sub ( T * b, T * a ) { return (int)(b-a); }
  3227. static void CopyKey ( MEDIAN_TYPE * pMed, CSphMatch * pVal ) { *pMed = Key ( pVal ); }
  3228. bool IsLess ( int a, int b ) const
  3229. {
  3230. return sphGetDocID ( m_dTagOrder[a].m_pDynamic )<sphGetDocID ( m_dTagOrder[b].m_pDynamic );
  3231. }
  3232. };
  3233. class MatchIterator_c
  3234. {
  3235. int m_iRawIdx; // raw iteration index (internal)
  3236. int m_iLimit;
  3237. std::function<int(int)> m_fnOrder; // use to access matches by accending docid order
  3238. bool m_bTailClean = false;
  3239. // use space after end of matches to store indexes, WORD per match
  3240. bool MaybeUseWordOrder ( const CSphSwapVector<CSphMatch>& dMatches ) const
  3241. {
  3242. if ( dMatches.GetLength()>0x10000 )
  3243. return false;
  3244. int64_t iTail = dMatches.AllocatedBytes ()-dMatches.GetLengthBytes64 ();
  3245. if ( iTail<(int64_t) ( dMatches.GetLength () * sizeof ( WORD ) ) )
  3246. return false;
  3247. // will use tail of the vec as blob of WORDs
  3248. VecTraits_T<WORD> dOrder = { (WORD *) dMatches.end (), m_iLimit };
  3249. ARRAY_CONSTFOREACH( i, dOrder )
  3250. dOrder[i] = i;
  3251. dOrder.Sort ( Lesser ( [&dMatches] ( WORD a, WORD b ) {
  3252. return sphGetDocID ( dMatches[a].m_pDynamic )<sphGetDocID ( dMatches[b].m_pDynamic );
  3253. } ) );
  3254. return true;
  3255. }
  3256. // use space after end of matches to store indexes, DWORD per match
  3257. bool MaybeUseDwordOrder ( const CSphSwapVector<CSphMatch>& dMatches ) const
  3258. {
  3259. if ( dMatches.GetLength64()>0x100000000 )
  3260. return false;
  3261. int64_t iTail = dMatches.AllocatedBytes ()-dMatches.GetLengthBytes64 ();
  3262. if ( iTail<(int64_t) ( dMatches.GetLength () * sizeof ( DWORD ) ) )
  3263. return false;
  3264. // will use tail of the vec as blob of WORDs
  3265. VecTraits_T<DWORD> dOrder = { (DWORD *) dMatches.end (), m_iLimit };
  3266. for( DWORD i=0, uLen=dOrder.GetLength(); i<uLen; ++i )
  3267. dOrder[i] = i;
  3268. dOrder.Sort ( Lesser ( [&dMatches] ( DWORD a, DWORD b ) {
  3269. return sphGetDocID ( dMatches[a].m_pDynamic )<sphGetDocID ( dMatches[b].m_pDynamic );
  3270. } ) );
  3271. return true;
  3272. }
  3273. // use tags to store indexes. No extra space, but random access order, many cash misses expected
  3274. void UseTags ( VecTraits_T<CSphMatch> & dOrder )
  3275. {
  3276. ARRAY_CONSTFOREACH( i, dOrder )
  3277. dOrder[i].m_iTag = i;
  3278. MatchTagSortAccessor_c tOrder ( dOrder );
  3279. sphSort ( dOrder.Begin (), dOrder.GetLength (), tOrder, tOrder );
  3280. m_bTailClean = true;
  3281. }
  3282. public:
  3283. OneResultset_t& m_tResult;
  3284. DocID_t m_tDocID;
  3285. int m_iIdx; // ordering index (each step gives matches in sorted by Docid order)
  3286. explicit MatchIterator_c ( OneResultset_t & tResult )
  3287. : m_tResult ( tResult )
  3288. {
  3289. auto& dMatches = tResult.m_dMatches;
  3290. m_iLimit = dMatches.GetLength();
  3291. if ( MaybeUseWordOrder ( dMatches ) )
  3292. m_fnOrder = [pData = (WORD *) m_tResult.m_dMatches.end ()] ( int i ) { return pData[i]; };
  3293. else if ( MaybeUseDwordOrder ( dMatches ) )
  3294. m_fnOrder = [pData = (DWORD *) m_tResult.m_dMatches.end ()] ( int i ) { return pData[i]; };
  3295. else
  3296. {
  3297. UseTags ( dMatches );
  3298. m_fnOrder = [this] ( int i ) { return m_tResult.m_dMatches[m_iRawIdx].m_iTag; };
  3299. }
  3300. m_iRawIdx = 0;
  3301. m_iIdx = m_fnOrder(0);
  3302. assert ( m_tResult.m_tSchema.GetAttr ( sphGetDocidName() ) );
  3303. m_tDocID = sphGetDocID ( m_tResult.m_dMatches[m_iIdx].m_pDynamic );
  3304. }
  3305. ~MatchIterator_c()
  3306. {
  3307. if ( m_bTailClean )
  3308. return;
  3309. // need to reset state of some tail matches in order to avoid issues when deleting the vec of them
  3310. // (since we used that memory region for own purposes)
  3311. int iDirtyMatches = m_iLimit>0x10000 ? m_iLimit * sizeof ( DWORD ) : m_iLimit * sizeof ( WORD );
  3312. iDirtyMatches = ( iDirtyMatches+sizeof ( CSphMatch )-1 ) / sizeof ( CSphMatch );
  3313. for ( int i = 0; i<iDirtyMatches; ++i )
  3314. ( m_tResult.m_dMatches.end ()+i )->CleanGarbage();
  3315. }
  3316. inline bool Step()
  3317. {
  3318. ++m_iRawIdx;
  3319. if ( m_iRawIdx>=m_iLimit )
  3320. return false;
  3321. m_iIdx = m_fnOrder ( m_iRawIdx );
  3322. m_tDocID = sphGetDocID ( m_tResult.m_dMatches[m_iIdx].m_pDynamic );
  3323. return true;
  3324. }
  3325. static inline bool IsLess ( MatchIterator_c *a, MatchIterator_c *b )
  3326. {
  3327. if ( a->m_tDocID!=b->m_tDocID )
  3328. return a->m_tDocID<b->m_tDocID;
  3329. // that mean local matches always preffered over remote, but it seems that is not necessary
  3330. // if ( !a->m_dResult.m_bTag && b->m_dResult.m_bTag )
  3331. // return true;
  3332. return a->m_tResult.m_iTag>b->m_tResult.m_iTag;
  3333. }
  3334. };
  3335. int KillPlainDupes ( ISphMatchSorter * pSorter, AggrResult_t & tRes )
  3336. {
  3337. int iDupes = 0;
  3338. auto& dResults = tRes.m_dResults;
  3339. // normal sorter needs massage
  3340. // queue by docid and then ascending by tag to guarantee the replacement order
  3341. RawVector_T <MatchIterator_c> dIterators;
  3342. dIterators.Reserve_static ( dResults.GetLength () );
  3343. CSphQueue<MatchIterator_c *, MatchIterator_c> qMatches ( dResults.GetLength () );
  3344. for ( auto & tResult : dResults )
  3345. if ( !tResult.m_dMatches.IsEmpty() )
  3346. {
  3347. dIterators.Emplace_back(tResult);
  3348. qMatches.Push ( &dIterators.Last() );
  3349. }
  3350. DocID_t tPrevDocID = DOCID_MIN;
  3351. while ( qMatches.GetLength() )
  3352. {
  3353. auto * pMin = qMatches.Root();
  3354. DocID_t tDocID = pMin->m_tDocID;
  3355. if ( tDocID!=tPrevDocID ) // by default, simply remove dupes (select first by tag)
  3356. {
  3357. CSphMatch & tMatch = pMin->m_tResult.m_dMatches[pMin->m_iIdx];
  3358. auto iTag = tMatch.m_iTag; // as we may use tag for ordering
  3359. if ( !pMin->m_tResult.m_bTagsAssigned )
  3360. tMatch.m_iTag = pMin->m_tResult.m_iTag; // that will link us back to docstore
  3361. pSorter->Push ( tMatch );
  3362. tMatch.m_iTag = iTag; // restore tag
  3363. tPrevDocID = tDocID;
  3364. }
  3365. else
  3366. ++iDupes;
  3367. qMatches.Pop ();
  3368. if ( pMin->Step() )
  3369. qMatches.Push ( pMin );
  3370. }
  3371. tRes.m_bTagsAssigned = true;
  3372. return iDupes;
  3373. }
  3374. int KillGroupbyDupes ( ISphMatchSorter * pSorter, AggrResult_t & tRes, const VecTraits_T<int>& dOrd )
  3375. {
  3376. int iDupes = 0;
  3377. pSorter->SetBlobPool ( nullptr );
  3378. for ( int iOrd : dOrd )
  3379. {
  3380. auto & tResult = tRes.m_dResults[iOrd];
  3381. ARRAY_CONSTFOREACH( i, tResult.m_dMatches )
  3382. {
  3383. CSphMatch & tMatch = tResult.m_dMatches[i];
  3384. if ( !tResult.m_bTagsAssigned )
  3385. tMatch.m_iTag = tResult.m_iTag; // that will link us back to docstore
  3386. if ( !pSorter->PushGrouped ( tMatch, i==0 ) ) // groupby sorter does that automagically
  3387. ++iDupes;
  3388. }
  3389. }
  3390. tRes.m_bTagsAssigned = true;
  3391. return iDupes;
  3392. }
  3393. // rearrange results so thet the're placed by accending tags order
  3394. // dOrd contains indexes to access results in descending tag order
  3395. void SortTagsAndDocstores ( AggrResult_t & tRes, const VecTraits_T<int>& dOrd )
  3396. {
  3397. auto iTags = dOrd.GetLength ();
  3398. CSphFixedVector<DocstoreAndTag_t> dTmp { iTags };
  3399. auto & dResults = tRes.m_dResults;
  3400. for ( int i=0; i<iTags; ++i )
  3401. dTmp[iTags-i-1].Assign ( dResults[dOrd[i]] );
  3402. for ( int i = 0; i<iTags; ++i )
  3403. dResults[i].Assign ( dTmp[i] );
  3404. Debug ( tRes.m_bIdxByTag = true; )
  3405. }
  3406. int KillDupesAndFlatten ( ISphMatchSorter * pSorter, AggrResult_t & tRes )
  3407. {
  3408. assert ( pSorter );
  3409. int iTags = tRes.m_dResults.GetLength();
  3410. CSphFixedVector<int> dOrd ( iTags );
  3411. ARRAY_CONSTFOREACH( i, dOrd )
  3412. dOrd[i] = i;
  3413. // sort resultsets in descending tag order
  3414. dOrd.Sort ( Lesser ( [&tRes] ( int l, int r ) { return tRes.m_dResults[r].m_iTag<tRes.m_dResults[l].m_iTag; } ) );
  3415. // remap to compact (non-fragmented) range of tags
  3416. for ( int iRes : dOrd )
  3417. tRes.m_dResults[iRes].m_iTag = --iTags;
  3418. Debug ( tRes.m_bTagsCompacted = true );
  3419. // do actual deduplication
  3420. int iDup = pSorter->IsGroupby() ? KillGroupbyDupes ( pSorter, tRes, dOrd ) : KillPlainDupes ( pSorter, tRes );
  3421. // ALL matches have same schema, as KillAllDupes called after RemapResults(), or already having identical schemas.
  3422. for ( auto& dResult : tRes.m_dResults )
  3423. {
  3424. for ( auto& dMatch : dResult.m_dMatches )
  3425. tRes.m_tSchema.FreeDataPtrs ( dMatch );
  3426. dResult.m_dMatches.Reset();
  3427. }
  3428. // don't issue tRes.m_dResults.reset since each result still has a docstore by tag
  3429. // flatten all results into single chunk
  3430. auto & tFinalMatches = tRes.m_dResults.First ();
  3431. tFinalMatches.FillFromSorter ( pSorter );
  3432. Debug ( tRes.m_bSingle = true; )
  3433. Debug ( tRes.m_bOneSchema = true; )
  3434. // now all matches properly tagged located in tRes.m_dResults.First()
  3435. // each tRes.m_dResults has proper tag and corresponding docstore pointer in random order
  3436. // and we have dOrd wich enumerates them in descending tag order
  3437. SortTagsAndDocstores ( tRes, dOrd );
  3438. return iDup;
  3439. }
  3440. void RecoverAggregateFunctions ( const CSphQuery & tQuery, const AggrResult_t & tRes )
  3441. {
  3442. for ( const auto& tItem : tQuery.m_dItems )
  3443. {
  3444. if ( tItem.m_eAggrFunc==SPH_AGGR_NONE )
  3445. continue;
  3446. for ( int j = 0, iAttrsCount = tRes.m_tSchema.GetAttrsCount (); j<iAttrsCount; ++j )
  3447. {
  3448. auto & tCol = const_cast<CSphColumnInfo&> ( tRes.m_tSchema.GetAttr(j) );
  3449. if ( tCol.m_sName==tItem.m_sAlias )
  3450. {
  3451. assert ( tCol.m_eAggrFunc==SPH_AGGR_NONE );
  3452. tCol.m_eAggrFunc = tItem.m_eAggrFunc;
  3453. }
  3454. }
  3455. }
  3456. }
  3457. struct GenericMatchSort_fn : public CSphMatchComparatorState
  3458. {
  3459. bool IsLess ( const CSphMatch * a, const CSphMatch * b ) const
  3460. {
  3461. for ( int i=0; i<CSphMatchComparatorState::MAX_ATTRS; i++ )
  3462. switch ( m_eKeypart[i] )
  3463. {
  3464. case SPH_KEYPART_ROWID:
  3465. if ( a->m_tRowID==b->m_tRowID )
  3466. continue;
  3467. return ( ( m_uAttrDesc>>i ) & 1 ) ^ ( a->m_tRowID < b->m_tRowID );
  3468. case SPH_KEYPART_WEIGHT:
  3469. if ( a->m_iWeight==b->m_iWeight )
  3470. continue;
  3471. return ( ( m_uAttrDesc>>i ) & 1 ) ^ ( a->m_iWeight < b->m_iWeight );
  3472. case SPH_KEYPART_INT:
  3473. {
  3474. register SphAttr_t aa = a->GetAttr ( m_tLocator[i] );
  3475. register SphAttr_t bb = b->GetAttr ( m_tLocator[i] );
  3476. if ( aa==bb )
  3477. continue;
  3478. return ( ( m_uAttrDesc>>i ) & 1 ) ^ ( aa < bb );
  3479. }
  3480. case SPH_KEYPART_FLOAT:
  3481. {
  3482. register float aa = a->GetAttrFloat ( m_tLocator[i] );
  3483. register float bb = b->GetAttrFloat ( m_tLocator[i] );
  3484. if ( aa==bb )
  3485. continue;
  3486. return ( ( m_uAttrDesc>>i ) & 1 ) ^ ( aa < bb );
  3487. }
  3488. case SPH_KEYPART_DOUBLE:
  3489. {
  3490. register double aa = a->GetAttrDouble ( m_tLocator[i] );
  3491. register double bb = b->GetAttrDouble ( m_tLocator[i] );
  3492. if ( aa==bb )
  3493. continue;
  3494. return ( ( m_uAttrDesc>>i ) & 1 ) ^ ( aa < bb );
  3495. }
  3496. case SPH_KEYPART_STRINGPTR:
  3497. case SPH_KEYPART_STRING:
  3498. {
  3499. int iCmp = CmpStrings ( *a, *b, i );
  3500. if ( iCmp!=0 )
  3501. return ( ( m_uAttrDesc>>i ) & 1 ) ^ ( iCmp < 0 );
  3502. break;
  3503. }
  3504. }
  3505. return a->m_tRowID<b->m_tRowID;
  3506. }
  3507. };
  3508. /// returns internal magic names for expressions like COUNT(*) that have a corresponding one
  3509. /// returns expression itself otherwise
  3510. const char * GetMagicSchemaName ( const CSphString & s )
  3511. {
  3512. if ( s=="count(*)" )
  3513. return "@count";
  3514. if ( s=="weight()" )
  3515. return "@weight";
  3516. if ( s=="groupby()" )
  3517. return "@groupby";
  3518. return s.cstr();
  3519. }
  3520. /// a functor to sort columns by (is_aggregate ASC, column_index ASC)
  3521. struct AggregateColumnSort_fn
  3522. {
  3523. bool IsAggr ( const CSphColumnInfo & c ) const
  3524. {
  3525. return c.m_eAggrFunc!=SPH_AGGR_NONE
  3526. || c.m_sName=="@groupby"
  3527. || c.m_sName=="@count"
  3528. || c.m_sName=="@distinct"
  3529. || IsSortJsonInternal ( c.m_sName );
  3530. }
  3531. bool IsLess ( const CSphColumnInfo & a, const CSphColumnInfo & b ) const
  3532. {
  3533. bool aa = IsAggr(a);
  3534. bool bb = IsAggr(b);
  3535. if ( aa!=bb )
  3536. return aa < bb;
  3537. return a.m_iIndex < b.m_iIndex;
  3538. }
  3539. };
  3540. void ExtractPostlimit ( const ISphSchema & tSchema, bool bMaster, CSphVector<const CSphColumnInfo *> & dPostlimit )
  3541. {
  3542. for ( int i=0; i<tSchema.GetAttrsCount(); ++i )
  3543. {
  3544. const CSphColumnInfo & tCol = tSchema.GetAttr ( i );
  3545. if ( tCol.m_eStage==SPH_EVAL_POSTLIMIT && ( bMaster || tCol.m_uFieldFlags==CSphColumnInfo::FIELD_NONE ) )
  3546. dPostlimit.Add ( &tCol );
  3547. }
  3548. }
  3549. // for single chunk of matches return list of tags with docstores
  3550. CSphVector<int> GetUniqueTagsWithDocstores ( const AggrResult_t & tRes, int iOff, int iLim )
  3551. {
  3552. assert ( tRes.m_bTagsCompacted );
  3553. assert ( tRes.m_bSingle );
  3554. CSphVector<bool> dBoolTags;
  3555. dBoolTags.Resize ( tRes.m_dResults.GetLength() );
  3556. dBoolTags.ZeroVec();
  3557. auto dMatches = tRes.m_dResults.First ().m_dMatches.Slice ( iOff, iLim );
  3558. for ( const auto& dMatch : dMatches )
  3559. {
  3560. assert ( dMatch.m_iTag < tRes.m_dResults.GetLength() );
  3561. if ( tRes.m_dResults[dMatch.m_iTag].Docstore() )
  3562. dBoolTags[dMatch.m_iTag] = true;
  3563. }
  3564. CSphVector<int> dTags;
  3565. ARRAY_CONSTFOREACH( i, dBoolTags )
  3566. if ( dBoolTags[i] )
  3567. dTags.Add(i);
  3568. return dTags;
  3569. }
  3570. void SetupPostlimitExprs ( const DocstoreReader_i * pDocstore, const CSphColumnInfo * pCol, const char * sQuery, int64_t iDocstoreSessionId )
  3571. {
  3572. DocstoreSession_c::InfoDocID_t tSessionInfo;
  3573. tSessionInfo.m_pDocstore = pDocstore;
  3574. tSessionInfo.m_iSessionId = iDocstoreSessionId;
  3575. assert ( pCol && pCol->m_pExpr );
  3576. pCol->m_pExpr->Command ( SPH_EXPR_SET_DOCSTORE_DOCID, &tSessionInfo ); // value is copied; no leak of pointer to local here.
  3577. pCol->m_pExpr->Command ( SPH_EXPR_SET_QUERY, (void *)sQuery);
  3578. }
  3579. void EvalPostlimitExprs ( CSphMatch & tMatch, const CSphColumnInfo * pCol )
  3580. {
  3581. assert ( pCol && pCol->m_pExpr );
  3582. switch ( pCol->m_eAttrType )
  3583. {
  3584. case SPH_ATTR_TIMESTAMP:
  3585. case SPH_ATTR_INTEGER:
  3586. case SPH_ATTR_BOOL:
  3587. tMatch.SetAttr ( pCol->m_tLocator, pCol->m_pExpr->IntEval ( tMatch ) );
  3588. break;
  3589. case SPH_ATTR_BIGINT:
  3590. tMatch.SetAttr ( pCol->m_tLocator, pCol->m_pExpr->Int64Eval ( tMatch ) );
  3591. break;
  3592. case SPH_ATTR_STRINGPTR:
  3593. // FIXME! a potential leak of *previous* value?
  3594. tMatch.SetAttr ( pCol->m_tLocator, (SphAttr_t) pCol->m_pExpr->StringEvalPacked ( tMatch ) );
  3595. break;
  3596. default:
  3597. tMatch.SetAttrFloat ( pCol->m_tLocator, pCol->m_pExpr->Eval ( tMatch ) );
  3598. break;
  3599. }
  3600. }
  3601. // single resultset cunk, but has many tags
  3602. void ProcessMultiPostlimit ( AggrResult_t & tRes, VecTraits_T<const CSphColumnInfo *> & dPostlimit, const char * sQuery, int iOff, int iLim )
  3603. {
  3604. if ( dPostlimit.IsEmpty() )
  3605. return;
  3606. assert ( tRes.m_bSingle );
  3607. assert ( tRes.m_bOneSchema );
  3608. assert ( tRes.m_bTagsAssigned );
  3609. assert ( tRes.m_bTagsCompacted );
  3610. assert ( tRes.m_bIdxByTag );
  3611. // collect unique tags from matches
  3612. CSphVector<int> dDocstoreTags = GetUniqueTagsWithDocstores ( tRes, iOff, iLim );
  3613. // generates docstore session id
  3614. DocstoreSession_c tSession;
  3615. auto iSessionUID = tSession.GetUID();
  3616. // spawn buffered readers for the current session
  3617. // put them to a global hash
  3618. for ( int iTag : dDocstoreTags )
  3619. tRes.m_dResults[iTag].m_pDocstore->CreateReader ( iSessionUID );
  3620. int iLastTag = -1;
  3621. auto dMatches = tRes.m_dResults.First ().m_dMatches.Slice ( iOff, iLim );
  3622. for ( auto & dMatch : dMatches )
  3623. {
  3624. int iTag = dMatch.m_iTag;
  3625. if ( tRes.m_dResults[iTag].m_bTag )
  3626. continue; // remote match; everything should be precalculated
  3627. auto * pDocstore = tRes.m_dResults[iTag].Docstore ();
  3628. assert ( iTag<tRes.m_dResults.GetLength () );
  3629. if ( iTag!=iLastTag )
  3630. {
  3631. for ( const auto & pCol : dPostlimit )
  3632. SetupPostlimitExprs ( pDocstore, pCol, sQuery, iSessionUID );
  3633. iLastTag = iTag;
  3634. }
  3635. for ( const auto & pCol : dPostlimit )
  3636. EvalPostlimitExprs ( dMatch, pCol );
  3637. }
  3638. }
  3639. void ProcessSinglePostlimit ( OneResultset_t & tRes, VecTraits_T<const CSphColumnInfo *> & dPostlimit, const char * sQuery, int iOff, int iLim )
  3640. {
  3641. auto dMatches = tRes.m_dMatches.Slice ( iOff, iLim );
  3642. if ( dMatches.IsEmpty() )
  3643. return;
  3644. // generates docstore session id
  3645. DocstoreSession_c tSession;
  3646. auto iSessionUID = tSession.GetUID();
  3647. // spawn buffered reader for the current session
  3648. // put it to a global hash
  3649. if ( tRes.Docstore () )
  3650. tRes.m_pDocstore->CreateReader ( iSessionUID );
  3651. for ( const auto & pCol : dPostlimit )
  3652. SetupPostlimitExprs ( tRes.Docstore (), pCol, sQuery, iSessionUID );
  3653. for ( auto & tMatch : dMatches )
  3654. for ( const auto & pCol : dPostlimit )
  3655. EvalPostlimitExprs ( tMatch, pCol );
  3656. }
  3657. void ProcessLocalPostlimit ( AggrResult_t & tRes, const CSphQuery & tQuery, bool bMaster )
  3658. {
  3659. assert ( !tRes.m_bOneSchema );
  3660. assert ( !tRes.m_bSingle );
  3661. bool bGotPostlimit = false;
  3662. for ( int i = 0, iAttrsCount = tRes.m_tSchema.GetAttrsCount (); i<iAttrsCount && !bGotPostlimit; ++i )
  3663. {
  3664. const CSphColumnInfo & tCol = tRes.m_tSchema.GetAttr(i);
  3665. bGotPostlimit = ( tCol.m_eStage==SPH_EVAL_POSTLIMIT && ( bMaster || tCol.m_uFieldFlags==CSphColumnInfo::FIELD_NONE ) );
  3666. }
  3667. if ( !bGotPostlimit )
  3668. return;
  3669. int iLimit = ( tQuery.m_iOuterLimit ? tQuery.m_iOuterLimit : tQuery.m_iLimit );
  3670. iLimit += Max ( tQuery.m_iOffset, tQuery.m_iOuterOffset );
  3671. CSphVector<const CSphColumnInfo *> dPostlimit;
  3672. for ( auto & tResult : tRes.m_dResults )
  3673. {
  3674. dPostlimit.Resize ( 0 );
  3675. ExtractPostlimit ( tResult.m_tSchema, bMaster, dPostlimit );
  3676. if ( dPostlimit.IsEmpty () )
  3677. continue;
  3678. iLimit = ( tQuery.m_iOuterLimit ? tQuery.m_iOuterLimit : tQuery.m_iLimit );
  3679. // we can't estimate limit.offset per result set
  3680. // as matches got merged and sort next step
  3681. if ( !tResult.m_bTag )
  3682. ProcessSinglePostlimit ( tResult, dPostlimit, tQuery.m_sQuery.cstr (), 0, iLimit );
  3683. }
  3684. }
  3685. bool MinimizeSchemas ( AggrResult_t & tRes )
  3686. {
  3687. bool bAllEqual = true;
  3688. bool bSchemaBaseSet = false;
  3689. auto iResults = tRes.m_dResults.GetLength();
  3690. for ( int i=0; i<iResults; ++i )
  3691. {
  3692. // skip empty result set
  3693. if ( !tRes.m_dResults[i].m_dMatches.GetLength() )
  3694. continue;
  3695. // set base schema only from non-empty result set
  3696. if ( !bSchemaBaseSet )
  3697. {
  3698. bSchemaBaseSet = true;
  3699. tRes.m_tSchema = tRes.m_dResults[i].m_tSchema;
  3700. continue;
  3701. }
  3702. if ( !MinimizeSchema ( tRes.m_tSchema, tRes.m_dResults[i].m_tSchema ) )
  3703. bAllEqual = false;
  3704. }
  3705. if ( !bSchemaBaseSet && iResults>0 )
  3706. tRes.m_tSchema = tRes.m_dResults[0].m_tSchema;
  3707. return bAllEqual;
  3708. }
  3709. //////////////////////////////////////////////////////////////////////////
  3710. class FrontendSchemaBuilder_c
  3711. {
  3712. public:
  3713. FrontendSchemaBuilder_c ( const AggrResult_t & tRes, const CSphQuery & tQuery, const CSphVector<CSphQueryItem> & dItems, const CSphVector<CSphQueryItem> & dQueryItems,
  3714. const sph::StringSet & hExtraColumns, bool bQueryFromAPI, bool bHaveLocals );
  3715. void CollectKnownItems();
  3716. void AddAttrs();
  3717. bool CheckUnmapped ( CSphString & sError ) const;
  3718. void Finalize();
  3719. void RemapGroupBy();
  3720. void RemapFacets();
  3721. void SwapAttrs ( CSphSchema & tSchema );
  3722. private:
  3723. const AggrResult_t & m_tRes;
  3724. const CSphQuery & m_tQuery;
  3725. const CSphVector<CSphQueryItem> & m_dItems;
  3726. const CSphVector<CSphQueryItem> & m_dQueryItems;
  3727. const sph::StringSet & m_hExtraColumns;
  3728. bool m_bQueryFromAPI;
  3729. bool m_bHaveLocals;
  3730. bool m_bAgent;
  3731. CSphVector<CSphColumnInfo> m_dFrontend;
  3732. CSphVector<int> m_dKnownAttrs;
  3733. CSphVector<int> m_dUnmappedAttrs;
  3734. };
  3735. FrontendSchemaBuilder_c::FrontendSchemaBuilder_c ( const AggrResult_t & tRes, const CSphQuery & tQuery, const CSphVector<CSphQueryItem> & dItems, const CSphVector<CSphQueryItem> & dQueryItems,
  3736. const sph::StringSet & hExtraColumns, bool bQueryFromAPI, bool bHaveLocals )
  3737. : m_tRes ( tRes )
  3738. , m_tQuery ( tQuery )
  3739. , m_dItems ( dItems )
  3740. , m_dQueryItems ( dQueryItems )
  3741. , m_hExtraColumns ( hExtraColumns )
  3742. , m_bQueryFromAPI ( bQueryFromAPI )
  3743. , m_bHaveLocals ( bHaveLocals )
  3744. , m_bAgent ( tQuery.m_bAgent )
  3745. {
  3746. m_dFrontend.Resize(dItems.GetLength());
  3747. }
  3748. void FrontendSchemaBuilder_c::CollectKnownItems()
  3749. {
  3750. ARRAY_CONSTFOREACH ( i, m_dItems )
  3751. {
  3752. const CSphQueryItem & tItem = m_dItems[i];
  3753. int iCol = -1;
  3754. if ( !m_bQueryFromAPI && tItem.m_sAlias.IsEmpty() )
  3755. iCol = m_tRes.m_tSchema.GetAttrIndex ( tItem.m_sExpr.cstr() );
  3756. if ( iCol>=0 )
  3757. {
  3758. m_dKnownAttrs.Add(i);
  3759. m_dFrontend[i].m_sName = tItem.m_sExpr;
  3760. m_dFrontend[i].m_iIndex = iCol;
  3761. }
  3762. else
  3763. m_dUnmappedAttrs.Add(i);
  3764. }
  3765. }
  3766. void FrontendSchemaBuilder_c::AddAttrs()
  3767. {
  3768. bool bUsualApi = !m_bAgent && m_bQueryFromAPI;
  3769. for ( int iCol=0; iCol<m_tRes.m_tSchema.GetAttrsCount(); ++iCol )
  3770. {
  3771. const CSphColumnInfo & tCol = m_tRes.m_tSchema.GetAttr(iCol);
  3772. assert ( !tCol.m_sName.IsEmpty() );
  3773. bool bMagic = ( *tCol.m_sName.cstr()=='@' );
  3774. if ( !bMagic && tCol.m_pExpr )
  3775. {
  3776. ARRAY_FOREACH ( j, m_dUnmappedAttrs )
  3777. if ( m_dItems[ m_dUnmappedAttrs[j] ].m_sAlias==tCol.m_sName )
  3778. {
  3779. int k = m_dUnmappedAttrs[j];
  3780. m_dFrontend[k].m_iIndex = iCol;
  3781. m_dFrontend[k].m_sName = m_dItems[k].m_sAlias;
  3782. m_dKnownAttrs.Add(k);
  3783. m_dUnmappedAttrs.Remove ( j-- ); // do not skip an element next to removed one!
  3784. }
  3785. // FIXME?
  3786. // really not sure if this is the right thing to do
  3787. // but it fixes a couple queries in test_163 in compaitbility mode
  3788. if ( m_bAgent && !m_dFrontend.Contains ( bind ( &CSphColumnInfo::m_sName ), tCol.m_sName ) )
  3789. {
  3790. CSphColumnInfo & t = m_dFrontend.Add();
  3791. t.m_iIndex = iCol;
  3792. t.m_sName = tCol.m_sName;
  3793. }
  3794. } else if ( bMagic && ( tCol.m_pExpr || bUsualApi ) )
  3795. {
  3796. ARRAY_FOREACH ( j, m_dUnmappedAttrs )
  3797. if ( tCol.m_sName==GetMagicSchemaName ( m_dItems[ m_dUnmappedAttrs[j] ].m_sExpr ) )
  3798. {
  3799. int k = m_dUnmappedAttrs[j];
  3800. m_dFrontend[k].m_iIndex = iCol;
  3801. m_dFrontend[k].m_sName = m_dItems[k].m_sAlias;
  3802. m_dKnownAttrs.Add(k);
  3803. m_dUnmappedAttrs.Remove ( j-- ); // do not skip an element next to removed one!
  3804. }
  3805. if ( !m_dFrontend.Contains ( bind ( &CSphColumnInfo::m_sName ), tCol.m_sName ) )
  3806. {
  3807. CSphColumnInfo & t = m_dFrontend.Add();
  3808. t.m_iIndex = iCol;
  3809. t.m_sName = tCol.m_sName;
  3810. }
  3811. } else
  3812. {
  3813. bool bAdded = false;
  3814. ARRAY_FOREACH ( j, m_dUnmappedAttrs )
  3815. {
  3816. int k = m_dUnmappedAttrs[j];
  3817. const CSphQueryItem & t = m_dItems[k];
  3818. if ( ( tCol.m_sName==GetMagicSchemaName ( t.m_sExpr ) && t.m_eAggrFunc==SPH_AGGR_NONE )
  3819. || ( t.m_sAlias==tCol.m_sName &&
  3820. ( m_tRes.m_tSchema.GetAttrIndex ( GetMagicSchemaName ( t.m_sExpr ) )==-1 || t.m_eAggrFunc!=SPH_AGGR_NONE ) ) )
  3821. {
  3822. // tricky bit about naming
  3823. //
  3824. // in master mode, we can just use the alias or expression or whatever
  3825. // the data will be fetched using the locator anyway, column name does not matter anymore
  3826. //
  3827. // in agent mode, however, we need to keep the original column names in our response
  3828. // otherwise, queries like SELECT col1 c, count(*) c FROM dist will fail on master
  3829. // because it won't be able to identify the count(*) aggregate by its name
  3830. m_dFrontend[k].m_iIndex = iCol;
  3831. m_dFrontend[k].m_sName = m_bAgent
  3832. ? tCol.m_sName
  3833. : ( m_dItems[k].m_sAlias.IsEmpty()
  3834. ? m_dItems[k].m_sExpr
  3835. : m_dItems[k].m_sAlias );
  3836. m_dKnownAttrs.Add(k);
  3837. bAdded = true;
  3838. m_dUnmappedAttrs.Remove ( j-- ); // do not skip an element next to removed one!
  3839. }
  3840. }
  3841. // column was not found in the select list directly
  3842. // however we might need it anyway because of a non-NULL extra-schema
  3843. // (extra-schema is additional set of columns came from right side of query
  3844. // when you perform 'select a from index order by b', the 'b' is not displayed, but need for sorting,
  3845. // so extra-schema in the case will contain 'b').
  3846. // bMagic condition added for @groupbystr in the agent mode
  3847. if ( !bAdded && m_bAgent && ( m_hExtraColumns[tCol.m_sName] || !m_bHaveLocals || bMagic ) )
  3848. {
  3849. CSphColumnInfo & t = m_dFrontend.Add();
  3850. t.m_iIndex = iCol;
  3851. t.m_sName = tCol.m_sName;
  3852. }
  3853. }
  3854. }
  3855. m_dKnownAttrs.Sort();
  3856. }
  3857. bool FrontendSchemaBuilder_c::CheckUnmapped ( CSphString & sError ) const
  3858. {
  3859. // sanity check
  3860. // verify that we actually have all the queried select items
  3861. assert ( m_dUnmappedAttrs.IsEmpty() || ( m_dUnmappedAttrs.GetLength()==1 && m_dItems [ m_dUnmappedAttrs[0] ].m_sExpr=="id" ) );
  3862. ARRAY_CONSTFOREACH ( i, m_dItems )
  3863. {
  3864. const CSphQueryItem & tItem = m_dItems[i];
  3865. if ( !m_dKnownAttrs.BinarySearch(i) && tItem.m_sExpr!="id" )
  3866. {
  3867. sError.SetSprintf ( "internal error: column '%s/%s' not found in result set schema", tItem.m_sExpr.cstr(), tItem.m_sAlias.cstr() );
  3868. return false;
  3869. }
  3870. }
  3871. return true;
  3872. }
  3873. void FrontendSchemaBuilder_c::Finalize()
  3874. {
  3875. // finalize the frontend schema columns
  3876. // we kept indexes into internal schema there, now use them to lookup and copy column data
  3877. ARRAY_CONSTFOREACH ( i, m_dFrontend )
  3878. {
  3879. CSphColumnInfo & tFrontend = m_dFrontend[i];
  3880. const CSphColumnInfo & s = m_tRes.m_tSchema.GetAttr ( tFrontend.m_iIndex );
  3881. tFrontend.m_tLocator = s.m_tLocator;
  3882. tFrontend.m_eAttrType = s.m_eAttrType;
  3883. tFrontend.m_eAggrFunc = s.m_eAggrFunc; // for a sort loop just below
  3884. tFrontend.m_iIndex = i; // to make the aggr sort loop just below stable
  3885. tFrontend.m_uFieldFlags = s.m_uFieldFlags;
  3886. }
  3887. // tricky bit
  3888. // in agents only, push aggregated columns, if any, to the end
  3889. // for that, sort the schema by (is_aggregate ASC, column_index ASC)
  3890. if ( m_bAgent )
  3891. m_dFrontend.Sort ( AggregateColumnSort_fn() );
  3892. }
  3893. void FrontendSchemaBuilder_c::RemapGroupBy()
  3894. {
  3895. // remap groupby() and aliased groupby() to @groupbystr or string attribute
  3896. const CSphColumnInfo * p = nullptr;
  3897. CSphString sJsonGroupBy;
  3898. if ( sphJsonNameSplit ( m_tQuery.m_sGroupBy.cstr() ) )
  3899. {
  3900. sJsonGroupBy = SortJsonInternalSet ( m_tQuery.m_sGroupBy );
  3901. p = m_tRes.m_tSchema.GetAttr ( sJsonGroupBy.cstr() );
  3902. }
  3903. if ( !p )
  3904. {
  3905. // try string attribute (multiple group-by still displays hashes)
  3906. if ( !m_tQuery.m_sGroupBy.IsEmpty() )
  3907. {
  3908. p = m_tRes.m_tSchema.GetAttr ( m_tQuery.m_sGroupBy.cstr() );
  3909. if ( p )
  3910. {
  3911. if ( p->m_eAttrType==SPH_ATTR_JSON_PTR )
  3912. {
  3913. sJsonGroupBy = SortJsonInternalSet ( m_tQuery.m_sGroupBy );
  3914. p = m_tRes.m_tSchema.GetAttr ( sJsonGroupBy.cstr() );
  3915. } else if ( p->m_eAttrType!=SPH_ATTR_STRINGPTR )
  3916. {
  3917. p = nullptr;
  3918. }
  3919. }
  3920. }
  3921. if ( !p )
  3922. return;
  3923. }
  3924. for ( auto & tFrontend : m_dFrontend )
  3925. if ( tFrontend.m_sName=="groupby()" )
  3926. {
  3927. tFrontend.m_tLocator = p->m_tLocator;
  3928. tFrontend.m_eAttrType = p->m_eAttrType;
  3929. tFrontend.m_eAggrFunc = p->m_eAggrFunc;
  3930. }
  3931. // check aliases too
  3932. for ( const auto & tQueryItem : m_dQueryItems )
  3933. {
  3934. if ( tQueryItem.m_sExpr!="groupby()" )
  3935. continue;
  3936. for ( auto & tFrontend : m_dFrontend )
  3937. if ( tFrontend.m_sName==tQueryItem.m_sAlias )
  3938. {
  3939. tFrontend.m_tLocator = p->m_tLocator;
  3940. tFrontend.m_eAttrType = p->m_eAttrType;
  3941. tFrontend.m_eAggrFunc = p->m_eAggrFunc;
  3942. }
  3943. }
  3944. }
  3945. void FrontendSchemaBuilder_c::RemapFacets()
  3946. {
  3947. // facets
  3948. if ( !m_tQuery.m_bFacet && !m_tQuery.m_bFacetHead )
  3949. return;
  3950. // remap MVA/JSON column to @groupby/@groupbystr in facet queries
  3951. const CSphColumnInfo * pGroupByCol = nullptr;
  3952. CSphString sJsonGroupBy;
  3953. if ( sphJsonNameSplit ( m_tQuery.m_sGroupBy.cstr() ) )
  3954. {
  3955. sJsonGroupBy = SortJsonInternalSet ( m_tQuery.m_sGroupBy );
  3956. pGroupByCol = m_tRes.m_tSchema.GetAttr ( sJsonGroupBy.cstr() );
  3957. }
  3958. if ( !pGroupByCol )
  3959. {
  3960. pGroupByCol = m_tRes.m_tSchema.GetAttr ( "@groupby" );
  3961. if ( !pGroupByCol )
  3962. return;
  3963. }
  3964. for ( auto & tFrontend : m_dFrontend )
  3965. {
  3966. ESphAttr eAttr = tFrontend.m_eAttrType;
  3967. // checking _PTR attrs only because we should not have and non-ptr attr at this point
  3968. if ( m_tQuery.m_sGroupBy==tFrontend.m_sName && ( eAttr==SPH_ATTR_UINT32SET_PTR || eAttr==SPH_ATTR_INT64SET_PTR || eAttr==SPH_ATTR_JSON_FIELD_PTR ) )
  3969. {
  3970. tFrontend.m_tLocator = pGroupByCol->m_tLocator;
  3971. tFrontend.m_eAttrType = pGroupByCol->m_eAttrType;
  3972. tFrontend.m_eAggrFunc = pGroupByCol->m_eAggrFunc;
  3973. }
  3974. }
  3975. }
  3976. void FrontendSchemaBuilder_c::SwapAttrs ( CSphSchema & tSchema )
  3977. {
  3978. tSchema.SwapAttrs ( m_dFrontend );
  3979. }
  3980. //////////////////////////////////////////////////////////////////////////
  3981. bool MergeAllMatches ( AggrResult_t & tRes, const CSphQuery & tQuery, bool bHaveLocals, bool bAllEqual, bool bMaster, const CSphFilterSettings * pAggrFilter, QueryProfile_c * pProfiler )
  3982. {
  3983. ESphSortOrder eQuerySort = ( tQuery.m_sOuterOrderBy.IsEmpty() ? SPH_SORT_RELEVANCE : SPH_SORT_EXTENDED );
  3984. CSphQuery tQueryCopy = tQuery;
  3985. // got outer order? gotta do a couple things
  3986. if ( tQueryCopy.m_bHasOuter )
  3987. {
  3988. // first, temporarily patch up sorting clause and max_matches (we will restore them later)
  3989. Swap ( tQueryCopy.m_sOuterOrderBy, tQueryCopy.m_sGroupBy.IsEmpty() ? tQueryCopy.m_sSortBy : tQueryCopy.m_sGroupSortBy );
  3990. Swap ( eQuerySort, tQueryCopy.m_eSort );
  3991. // second, apply inner limit now, before (!) reordering
  3992. for ( auto & tResult : tRes.m_dResults )
  3993. tResult.ClampMatches ( tQueryCopy.m_iLimit );
  3994. }
  3995. // so we need to bring matches to the schema that the *sorter* wants
  3996. // so we need to create the sorter before conversion
  3997. //
  3998. // create queue
  3999. // at this point, we do not need to compute anything; it all must be here
  4000. SphQueueSettings_t tQueueSettings ( tRes.m_tSchema );
  4001. tQueueSettings.m_pAggrFilter = pAggrFilter;
  4002. // FIXME? probably not right; 20 shards with by 300 matches might be too much
  4003. // but propagating too small inner max_matches to the outer is not right either
  4004. if ( tQueryCopy.m_bHasOuter )
  4005. tQueueSettings.m_iMaxMatches = Min ( tQuery.m_iMaxMatches * tRes.m_dResults.GetLength(), tRes.GetLength() );
  4006. else
  4007. tQueueSettings.m_iMaxMatches = Min ( tQuery.m_iMaxMatches, tRes.GetLength() );
  4008. tQueueSettings.m_iMaxMatches = Max ( tQueueSettings.m_iMaxMatches, 1 );
  4009. SphQueueRes_t tQueueRes;
  4010. std::unique_ptr<ISphMatchSorter> pSorter ( sphCreateQueue ( tQueueSettings, tQueryCopy, tRes.m_sError, tQueueRes ) );
  4011. // restore outer order related patches, or it screws up the query log
  4012. if ( tQueryCopy.m_bHasOuter )
  4013. {
  4014. Swap ( tQueryCopy.m_sOuterOrderBy, tQueryCopy.m_sGroupBy.IsEmpty() ? tQueryCopy.m_sSortBy : tQueryCopy.m_sGroupSortBy );
  4015. Swap ( eQuerySort, tQueryCopy.m_eSort );
  4016. }
  4017. if ( !pSorter )
  4018. return false;
  4019. pSorter->SetMerge(true);
  4020. // reset bAllEqual flag if sorter makes new attributes
  4021. if ( bAllEqual )
  4022. {
  4023. // at first we count already existed internal attributes
  4024. // then check if sorter makes more
  4025. int iRemapCount = GetStringRemapCount ( tRes.m_tSchema, tRes.m_tSchema );
  4026. int iNewCount = GetStringRemapCount ( *pSorter->GetSchema(), tRes.m_tSchema );
  4027. bAllEqual = ( iNewCount<=iRemapCount );
  4028. }
  4029. // sorter expects this
  4030. // just doing tRes.m_tSchema = *pSorter->GetSchema() won't work here
  4031. // because pSorter->GetSchema() may already contain a pointer to tRes.m_tSchema as m_pIndexSchema
  4032. // that's why we explicitly copy a CSphRsetSchema to a plain CSphSchema and move it to tRes.m_tSchema
  4033. {
  4034. CSphSchema tSchemaCopy;
  4035. tSchemaCopy = *pSorter->GetSchema();
  4036. tRes.m_tSchema.Swap ( tSchemaCopy );
  4037. }
  4038. // convert all matches to sorter schema - at least to manage all static to dynamic
  4039. if ( !bAllEqual )
  4040. {
  4041. // post-limit stuff first
  4042. if ( bHaveLocals )
  4043. {
  4044. CSphScopedProfile tProf ( pProfiler, SPH_QSTATE_EVAL_POST );
  4045. ProcessLocalPostlimit ( tRes, tQueryCopy, bMaster );
  4046. }
  4047. RemapResult ( tRes );
  4048. }
  4049. // do the sort work!
  4050. tRes.m_iTotalMatches -= KillDupesAndFlatten ( pSorter.get(), tRes );
  4051. return true;
  4052. }
  4053. bool ApplyOuterOrder ( AggrResult_t & tRes, const CSphQuery & tQuery )
  4054. {
  4055. assert ( !tRes.m_dResults.IsEmpty() );
  4056. // reorder (aka outer order)
  4057. ESphSortFunc eFunc;
  4058. GenericMatchSort_fn tReorder;
  4059. CSphVector<ExtraSortExpr_t> dExtraExprs;
  4060. ESortClauseParseResult eRes = sphParseSortClause ( tQuery, tQuery.m_sOuterOrderBy.cstr(), tRes.m_tSchema, eFunc, tReorder, dExtraExprs, true, tRes.m_sError );
  4061. if ( eRes==SORT_CLAUSE_RANDOM )
  4062. tRes.m_sError = "order by rand() not supported in outer select";
  4063. if ( eRes!=SORT_CLAUSE_OK )
  4064. return false;
  4065. assert ( eFunc==FUNC_GENERIC1 ||eFunc==FUNC_GENERIC2 || eFunc==FUNC_GENERIC3 || eFunc==FUNC_GENERIC4 || eFunc==FUNC_GENERIC5 );
  4066. auto& dMatches = tRes.m_dResults.First().m_dMatches;
  4067. sphSort ( dMatches.Begin(), dMatches.GetLength(), tReorder, MatchSortAccessor_t() );
  4068. return true;
  4069. }
  4070. void ComputePostlimit ( AggrResult_t & tRes, const CSphQuery & tQuery, bool bMaster )
  4071. {
  4072. assert ( tRes.m_bSingle );
  4073. assert ( tRes.m_bOneSchema );
  4074. assert ( !tRes.m_dResults.IsEmpty () );
  4075. CSphVector<const CSphColumnInfo *> dPostlimit;
  4076. ExtractPostlimit ( tRes.m_tSchema, bMaster, dPostlimit );
  4077. // post compute matches only between offset..limit
  4078. // however at agent we can't estimate limit.offset at master merged result set
  4079. // but master don't provide offset to agents only offset+limit as limit
  4080. // so computing all matches from 0 up to inner.limit/outer.limit
  4081. assert ( tRes.GetLength ()==tRes.m_dResults.First().m_dMatches.GetLength() );
  4082. int iOff = Max ( tQuery.m_iOffset, tQuery.m_iOuterOffset );
  4083. int iLimit = ( tQuery.m_iOuterLimit ? tQuery.m_iOuterLimit : tQuery.m_iLimit );
  4084. if ( tRes.m_bTagsAssigned )
  4085. ProcessMultiPostlimit ( tRes, dPostlimit, tQuery.m_sQuery.cstr (), iOff, iLimit );
  4086. else
  4087. ProcessSinglePostlimit ( tRes.m_dResults.First(), dPostlimit, tQuery.m_sQuery.cstr(), iOff, iLimit );
  4088. }
  4089. int64_t CalcPredictedTimeMsec ( const CSphQueryResultMeta & tMeta )
  4090. {
  4091. assert ( tMeta.m_bHasPrediction );
  4092. int64_t iNanoResult = int64_t(g_iPredictorCostSkip)* tMeta.m_tStats.m_iSkips
  4093. + g_iPredictorCostDoc * tMeta.m_tStats.m_iFetchedDocs
  4094. + g_iPredictorCostHit * tMeta.m_tStats.m_iFetchedHits
  4095. + g_iPredictorCostMatch * tMeta.m_iTotalMatches;
  4096. return iNanoResult/1000000;
  4097. }
  4098. int GetMaxMatches ( int iQueryMaxMatches, const CSphIndex * pIndex )
  4099. {
  4100. if ( iQueryMaxMatches<=DEFAULT_MAX_MATCHES )
  4101. return iQueryMaxMatches;
  4102. int64_t iDocs = Min ( (int)INT_MAX, pIndex->GetStats().m_iTotalDocuments ); // clamp to int max
  4103. return Min ( iQueryMaxMatches, Max ( iDocs, DEFAULT_MAX_MATCHES ) ); // do not want 0 sorter and sorter longer than query.max_matches
  4104. }
  4105. } // namespace static
  4106. /// merges multiple result sets, remaps columns, does reorder for outer selects
  4107. bool MinimizeAggrResult ( AggrResult_t & tRes, const CSphQuery & tQuery, bool bHaveLocals, const sph::StringSet & hExtraColumns, QueryProfile_c * pProfiler, const CSphFilterSettings * pAggrFilter, bool bForceRefItems, bool bMaster )
  4108. {
  4109. bool bReturnZeroCount = !tRes.m_dZeroCount.IsEmpty();
  4110. bool bQueryFromAPI = tQuery.m_eQueryType==QUERY_API;
  4111. // 0 matches via SphinxAPI? no fiddling with schemes is necessary
  4112. // (and via SphinxQL, we still need to return the right schema)
  4113. // 0 result set schemes via SphinxQL? just bail
  4114. if ( tRes.IsEmpty() && ( bQueryFromAPI || !bReturnZeroCount ) )
  4115. {
  4116. Debug ( tRes.m_bSingle = true; )
  4117. if ( !tRes.m_dResults.IsEmpty () )
  4118. {
  4119. tRes.m_tSchema = tRes.m_dResults.First ().m_tSchema;
  4120. Debug( tRes.m_bOneSchema = true; )
  4121. }
  4122. return true;
  4123. }
  4124. Debug ( tRes.m_bSingle = tRes.m_dResults.GetLength ()==1; )
  4125. // build a minimal schema over all the (potentially different) schemes
  4126. // that we have in our aggregated result set
  4127. assert ( tRes.m_dResults.GetLength() || bReturnZeroCount );
  4128. bool bAllEqual = MinimizeSchemas(tRes);
  4129. Debug ( tRes.m_bOneSchema = tRes.m_bSingle; )
  4130. const CSphVector<CSphQueryItem> & dQueryItems = ( tQuery.m_bFacet || tQuery.m_bFacetHead || bForceRefItems ) ? tQuery.m_dRefItems : tQuery.m_dItems;
  4131. // build a list of select items that the query asked for
  4132. bool bHaveExprs = false;
  4133. CSphVector<CSphQueryItem> tExtItems;
  4134. const CSphVector<CSphQueryItem> & dItems = ExpandAsterisk ( tRes.m_tSchema, dQueryItems, tExtItems, tQuery.m_bFacetHead, bHaveExprs );
  4135. // api + index without attributes + select * case
  4136. // can not skip aggregate filtering
  4137. if ( bQueryFromAPI && dItems.IsEmpty() && !pAggrFilter && !bHaveExprs )
  4138. {
  4139. tRes.ClampAllMatches();
  4140. return true;
  4141. }
  4142. // build the final schemas!
  4143. FrontendSchemaBuilder_c tFrontendBuilder ( tRes, tQuery, dItems, dQueryItems, hExtraColumns, bQueryFromAPI, bHaveLocals );
  4144. // track select items that made it into the internal schema and the ones that didn't
  4145. tFrontendBuilder.CollectKnownItems();
  4146. tFrontendBuilder.AddAttrs();
  4147. if ( !tFrontendBuilder.CheckUnmapped ( tRes.m_sError ) )
  4148. return false;
  4149. tFrontendBuilder.Finalize();
  4150. // tricky bit
  4151. // in purely distributed case, all schemas are received from the wire, and miss aggregate functions info
  4152. // thus, we need to re-assign that info
  4153. if ( !bHaveLocals )
  4154. RecoverAggregateFunctions ( tQuery, tRes );
  4155. // if there's more than one result set,
  4156. // we now have to merge and order all the matches
  4157. // this is a good time to apply outer order clause, too
  4158. if ( tRes.m_iSuccesses>1 || pAggrFilter )
  4159. {
  4160. if ( !MergeAllMatches ( tRes, tQuery, bHaveLocals, bAllEqual, bMaster, pAggrFilter, pProfiler ) )
  4161. return false;
  4162. } else
  4163. {
  4164. tRes.m_dResults.First().m_iTag = 0;
  4165. Debug ( tRes.m_bTagsCompacted = true );
  4166. Debug ( tRes.m_bIdxByTag = true; )
  4167. }
  4168. // apply outer order clause to single result set
  4169. // (multiple combined sets just got reordered above)
  4170. // apply inner limit first
  4171. if ( tRes.m_iSuccesses==1 && tQuery.m_bHasOuter )
  4172. {
  4173. tRes.ClampMatches ( tQuery.m_iLimit );
  4174. if ( !tQuery.m_sOuterOrderBy.IsEmpty() )
  4175. {
  4176. if ( !ApplyOuterOrder ( tRes, tQuery ) )
  4177. return false;
  4178. }
  4179. Debug ( tRes.m_bSingle = true; )
  4180. Debug ( tRes.m_bTagsCompacted = true );
  4181. Debug ( tRes.m_bIdxByTag = true; )
  4182. }
  4183. if ( bAllEqual && bHaveLocals )
  4184. {
  4185. CSphScopedProfile tProf ( pProfiler, SPH_QSTATE_EVAL_POST );
  4186. ComputePostlimit ( tRes, tQuery, bMaster );
  4187. }
  4188. if ( bMaster )
  4189. {
  4190. CSphScopedProfile tProf ( pProfiler, SPH_QSTATE_EVAL_GETFIELD );
  4191. RemotesGetField ( tRes, tQuery );
  4192. }
  4193. tFrontendBuilder.RemapGroupBy();
  4194. tFrontendBuilder.RemapFacets();
  4195. // all the merging and sorting is now done
  4196. // replace the minimized matches schema with its subset, the result set schema
  4197. tFrontendBuilder.SwapAttrs ( tRes.m_tSchema );
  4198. return true;
  4199. }
  4200. /////////////////////////////////////////////////////////////////////////////
  4201. struct LocalIndex_t
  4202. {
  4203. CSphString m_sName;
  4204. CSphString m_sParentIndex;
  4205. int m_iOrderTag = 0;
  4206. int m_iWeight = 1;
  4207. int64_t m_iMass = 0;
  4208. };
  4209. struct QueryStat_t
  4210. {
  4211. uint64_t m_uQueryTime = 0;
  4212. uint64_t m_uFoundRows = 0;
  4213. int m_iSuccesses = 0;
  4214. };
  4215. struct StatsPerQuery_t
  4216. {
  4217. CSphVector<QueryStat_t> m_dStats;
  4218. };
  4219. struct DistrServedByAgent_t : StatsPerQuery_t
  4220. {
  4221. CSphString m_sIndex;
  4222. CSphVector<int> m_dAgentIds;
  4223. StrVec_t m_dLocalNames;
  4224. };
  4225. /// manage collection of indexes (to keep them alive)
  4226. /// Get(name) - returns an index from collection.
  4227. /// AddUniqIndex(name) - add local idx to collection, addref is implied by design
  4228. /// AddIndex(name,pidx) - add custom idx, to make it available with Get()
  4229. class KeepCollection_c : public ISphNoncopyable
  4230. {
  4231. SmallStringHash_T<cServedIndexRefPtr_c> m_hIndexes;
  4232. public:
  4233. // add from globally served
  4234. bool AddUniqIndex ( const CSphString& sName );
  4235. // add custom
  4236. void AddIndex ( const CSphString& sName, cServedIndexRefPtr_c pIdx );
  4237. // use idx
  4238. cServedIndexRefPtr_c Get ( const CSphString &sName ) const;
  4239. };
  4240. class SearchHandler_c
  4241. {
  4242. public:
  4243. SearchHandler_c ( int iQueries, std::unique_ptr<QueryParser_i> pParser, QueryType_e eQueryType, bool bMaster );
  4244. ~SearchHandler_c();
  4245. void RunQueries (); ///< run all queries, get all results
  4246. void RunCollect ( const CSphQuery & tQuery, const CSphString & sIndex, CSphString * pErrors, CSphVector<BYTE> * pCollectedDocs );
  4247. void SetQuery ( int iQuery, const CSphQuery & tQuery, std::unique_ptr<ISphTableFunc> pTableFunc );
  4248. void SetQueryParser ( std::unique_ptr<QueryParser_i> pParser, QueryType_e eQueryType );
  4249. void SetProfile ( QueryProfile_c * pProfile );
  4250. AggrResult_t * GetResult ( int iResult ) { return m_dAggrResults.Begin() + iResult; }
  4251. void SetFederatedUser () { m_bFederatedUser = true; }
  4252. public:
  4253. CSphVector<CSphQuery> m_dQueries; ///< queries which i need to search
  4254. CSphVector<AggrResult_t> m_dAggrResults; ///< results which i obtained
  4255. CSphVector<StatsPerQuery_t> m_dQueryIndexStats; ///< statistics for current query
  4256. CSphVector<SearchFailuresLog_c> m_dFailuresSet; ///< failure logs for each query
  4257. CSphVector<CSphVector<int64_t>> m_dAgentTimes; ///< per-agent time stats
  4258. KeepCollection_c m_dAcquired; /// locked indexes
  4259. CSphFixedVector<std::unique_ptr<ISphTableFunc>> m_dTables;
  4260. SqlStmt_t * m_pStmt = nullptr; ///< original (one) statement to take extra options
  4261. protected:
  4262. void RunSubset ( int iStart, int iEnd ); ///< run queries against index(es) from first query in the subset
  4263. void RunLocalSearches();
  4264. bool AllowsMulti() const;
  4265. void SetupLocalDF();
  4266. bool m_bMultiQueue = false; ///< whether current subset is subject to multi-queue optimization
  4267. bool m_bFacetQueue = false; ///< whether current subset is subject to facet-queue optimization
  4268. CSphVector<LocalIndex_t> m_dLocal; ///< local indexes for the current subset
  4269. StrVec_t m_dExtraSchema; ///< the extra attrs for agents. One vec per index*threads
  4270. CSphVector<BYTE> * m_pCollectedDocs = nullptr; ///< this query is for deleting
  4271. QueryProfile_c * m_pProfile = nullptr;
  4272. QueryType_e m_eQueryType {QUERY_API}; ///< queries from sphinxql require special handling
  4273. std::unique_ptr<QueryParser_i> m_pQueryParser; ///< parser used for queries in this handler. e.g. plain or json-style
  4274. bool m_bNeedDocIDs = false; ///< do we need docids returned from local searches (remotes return them anyway)?
  4275. // FIXME!!! breaks for dist threads with SNIPPETS expressions for queries to multiple indexes
  4276. mutable ExprHook_c m_tHook;
  4277. SmallStringHash_T < int64_t > m_hLocalDocs;
  4278. int64_t m_iTotalDocs = 0;
  4279. bool m_bGotLocalDF = false;
  4280. bool m_bMaster;
  4281. bool m_bFederatedUser;
  4282. bool m_bQueryLog = true;
  4283. void OnRunFinished ();
  4284. private:
  4285. CSphVector<CSphQueryResult> m_dResults;
  4286. VecTraits_T<CSphQuery> m_dNQueries; ///< working subset of queries
  4287. VecTraits_T<AggrResult_t> m_dNAggrResults; ///< working subset of results
  4288. VecTraits_T<CSphQueryResult> m_dNResults; ///< working subset of result pointers
  4289. VecTraits_T<SearchFailuresLog_c> m_dNFailuresSet; ///< working subset of failures
  4290. StringBuilder_c m_sError;
  4291. private:
  4292. bool ParseSysVar();
  4293. bool ParseIdxSubkeys();
  4294. bool CheckMultiQuery() const;
  4295. bool AcquireInvokedIndexes();
  4296. void UniqLocals ( VecTraits_T<LocalIndex_t>& dLocals );
  4297. void RunActionQuery ( const CSphQuery & tQuery, const CSphString & sIndex, CSphString * pErrors ); ///< run delete/update
  4298. bool BuildIndexList ( int & iDivideLimits, VecRefPtrsAgentConn_t & dRemotes, CSphVector<DistrServedByAgent_t> & dDistrServedByAgent ); // fixme!
  4299. void CalcTimeStats ( int64_t tmCpu, int64_t tmSubset, const CSphVector<DistrServedByAgent_t> & dDistrServedByAgent );
  4300. void CalcPerIndexStats ( const CSphVector<DistrServedByAgent_t> & dDistrServedByAgent ) const;
  4301. void CalcGlobalStats ( int64_t tmCpu, int64_t tmSubset, int64_t tmLocal, const CSphIOStats & tIO, const VecRefPtrsAgentConn_t & dRemotes ) const;
  4302. int CreateSorters ( const CSphIndex * pIndex, VecTraits_T<ISphMatchSorter*> & dSorters, VecTraits_T<CSphString> & dErrors, StrVec_t * pExtra, SphQueueRes_t & tQueueRes, ISphExprHook * pHook ) const;
  4303. int CreateSingleSorters ( const CSphIndex * pIndex, VecTraits_T<ISphMatchSorter*> & dSorters, VecTraits_T<CSphString> & dErrors, StrVec_t * pExtra, SphQueueRes_t & tQueueRes, ISphExprHook * pHook ) const;
  4304. int CreateMultiQueryOrFacetSorters ( const CSphIndex * pIndex, VecTraits_T<ISphMatchSorter*> & dSorters, VecTraits_T<CSphString> & dErrors, StrVec_t * pExtra, SphQueueRes_t & tQueueRes, ISphExprHook * pHook ) const;
  4305. SphQueueSettings_t MakeQueueSettings ( const CSphIndex * pIndex, int iMaxMatches, ISphExprHook * pHook ) const;
  4306. cServedIndexRefPtr_c CheckIndexSelectable ( const CSphString& sLocal, const char * szParent, VecTraits_T<SearchFailuresLog_c> * pNFailuresSet=nullptr ) const;
  4307. bool CreateValidSorters ( VecTraits_T<ISphMatchSorter *> & dSrt, SphQueueRes_t * pQueueRes, VecTraits_T<SearchFailuresLog_c> & dFlr, StrVec_t * pExtra, const CSphIndex* pIndex, const CSphString & sLocal, const char * szParent, ISphExprHook * pHook );
  4308. void CalcSplits ( int iConcurrency, CSphFixedVector<int> & dSplits );
  4309. };
  4310. PubSearchHandler_c::PubSearchHandler_c ( int iQueries, std::unique_ptr<QueryParser_i> pQueryParser, QueryType_e eQueryType, bool bMaster )
  4311. : m_pImpl { std::make_unique<SearchHandler_c> ( iQueries, std::move ( pQueryParser ), eQueryType, bMaster ) }
  4312. {
  4313. assert ( m_pImpl );
  4314. }
  4315. PubSearchHandler_c::~PubSearchHandler_c () = default;
  4316. void PubSearchHandler_c::RunQueries ()
  4317. {
  4318. m_pImpl->RunQueries();
  4319. }
  4320. void PubSearchHandler_c::SetQuery ( int iQuery, const CSphQuery & tQuery, std::unique_ptr<ISphTableFunc> pTableFunc )
  4321. {
  4322. m_pImpl->SetQuery ( iQuery, tQuery, std::move(pTableFunc) );
  4323. }
  4324. void PubSearchHandler_c::SetProfile ( QueryProfile_c * pProfile )
  4325. {
  4326. m_pImpl->SetProfile ( pProfile );
  4327. }
  4328. void PubSearchHandler_c::SetStmt ( SqlStmt_t & tStmt )
  4329. {
  4330. m_pImpl->m_pStmt = &tStmt;
  4331. }
  4332. AggrResult_t * PubSearchHandler_c::GetResult ( int iResult )
  4333. {
  4334. return m_pImpl->GetResult (iResult);
  4335. }
  4336. void PubSearchHandler_c::PushIndex ( const CSphString& sIndex, const cServedIndexRefPtr_c& pDesc )
  4337. {
  4338. m_pImpl->m_dAcquired.AddIndex ( sIndex, pDesc );
  4339. }
  4340. void PubSearchHandler_c::RunCollect ( const CSphQuery& tQuery, const CSphString& sIndex, CSphString* pErrors, CSphVector<BYTE>* pCollectedDocs )
  4341. {
  4342. m_pImpl->RunCollect ( tQuery, sIndex, pErrors, pCollectedDocs );
  4343. }
  4344. SearchHandler_c::SearchHandler_c ( int iQueries, std::unique_ptr<QueryParser_i> pQueryParser, QueryType_e eQueryType, bool bMaster )
  4345. : m_dTables ( iQueries )
  4346. {
  4347. m_dQueries.Resize ( iQueries );
  4348. m_dAggrResults.Resize ( iQueries );
  4349. m_dFailuresSet.Resize ( iQueries );
  4350. m_dAgentTimes.Resize ( iQueries );
  4351. m_bMaster = bMaster;
  4352. m_bFederatedUser = false;
  4353. SetQueryParser ( std::move ( pQueryParser ), eQueryType );
  4354. m_dResults.Resize ( iQueries );
  4355. for ( int i=0; i<iQueries; ++i )
  4356. m_dResults[i].m_pMeta = &m_dAggrResults[i];
  4357. // initial slices (when nothing explicitly asked)
  4358. m_dNQueries = m_dQueries;
  4359. m_dNAggrResults = m_dAggrResults;
  4360. m_dNResults = m_dResults;
  4361. m_dNFailuresSet = m_dFailuresSet;
  4362. }
  4363. //////////////////
  4364. /* Smart gc retire of vec of queries.
  4365. * We have CSphVector<CSphQuery> which is over, but some threads may still use separate queries from it, so we can't just
  4366. * delete it, since they will loose the objects and it will cause crash.
  4367. *
  4368. * So, if some queries are still in use, we retire them with custom deleter, which will decrease counter,
  4369. * and finally delete whole vec.
  4370. */
  4371. class RetireQueriesVec_c
  4372. {
  4373. CSphVector<CSphQuery> m_dQueries; // given queries I'll finally remove
  4374. std::atomic<int> m_iInUse; // how many of them still reffered
  4375. void OneQueryDeleted()
  4376. {
  4377. if ( m_iInUse.fetch_sub ( 1, std::memory_order_release )==1 )
  4378. {
  4379. assert( m_iInUse.load ( std::memory_order_acquire )==0 );
  4380. delete this;
  4381. }
  4382. }
  4383. static void Delete ( void * pArg )
  4384. {
  4385. if ( pArg )
  4386. {
  4387. auto pMe = (RetireQueriesVec_c *) ( (CSphQuery *) pArg )->m_pCookie;
  4388. assert ( pMe && "Each retiring query from vec must have address of RetireQueriesVec_c in cookie");
  4389. if ( pMe )
  4390. pMe->OneQueryDeleted ();
  4391. }
  4392. }
  4393. public:
  4394. void EngageRetiring ( CSphVector<CSphQuery> dQueries, CSphVector<int> dRetired )
  4395. {
  4396. assert ( !dRetired.IsEmpty () );
  4397. m_iInUse.store ( dRetired.GetLength (), std::memory_order_release );
  4398. m_dQueries = std::move ( dQueries );
  4399. for ( auto iRetired: dRetired )
  4400. {
  4401. m_dQueries[iRetired].m_pCookie = this;
  4402. hazard::Retire ( (void*) &m_dQueries[iRetired], Delete );
  4403. }
  4404. }
  4405. };
  4406. SearchHandler_c::~SearchHandler_c ()
  4407. {
  4408. auto dPointed = hazard::GetListOfPointed ( m_dQueries );
  4409. if ( !dPointed.IsEmpty () )
  4410. {
  4411. // pQueryHolder will be self-removed when all used queries retired
  4412. auto pQueryHolder = new RetireQueriesVec_c;
  4413. pQueryHolder->EngageRetiring ( std::move ( m_dQueries ), std::move ( dPointed ) );
  4414. }
  4415. }
  4416. void SearchHandler_c::SetQueryParser ( std::unique_ptr<QueryParser_i> pParser, QueryType_e eQueryType )
  4417. {
  4418. m_pQueryParser = std::move ( pParser );
  4419. m_eQueryType = eQueryType;
  4420. for ( auto & dQuery : m_dQueries )
  4421. {
  4422. dQuery.m_pQueryParser = m_pQueryParser.get();
  4423. dQuery.m_eQueryType = eQueryType;
  4424. }
  4425. }
  4426. bool KeepCollection_c::AddUniqIndex ( const CSphString & sName )
  4427. {
  4428. if ( m_hIndexes.Exists ( sName ) )
  4429. return true;
  4430. auto pIdx = GetServed ( sName );
  4431. if ( !pIdx )
  4432. return false;
  4433. m_hIndexes.Add ( std::move ( pIdx ), sName );
  4434. return true;
  4435. }
  4436. void KeepCollection_c::AddIndex ( const CSphString & sName, cServedIndexRefPtr_c pIdx )
  4437. {
  4438. if ( m_hIndexes.Exists ( sName ) )
  4439. return;
  4440. m_hIndexes.Add ( std::move ( pIdx ), sName );
  4441. }
  4442. cServedIndexRefPtr_c KeepCollection_c::Get ( const CSphString & sName ) const
  4443. {
  4444. auto * ppIndex = m_hIndexes ( sName );
  4445. assert ( ppIndex && "KeepCollection_c::Get called with absent key");
  4446. return *ppIndex;
  4447. }
  4448. void SearchHandler_c::RunCollect ( const CSphQuery &tQuery, const CSphString &sIndex, CSphString * pErrors, CSphVector<BYTE> * pCollectedDocs )
  4449. {
  4450. m_bQueryLog = false;
  4451. m_pCollectedDocs = pCollectedDocs;
  4452. RunActionQuery ( tQuery, sIndex, pErrors );
  4453. }
  4454. void SearchHandler_c::RunActionQuery ( const CSphQuery & tQuery, const CSphString & sIndex, CSphString * pErrors )
  4455. {
  4456. SetQuery ( 0, tQuery, nullptr );
  4457. m_dQueries[0].m_sIndexes = sIndex;
  4458. m_dLocal.Add ().m_sName = sIndex;
  4459. CheckQuery ( tQuery, *pErrors );
  4460. if ( !pErrors->IsEmpty() )
  4461. return;
  4462. int64_t tmLocal = -sphMicroTimer();
  4463. int64_t tmCPU = -sphTaskCpuTimer ();
  4464. RunLocalSearches();
  4465. tmLocal += sphMicroTimer();
  4466. tmCPU += sphTaskCpuTimer();
  4467. OnRunFinished();
  4468. auto & tRes = m_dAggrResults[0];
  4469. tRes.m_iOffset = tQuery.m_iOffset;
  4470. tRes.m_iCount = Max ( Min ( tQuery.m_iLimit, tRes.GetLength()-tQuery.m_iOffset ), 0 );
  4471. // actualy tRes.m_iCount=0 since delete/update produces no matches
  4472. tRes.m_iQueryTime += (int)(tmLocal/1000);
  4473. tRes.m_iCpuTime += tmCPU;
  4474. if ( !tRes.m_iSuccesses )
  4475. {
  4476. StringBuilder_c sFailures;
  4477. m_dFailuresSet[0].BuildReport ( sFailures );
  4478. sFailures.MoveTo ( *pErrors );
  4479. } else if ( !tRes.m_sError.IsEmpty() )
  4480. {
  4481. StringBuilder_c sFailures;
  4482. m_dFailuresSet[0].BuildReport ( sFailures );
  4483. sFailures.MoveTo ( tRes.m_sWarning ); // FIXME!!! commit warnings too
  4484. }
  4485. const CSphIOStats & tIO = tRes.m_tIOStats;
  4486. auto & g_tStats = gStats ();
  4487. g_tStats.m_iQueries.fetch_add ( 1, std::memory_order_relaxed );
  4488. g_tStats.m_iQueryTime.fetch_add ( tmLocal, std::memory_order_relaxed );
  4489. g_tStats.m_iQueryCpuTime.fetch_add ( tmLocal, std::memory_order_relaxed );
  4490. g_tStats.m_iDiskReads.fetch_add ( tIO.m_iReadOps, std::memory_order_relaxed );
  4491. g_tStats.m_iDiskReadTime.fetch_add ( tIO.m_iReadTime, std::memory_order_relaxed );
  4492. g_tStats.m_iDiskReadBytes.fetch_add ( tIO.m_iReadBytes, std::memory_order_relaxed );
  4493. if ( m_bQueryLog )
  4494. LogQuery ( m_dQueries[0], m_dAggrResults[0], m_dAgentTimes[0] );
  4495. }
  4496. void SearchHandler_c::SetQuery ( int iQuery, const CSphQuery & tQuery, std::unique_ptr<ISphTableFunc> pTableFunc )
  4497. {
  4498. m_dQueries[iQuery] = tQuery;
  4499. m_dQueries[iQuery].m_pQueryParser = m_pQueryParser.get();
  4500. m_dQueries[iQuery].m_eQueryType = m_eQueryType;
  4501. m_dTables[iQuery] = std::move ( pTableFunc );
  4502. }
  4503. void SearchHandler_c::SetProfile ( QueryProfile_c * pProfile )
  4504. {
  4505. assert ( pProfile );
  4506. m_pProfile = pProfile;
  4507. }
  4508. void SearchHandler_c::RunQueries()
  4509. {
  4510. // batch queries to same index(es)
  4511. // or work each query separately if indexes are different
  4512. int iStart = 0;
  4513. ARRAY_FOREACH ( i, m_dQueries )
  4514. {
  4515. if ( m_dQueries[i].m_sIndexes!=m_dQueries[iStart].m_sIndexes )
  4516. {
  4517. RunSubset ( iStart, i );
  4518. iStart = i;
  4519. }
  4520. }
  4521. RunSubset ( iStart, m_dQueries.GetLength() );
  4522. if ( m_bQueryLog )
  4523. {
  4524. ARRAY_FOREACH ( i, m_dQueries )
  4525. LogQuery ( m_dQueries[i], m_dAggrResults[i], m_dAgentTimes[i] );
  4526. }
  4527. OnRunFinished();
  4528. }
  4529. // final fixup
  4530. void SearchHandler_c::OnRunFinished()
  4531. {
  4532. for ( auto & tResult : m_dAggrResults )
  4533. tResult.m_iMatches = tResult.GetLength();
  4534. }
  4535. SphQueueSettings_t SearchHandler_c::MakeQueueSettings ( const CSphIndex * pIndex, int iMaxMatches, ISphExprHook * pHook ) const
  4536. {
  4537. SphQueueSettings_t tQS ( pIndex->GetMatchSchema (), m_pProfile );
  4538. tQS.m_bComputeItems = true;
  4539. tQS.m_pCollection = m_pCollectedDocs;
  4540. tQS.m_pHook = pHook;
  4541. tQS.m_iMaxMatches = GetMaxMatches ( iMaxMatches, pIndex );
  4542. tQS.m_bNeedDocids = m_bNeedDocIDs; // need docids to merge results from indexes
  4543. tQS.m_fnGetCountDistinct = [pIndex]( const CSphString & sAttr ){ return pIndex->GetCountDistinct(sAttr); };
  4544. tQS.m_bEnableFastDistinct = m_dLocal.GetLength()<=1;
  4545. return tQS;
  4546. }
  4547. int SearchHandler_c::CreateMultiQueryOrFacetSorters ( const CSphIndex * pIndex, VecTraits_T<ISphMatchSorter *> & dSorters, VecTraits_T<CSphString> & dErrors, StrVec_t * pExtra, SphQueueRes_t & tQueueRes, ISphExprHook * pHook ) const
  4548. {
  4549. int iValidSorters = 0;
  4550. auto tQueueSettings = MakeQueueSettings ( pIndex, m_dNQueries.First ().m_iMaxMatches, pHook );
  4551. sphCreateMultiQueue ( tQueueSettings, m_dNQueries, dSorters, dErrors, tQueueRes, pExtra, m_pProfile );
  4552. m_dNQueries.First().m_bZSlist = tQueueRes.m_bZonespanlist;
  4553. dSorters.Apply ( [&iValidSorters] ( const ISphMatchSorter * pSorter ) {
  4554. if ( pSorter )
  4555. ++iValidSorters;
  4556. } );
  4557. if ( m_bFacetQueue && iValidSorters<dSorters.GetLength () )
  4558. {
  4559. dSorters.Apply ( [] ( ISphMatchSorter *& pSorter ) { SafeDelete (pSorter); } );
  4560. return 0;
  4561. }
  4562. return iValidSorters;
  4563. }
  4564. int SearchHandler_c::CreateSingleSorters ( const CSphIndex * pIndex, VecTraits_T<ISphMatchSorter *> & dSorters, VecTraits_T<CSphString> & dErrors, StrVec_t * pExtra, SphQueueRes_t & tQueueRes, ISphExprHook * pHook ) const
  4565. {
  4566. int iValidSorters = 0;
  4567. tQueueRes.m_bAlowMulti = false;
  4568. const int iQueries = m_dNQueries.GetLength();
  4569. for ( int iQuery = 0; iQuery<iQueries; ++iQuery )
  4570. {
  4571. CSphQuery & tQuery = m_dNQueries[iQuery];
  4572. // create queue
  4573. auto tQueueSettings = MakeQueueSettings ( pIndex, tQuery.m_iMaxMatches, pHook );
  4574. ISphMatchSorter * pSorter = sphCreateQueue ( tQueueSettings, tQuery, dErrors[iQuery], tQueueRes, pExtra, m_pProfile );
  4575. if ( !pSorter )
  4576. continue;
  4577. tQuery.m_bZSlist = tQueueRes.m_bZonespanlist;
  4578. dSorters[iQuery] = pSorter;
  4579. ++iValidSorters;
  4580. }
  4581. return iValidSorters;
  4582. }
  4583. int SearchHandler_c::CreateSorters ( const CSphIndex * pIndex, VecTraits_T<ISphMatchSorter *> & dSorters, VecTraits_T<CSphString> & dErrors, StrVec_t* pExtra, SphQueueRes_t & tQueueRes, ISphExprHook * pHook ) const
  4584. {
  4585. if ( m_bMultiQueue || m_bFacetQueue )
  4586. return CreateMultiQueryOrFacetSorters ( pIndex, dSorters, dErrors, pExtra, tQueueRes, pHook );
  4587. return CreateSingleSorters ( pIndex, dSorters, dErrors, pExtra, tQueueRes, pHook );
  4588. }
  4589. struct LocalSearchRef_t
  4590. {
  4591. ExprHook_c& m_tHook;
  4592. StrVec_t* m_pExtra;
  4593. VecTraits_T<SearchFailuresLog_c>& m_dFailuresSet;
  4594. VecTraits_T<AggrResult_t>& m_dAggrResults;
  4595. VecTraits_T<CSphQueryResult>& m_dResults;
  4596. LocalSearchRef_t ( ExprHook_c & tHook, StrVec_t* pExtra, VecTraits_T<SearchFailuresLog_c> & dFailures, VecTraits_T<AggrResult_t> & dAggrResults, VecTraits_T<CSphQueryResult> & dResults )
  4597. : m_tHook ( tHook )
  4598. , m_pExtra ( pExtra )
  4599. , m_dFailuresSet ( dFailures )
  4600. , m_dAggrResults ( dAggrResults )
  4601. , m_dResults ( dResults )
  4602. {}
  4603. void MergeChild ( LocalSearchRef_t dChild ) const
  4604. {
  4605. if ( m_pExtra )
  4606. {
  4607. assert ( dChild.m_pExtra );
  4608. m_pExtra->Append ( *dChild.m_pExtra );
  4609. }
  4610. auto & dChildAggrResults = dChild.m_dAggrResults;
  4611. for ( int i = 0, iQueries = m_dAggrResults.GetLength (); i<iQueries; ++i )
  4612. {
  4613. auto & tResult = m_dAggrResults[i];
  4614. auto & tChild = dChildAggrResults[i];
  4615. tResult.m_dResults.Append ( tChild.m_dResults );
  4616. // word statistics
  4617. tResult.MergeWordStats ( tChild );
  4618. // other data (warnings, errors, etc.)
  4619. // errors
  4620. if ( !tChild.m_sError.IsEmpty ())
  4621. tResult.m_sError = tChild.m_sError;
  4622. // warnings
  4623. if ( !tChild.m_sWarning.IsEmpty ())
  4624. tResult.m_sWarning = tChild.m_sWarning;
  4625. // prediction counters
  4626. tResult.m_bHasPrediction |= tChild.m_bHasPrediction;
  4627. if ( tChild.m_bHasPrediction )
  4628. {
  4629. tResult.m_tStats.Add ( tChild.m_tStats );
  4630. tResult.m_iPredictedTime = CalcPredictedTimeMsec ( tResult );
  4631. }
  4632. // profiling
  4633. if ( tChild.m_pProfile )
  4634. tResult.m_pProfile->AddMetric ( *tChild.m_pProfile );
  4635. tResult.m_iCpuTime += tChild.m_iCpuTime;
  4636. tResult.m_iTotalMatches += tChild.m_iTotalMatches;
  4637. tResult.m_bTotalMatchesApprox |= tChild.m_bTotalMatchesApprox;
  4638. tResult.m_iSuccesses += tChild.m_iSuccesses;
  4639. tResult.m_tIOStats.Add ( tChild.m_tIOStats );
  4640. tResult.m_tIteratorStats.Merge ( tChild.m_tIteratorStats );
  4641. // failures
  4642. m_dFailuresSet[i].Append ( dChild.m_dFailuresSet[i] );
  4643. }
  4644. }
  4645. inline static bool IsClonable()
  4646. {
  4647. return true;
  4648. }
  4649. };
  4650. struct LocalSearchClone_t
  4651. {
  4652. ExprHook_c m_tHook;
  4653. StrVec_t m_dExtra;
  4654. StrVec_t* m_pExtra;
  4655. CSphVector<SearchFailuresLog_c> m_dFailuresSet;
  4656. CSphVector<AggrResult_t> m_dAggrResults;
  4657. CSphVector<CSphQueryResult> m_dResults;
  4658. explicit LocalSearchClone_t ( const LocalSearchRef_t & dParent)
  4659. {
  4660. int iQueries = dParent.m_dFailuresSet.GetLength ();
  4661. m_dFailuresSet.Resize ( iQueries );
  4662. m_dAggrResults.Resize ( iQueries );
  4663. m_dResults.Resize ( iQueries );
  4664. for ( int i=0; i<iQueries; ++i )
  4665. m_dResults[i].m_pMeta = &m_dAggrResults[i];
  4666. m_pExtra = dParent.m_pExtra ? &m_dExtra : nullptr;
  4667. // set profiler complementary to one in RunSubset (search by `if ( iQueries==1 && m_pProfile )` clause)
  4668. if ( iQueries==1 && dParent.m_dAggrResults.First ().m_pProfile )
  4669. {
  4670. auto pProfile = new QueryProfile_c;
  4671. m_dAggrResults.First().m_pProfile = pProfile;
  4672. m_tHook.SetProfiler ( pProfile );
  4673. }
  4674. }
  4675. explicit operator LocalSearchRef_t ()
  4676. {
  4677. return { m_tHook, m_pExtra, m_dFailuresSet, m_dAggrResults, m_dResults };
  4678. }
  4679. ~LocalSearchClone_t()
  4680. {
  4681. if ( !m_dAggrResults.IsEmpty () )
  4682. SafeDelete ( m_dAggrResults.First().m_pProfile );
  4683. }
  4684. };
  4685. cServedIndexRefPtr_c SearchHandler_c::CheckIndexSelectable ( const CSphString & sLocal, const char * szParent, VecTraits_T<SearchFailuresLog_c> * pNFailuresSet ) const
  4686. {
  4687. const auto& pServed = m_dAcquired.Get ( sLocal );
  4688. assert ( pServed );
  4689. if ( !ServedDesc_t::IsSelectable ( pServed ) )
  4690. {
  4691. if ( pNFailuresSet )
  4692. for ( auto & dFailureSet : *pNFailuresSet )
  4693. dFailureSet.SubmitEx ( sLocal, nullptr, "%s", "index is not suitable for select" );
  4694. return cServedIndexRefPtr_c{};
  4695. }
  4696. return pServed;
  4697. }
  4698. bool SearchHandler_c::CreateValidSorters ( VecTraits_T<ISphMatchSorter *> & dSrt, SphQueueRes_t * pQueueRes, VecTraits_T<SearchFailuresLog_c> & dFlr, StrVec_t * pExtra, const CSphIndex * pIndex, const CSphString & sLocal, const char * szParent, ISphExprHook * pHook )
  4699. {
  4700. auto iQueries = dSrt.GetLength();
  4701. #if PARANOID
  4702. for ( const auto* pSorter : dSrt)
  4703. assert ( !pSorter );
  4704. #endif
  4705. CSphFixedVector<CSphString> dErrors ( iQueries );
  4706. int iValidSorters = CreateSorters ( pIndex, dSrt, dErrors, pExtra, *pQueueRes, pHook );
  4707. if ( iValidSorters<dSrt.GetLength() )
  4708. {
  4709. ARRAY_FOREACH ( i, dErrors )
  4710. {
  4711. if ( !dErrors[i].IsEmpty () )
  4712. dFlr[i].Submit ( sLocal, szParent, dErrors[i].cstr () );
  4713. }
  4714. }
  4715. m_bMultiQueue = pQueueRes->m_bAlowMulti;
  4716. return !!iValidSorters;
  4717. }
  4718. void SearchHandler_c::CalcSplits ( int iConcurrency, CSphFixedVector<int> & dSplits )
  4719. {
  4720. // dSplits should already be initialized with 1s
  4721. if ( !g_bSplit )
  4722. return;
  4723. if ( !iConcurrency )
  4724. iConcurrency = g_iThreads;
  4725. struct SplitData_t
  4726. {
  4727. bool m_bEnabled = false;
  4728. int64_t m_iMetric = 0;
  4729. };
  4730. CSphFixedVector<SplitData_t> dSplitData { m_dLocal.GetLength() };
  4731. // FIXME! what about PQ?
  4732. int64_t iTotalMetric = 0;
  4733. int iSingleSplits = 0;
  4734. int iEnabled = 0;
  4735. ARRAY_FOREACH ( iLocal, m_dLocal )
  4736. {
  4737. const LocalIndex_t & tLocal = m_dLocal[iLocal];
  4738. auto pIndex = CheckIndexSelectable ( tLocal.m_sName, tLocal.m_sParentIndex.cstr(), nullptr );
  4739. if ( !pIndex )
  4740. continue;
  4741. SplitData_t & tSplitData = dSplitData[iLocal];
  4742. int64_t iMetric = RIdx_c ( pIndex )->GetPseudoShardingMetric ( m_dNQueries );
  4743. if ( iMetric==-1 )
  4744. {
  4745. iSingleSplits++;
  4746. continue;
  4747. }
  4748. tSplitData.m_bEnabled = true;
  4749. tSplitData.m_iMetric = iMetric;
  4750. iTotalMetric += tSplitData.m_iMetric;
  4751. iEnabled++;
  4752. }
  4753. if ( iConcurrency>iSingleSplits+iEnabled )
  4754. {
  4755. int iLeft = iConcurrency-iSingleSplits;
  4756. ARRAY_FOREACH ( i, dSplitData )
  4757. {
  4758. const SplitData_t & tSplitData = dSplitData[i];
  4759. if ( !tSplitData.m_bEnabled )
  4760. continue;
  4761. dSplits[i] = Max ( (int)round ( double(tSplitData.m_iMetric) / iTotalMetric * iLeft ), 1 );
  4762. }
  4763. }
  4764. }
  4765. class AssignTag_c : public MatchProcessor_i
  4766. {
  4767. public:
  4768. AssignTag_c ( int iTag )
  4769. : m_iTag ( iTag )
  4770. {}
  4771. void Process ( CSphMatch * pMatch ) final { ProcessMatch(pMatch); }
  4772. bool ProcessInRowIdOrder() const final { return false; }
  4773. void Process ( VecTraits_T<CSphMatch *> & dMatches ) final { dMatches.for_each ( [this]( CSphMatch * pMatch ){ ProcessMatch(pMatch); } ); }
  4774. private:
  4775. int m_iTag = 0;
  4776. inline void ProcessMatch ( CSphMatch * pMatch ) { pMatch->m_iTag = m_iTag; }
  4777. };
  4778. class GlobalSorters_c
  4779. {
  4780. public:
  4781. GlobalSorters_c ( const VecTraits_T<CSphQuery> & dQueries, const CSphVector<cServedIndexRefPtr_c> & dIndexes )
  4782. : m_dQueries ( dQueries )
  4783. , m_dSorters { dQueries.GetLength() }
  4784. {
  4785. auto iValidIndexes = (int)dIndexes.count_of ( [&] ( const auto& pIndex ) { return pIndex; } );
  4786. m_bNeedGlobalSorters = iValidIndexes>1 && !dQueries.First().m_sGroupDistinct.IsEmpty();
  4787. if ( m_bNeedGlobalSorters )
  4788. {
  4789. // check if schemas are same
  4790. const CSphSchema * pFirstSchema = nullptr;
  4791. for ( auto i : dIndexes )
  4792. {
  4793. if ( !i )
  4794. continue;
  4795. if ( !pFirstSchema )
  4796. {
  4797. pFirstSchema = &RIdx_c ( i )->GetMatchSchema();
  4798. continue;
  4799. }
  4800. CSphString sCmpError;
  4801. if ( !pFirstSchema->CompareTo ( RIdx_c ( i )->GetMatchSchema(), sCmpError ) )
  4802. {
  4803. m_bNeedGlobalSorters = false;
  4804. break;
  4805. }
  4806. }
  4807. }
  4808. for ( auto & i : m_dSorters )
  4809. i.Resize ( dIndexes.GetLength() );
  4810. }
  4811. ~GlobalSorters_c()
  4812. {
  4813. for ( auto & i : m_dSorters )
  4814. for ( auto & j : i )
  4815. SafeDelete ( j.m_pSorter );
  4816. }
  4817. bool StoreSorter ( int iQuery, int iIndex, ISphMatchSorter * & pSorter, const DocstoreReader_i * pDocstore, int iTag )
  4818. {
  4819. // FACET head is the plain query wo group sorter and can not move all result set into single sorter
  4820. // could be replaced with !pSorter->IspSorter->IsGroupby()
  4821. if ( !NeedGlobalSorters() || m_dQueries[iQuery].m_bFacetHead )
  4822. return false;
  4823. // take ownership of the sorter
  4824. m_dSorters[iQuery][iIndex] = { pSorter, pDocstore, iTag };
  4825. pSorter = nullptr;
  4826. return true;
  4827. }
  4828. bool NeedGlobalSorters() const
  4829. {
  4830. return m_bNeedGlobalSorters;
  4831. }
  4832. void MergeResults ( VecTraits_T<AggrResult_t> & dResults )
  4833. {
  4834. if ( !NeedGlobalSorters() )
  4835. return;
  4836. ARRAY_FOREACH ( iQuery, m_dSorters )
  4837. {
  4838. CSphVector<ISphMatchSorter *> dValidSorters;
  4839. for ( auto i : m_dSorters[iQuery] )
  4840. {
  4841. if ( !i.m_pSorter )
  4842. continue;
  4843. dValidSorters.Add ( i.m_pSorter );
  4844. // assign order tag here so we can link to docstore later
  4845. AssignTag_c tAssign ( i.m_iTag );
  4846. i.m_pSorter->Finalize ( tAssign, false, false );
  4847. }
  4848. int iNumIndexes = dValidSorters.GetLength();
  4849. if ( !iNumIndexes )
  4850. continue;
  4851. ISphMatchSorter * pLastSorter = dValidSorters[iNumIndexes-1];
  4852. // merge all results to the last sorter. this is done to try to keep some compatibility with no-global-sorters code branch
  4853. for ( int iIndex = iNumIndexes-2; iIndex>=0; iIndex-- )
  4854. dValidSorters[iIndex]->MoveTo ( pLastSorter, true );
  4855. dResults[iQuery].m_iTotalMatches = pLastSorter->GetTotalCount();
  4856. dResults[iQuery].AddResultset ( pLastSorter, m_dSorters[iQuery][0].m_pDocstore, m_dSorters[iQuery][0].m_iTag, m_dQueries[iQuery].m_iCutoff );
  4857. // we already assigned index/docstore tags to all matches; no need to do it again
  4858. if ( dResults[iQuery].m_dResults.GetLength() )
  4859. dResults[iQuery].m_dResults[0].m_bTagsAssigned = true;
  4860. // add fake empty result sets (for tag->docstore lookup)
  4861. for ( int i = 1; i < m_dSorters[iQuery].GetLength(); i++ )
  4862. dResults[iQuery].AddEmptyResultset ( m_dSorters[iQuery][i].m_pDocstore, m_dSorters[iQuery][i].m_iTag );
  4863. }
  4864. }
  4865. private:
  4866. struct SorterData_t
  4867. {
  4868. ISphMatchSorter * m_pSorter = nullptr;
  4869. const DocstoreReader_i * m_pDocstore = nullptr;
  4870. int m_iTag = 0;
  4871. };
  4872. const VecTraits_T<CSphQuery> & m_dQueries;
  4873. CSphVector<CSphVector<SorterData_t>> m_dSorters;
  4874. bool m_bNeedGlobalSorters = false;
  4875. };
  4876. void SearchHandler_c::RunLocalSearches ()
  4877. {
  4878. int64_t tmLocal = sphMicroTimer ();
  4879. // setup local searches
  4880. const int iQueries = m_dNQueries.GetLength ();
  4881. const int iNumLocals = m_dLocal.GetLength();
  4882. // sphWarning ( "%s:%d", __FUNCTION__, __LINE__ );
  4883. // sphWarning ("Locals: %d, queries %d", iNumLocals, iQueries );
  4884. m_dQueryIndexStats.Resize ( iNumLocals );
  4885. for ( auto & dQueryIndexStats : m_dQueryIndexStats )
  4886. dQueryIndexStats.m_dStats.Resize ( iQueries );
  4887. StrVec_t * pMainExtra = nullptr;
  4888. if ( m_dNQueries.First ().m_bAgent )
  4889. {
  4890. m_dExtraSchema.Reset (); // cleanup from any possible previous usages
  4891. pMainExtra = &m_dExtraSchema;
  4892. }
  4893. CSphVector<cServedIndexRefPtr_c> dLocalIndexes;
  4894. for ( const auto& i : m_dLocal )
  4895. dLocalIndexes.Add ( CheckIndexSelectable ( i.m_sName, nullptr ) );
  4896. GlobalSorters_c tGlobalSorters ( m_dNQueries, dLocalIndexes );
  4897. CSphFixedVector<int> dSplits { iNumLocals };
  4898. dSplits.Fill(1);
  4899. CSphFixedVector<int> dOrder { iNumLocals };
  4900. for ( int i = 0; i<iNumLocals; ++i )
  4901. dOrder[i] = i;
  4902. auto tDispatch = GetEffectiveBaseDispatcherTemplate();
  4903. Dispatcher::Unify ( tDispatch, m_dNQueries.First().m_tMainDispatcher );
  4904. // the context
  4905. ClonableCtx_T<LocalSearchRef_t, LocalSearchClone_t, Threads::ECONTEXT::UNORDERED> dCtx { m_tHook, pMainExtra, m_dNFailuresSet, m_dNAggrResults, m_dNResults };
  4906. auto pDispatcher = Dispatcher::Make ( iNumLocals, m_dNQueries.First().m_iCouncurrency, tDispatch, dCtx.IsSingle() );
  4907. dCtx.LimitConcurrency ( pDispatcher->GetConcurrency() );
  4908. bool bSingle = pDispatcher->GetConcurrency()==1;
  4909. // sphWarning ( "iConcurrency: %d", iConcurrency );
  4910. if ( !bSingle )
  4911. {
  4912. // sphWarning ( "Reordering..." );
  4913. // if run parallel - start in mass order, if single - in natural order
  4914. // set order by decreasing index mass (heaviest one comes first). That is why 'less' implemented by '>'
  4915. dOrder.Sort ( Lesser ( [this] ( int a, int b ) {
  4916. return m_dLocal[a].m_iMass>m_dLocal[b].m_iMass;
  4917. } ) );
  4918. CalcSplits ( pDispatcher->GetConcurrency(), dSplits );
  4919. }
  4920. // for ( int iOrder : dOrder )
  4921. // sphWarning ( "Sorted: %d, Order %d, mass %d", !!bSingle, iOrder, (int) m_dLocal[iOrder].m_iMass );
  4922. std::atomic<int32_t> iTotalSuccesses { 0 };
  4923. Coro::ExecuteN ( dCtx.Concurrency ( iNumLocals ), [&]
  4924. {
  4925. auto pSource = pDispatcher->MakeSource();
  4926. int iJob = -1; // make it consumed
  4927. if ( !pSource->FetchTask ( iJob ) )
  4928. {
  4929. sphLogDebug ( "Early finish parallel RunLocalSearches because of empty queue" );
  4930. return; // already nothing to do, early finish.
  4931. }
  4932. // these two moved from inside the loop to avoid construction on every turn
  4933. CSphVector<ISphMatchSorter *> dSorters ( iQueries );
  4934. dSorters.ZeroVec ();
  4935. auto tJobContext = dCtx.CloneNewContext();
  4936. auto& tCtx = tJobContext.first;
  4937. sphLogDebug ( "RunLocalSearches cloned context %d", tJobContext.second );
  4938. Threads::Coro::Throttler_c tThrottler ( session::GetThrottlingPeriodMS () );
  4939. while ( true )
  4940. {
  4941. if ( !pSource->FetchTask ( iJob ) )
  4942. return; // all is done
  4943. auto iLocal = dOrder[iJob];
  4944. sphLogDebugv ( "RunLocalSearches %d, iJob: %d, iLocal: %d, mass %d", tJobContext.second, iJob, iLocal, (int) m_dLocal[iLocal].m_iMass );
  4945. iJob = -1; // mark it consumed
  4946. int64_t iCpuTime = -sphTaskCpuTimer ();
  4947. // FIXME!!! handle different proto
  4948. myinfo::SetTaskInfo( R"(api-search query="%s" comment="%s" index="%s")",
  4949. m_dNQueries.First().m_sQuery.scstr (),
  4950. m_dNQueries.First().m_sComment.scstr (),
  4951. m_dLocal[iLocal].m_sName.scstr ());
  4952. const LocalIndex_t & dLocal = m_dLocal[iLocal];
  4953. const CSphString& sLocal = dLocal.m_sName;
  4954. const char * szParent = dLocal.m_sParentIndex.cstr ();
  4955. int iOrderTag = dLocal.m_iOrderTag;
  4956. int iIndexWeight = dLocal.m_iWeight;
  4957. auto& dNFailuresSet = tCtx.m_dFailuresSet;
  4958. auto& dNAggrResults = tCtx.m_dAggrResults;
  4959. auto& dNResults = tCtx.m_dResults;
  4960. auto* pExtra = tCtx.m_pExtra;
  4961. // publish crash query index
  4962. GlobalCrashQueryGetRef().m_dIndex = FromStr ( sLocal );
  4963. // prepare and check the index
  4964. cServedIndexRefPtr_c pServed = CheckIndexSelectable ( sLocal, szParent, &dNFailuresSet );
  4965. if ( !pServed )
  4966. continue;
  4967. bool bResult = false;
  4968. CSphQueryResultMeta tMqMeta;
  4969. CSphQueryResult tMqRes;
  4970. tMqRes.m_pMeta = &tMqMeta;
  4971. { // scope for r-locking the index
  4972. RIdx_c pIndex { pServed };
  4973. tCtx.m_tHook.SetIndex ( pIndex );
  4974. tCtx.m_tHook.SetQueryType ( m_eQueryType );
  4975. // create sorters
  4976. SphQueueRes_t tQueueRes;
  4977. if ( !CreateValidSorters ( dSorters, &tQueueRes, dNFailuresSet, pExtra, pIndex, sLocal, szParent, &tCtx.m_tHook ) )
  4978. continue;
  4979. // do the query
  4980. CSphMultiQueryArgs tMultiArgs ( iIndexWeight );
  4981. tMultiArgs.m_uPackedFactorFlags = tQueueRes.m_uPackedFactorFlags;
  4982. if ( m_bGotLocalDF )
  4983. {
  4984. tMultiArgs.m_bLocalDF = true;
  4985. tMultiArgs.m_pLocalDocs = &m_hLocalDocs;
  4986. tMultiArgs.m_iTotalDocs = m_iTotalDocs;
  4987. }
  4988. bool bCanBeCloned = dSorters.all_of ( []( auto * pSorter ){ return pSorter ? pSorter->CanBeCloned() : true; } );
  4989. // fixme: previous calculations are wrong; we are not splitting the query if we are using non-clonable sorters
  4990. tMultiArgs.m_iSplit = bCanBeCloned ? dSplits[iLocal] : 1;
  4991. tMultiArgs.m_bFinalizeSorters = !tGlobalSorters.NeedGlobalSorters();
  4992. dNAggrResults.First().m_tIOStats.Start ();
  4993. if ( m_bMultiQueue )
  4994. bResult = pIndex->MultiQuery ( tMqRes, m_dNQueries.First(), dSorters, tMultiArgs );
  4995. else
  4996. bResult = pIndex->MultiQueryEx ( iQueries, &m_dNQueries[0], &dNResults[0], &dSorters[0], tMultiArgs );
  4997. dNAggrResults.First ().m_tIOStats.Stop ();
  4998. }
  4999. iCpuTime += sphTaskCpuTimer ();
  5000. // handle results
  5001. if ( bResult )
  5002. {
  5003. // multi-query succeeded
  5004. for ( int i=0; i<iQueries; ++i )
  5005. {
  5006. // in mt here kind of tricky index calculation, up to the next lines with sorter
  5007. // but some sorters could have failed at "create sorter" stage
  5008. ISphMatchSorter * pSorter = dSorters[i];
  5009. if ( !pSorter )
  5010. continue;
  5011. // this one seems OK
  5012. AggrResult_t & tNRes = dNAggrResults[i];
  5013. int iQTimeForStats = tNRes.m_iQueryTime;
  5014. auto pDocstore = m_bMultiQueue ? tMqRes.m_pDocstore : dNResults[i].m_pDocstore;
  5015. // multi-queue only returned one result set meta, so we need to replicate it
  5016. if ( m_bMultiQueue )
  5017. {
  5018. // these times will be overridden below, but let's be clean
  5019. iQTimeForStats = tMqMeta.m_iQueryTime / iQueries;
  5020. tNRes.m_iQueryTime += iQTimeForStats;
  5021. iCpuTime /= iQueries;
  5022. tNRes.MergeWordStats ( tMqMeta );
  5023. tNRes.m_iMultiplier = iQueries;
  5024. tNRes.m_iCpuTime += tMqMeta.m_iCpuTime / iQueries;
  5025. } else if ( tNRes.m_iMultiplier==-1 ) // multiplier -1 means 'error'
  5026. {
  5027. dNFailuresSet[i].Submit ( sLocal, szParent, tNRes.m_sError.cstr() );
  5028. continue;
  5029. }
  5030. ++tNRes.m_iSuccesses;
  5031. tNRes.m_iCpuTime = iCpuTime;
  5032. tNRes.m_iTotalMatches += pSorter->GetTotalCount();
  5033. tNRes.m_iPredictedTime = tNRes.m_bHasPrediction ? CalcPredictedTimeMsec ( tNRes ) : 0;
  5034. m_dQueryIndexStats[iLocal].m_dStats[i].m_iSuccesses = 1;
  5035. m_dQueryIndexStats[iLocal].m_dStats[i].m_uQueryTime = iQTimeForStats;
  5036. m_dQueryIndexStats[iLocal].m_dStats[i].m_uFoundRows = pSorter->GetTotalCount();
  5037. iTotalSuccesses.fetch_add ( 1, std::memory_order_relaxed );
  5038. // extract matches from sorter
  5039. if ( !tGlobalSorters.StoreSorter ( i, iLocal, dSorters[i], pDocstore, iOrderTag ) )
  5040. tNRes.AddResultset( pSorter, pDocstore, iOrderTag, m_dNQueries[i].m_iCutoff );
  5041. if ( !tNRes.m_sWarning.IsEmpty () )
  5042. dNFailuresSet[i].Submit ( sLocal, szParent, tNRes.m_sWarning.cstr () );
  5043. }
  5044. } else
  5045. // failed, submit local (if not empty) or global error string
  5046. for ( int i = 0; i<iQueries; ++i )
  5047. dNFailuresSet[i].Submit ( sLocal, szParent, tMqMeta.m_sError.IsEmpty ()
  5048. ? dNAggrResults[m_bMultiQueue ? 0 : i].m_sError.cstr ()
  5049. : tMqMeta.m_sError.cstr () );
  5050. // cleanup sorters
  5051. for ( auto &pSorter : dSorters )
  5052. SafeDelete ( pSorter );
  5053. if ( !pSource->FetchTask ( iJob ) )
  5054. return; // all is done
  5055. tThrottler.ThrottleAndKeepCrashQuery (); // we set CrashQuery anyway at the start of the loop
  5056. }
  5057. });
  5058. sphLogDebug ( "RunLocalSearches processed in %d thread(s)", dCtx.NumWorked() );
  5059. dCtx.Finalize (); // merge mt results (if any)
  5060. tGlobalSorters.MergeResults(m_dNAggrResults);
  5061. // update our wall time for every result set
  5062. tmLocal = sphMicroTimer ()-tmLocal;
  5063. for ( int iQuery = 0; iQuery<iQueries; ++iQuery )
  5064. m_dNAggrResults[iQuery].m_iQueryTime += (int) ( tmLocal / 1000 );
  5065. auto iTotalSuccessesInt = iTotalSuccesses.load ( std::memory_order_relaxed );
  5066. for ( auto iLocal = 0; iLocal<iNumLocals; ++iLocal )
  5067. for ( int iQuery = 0; iQuery<iQueries; ++iQuery )
  5068. {
  5069. QueryStat_t & tStat = m_dQueryIndexStats[iLocal].m_dStats[iQuery];
  5070. if ( tStat.m_iSuccesses )
  5071. tStat.m_uQueryTime = (int) ( tmLocal / 1000 / iTotalSuccessesInt );
  5072. }
  5073. }
  5074. // check expressions into a query to make sure that it's ready for multi query optimization
  5075. bool SearchHandler_c::AllowsMulti() const
  5076. {
  5077. if ( m_bFacetQueue )
  5078. return true;
  5079. // in some cases the same select list allows queries to be multi query optimized
  5080. // but we need to check dynamic parts size equality and we do it later in RunLocalSearches()
  5081. const CSphVector<CSphQueryItem> & tFirstQueryItems = m_dNQueries.First().m_dItems;
  5082. bool bItemsSameLen = true;
  5083. for ( int i=1; i<m_dNQueries.GetLength() && bItemsSameLen; ++i )
  5084. bItemsSameLen = ( tFirstQueryItems.GetLength()==m_dNQueries[i].m_dItems.GetLength() );
  5085. if ( bItemsSameLen )
  5086. {
  5087. bool bSameItems = true;
  5088. ARRAY_FOREACH_COND ( i, tFirstQueryItems, bSameItems )
  5089. {
  5090. const CSphQueryItem & tItem1 = tFirstQueryItems[i];
  5091. for ( int j=1; j<m_dNQueries.GetLength () && bSameItems; ++j )
  5092. {
  5093. const CSphQueryItem & tItem2 = m_dNQueries[j].m_dItems[i];
  5094. bSameItems = tItem1.m_sExpr==tItem2.m_sExpr && tItem1.m_eAggrFunc==tItem2.m_eAggrFunc;
  5095. }
  5096. }
  5097. if ( bSameItems )
  5098. return true;
  5099. }
  5100. // if select lists do not contain any expressions we can optimize queries too
  5101. for ( const auto & dLocal : m_dLocal )
  5102. {
  5103. RIdx_c pServedIndex ( m_dAcquired.Get ( dLocal.m_sName ) );
  5104. // FIXME!!! compare expressions as m_pExpr->GetHash
  5105. const CSphSchema & tSchema = pServedIndex->GetMatchSchema();
  5106. if ( m_dNQueries.any_of ( [&tSchema] ( const CSphQuery & tQ ) { return sphHasExpressions ( tQ, tSchema ); } ) )
  5107. return false;
  5108. }
  5109. return true;
  5110. }
  5111. struct IndexSettings_t
  5112. {
  5113. uint64_t m_uHash;
  5114. int m_iLocal;
  5115. };
  5116. void SearchHandler_c::SetupLocalDF ()
  5117. {
  5118. if ( m_dLocal.GetLength()<2 )
  5119. return;
  5120. SwitchProfile ( m_pProfile, SPH_QSTATE_LOCAL_DF );
  5121. bool bGlobalIDF = true;
  5122. ARRAY_FOREACH_COND ( i, m_dLocal, bGlobalIDF )
  5123. {
  5124. auto pDesc = GetServed( m_dLocal[i].m_sName );
  5125. bGlobalIDF = ( pDesc && !pDesc->m_sGlobalIDFPath.IsEmpty () );
  5126. }
  5127. // bail out on all indexes with global idf set
  5128. if ( bGlobalIDF )
  5129. return;
  5130. bool bOnlyNoneRanker = true;
  5131. bool bOnlyFullScan = true;
  5132. bool bHasLocalDF = false;
  5133. for ( const CSphQuery & tQuery : m_dNQueries )
  5134. {
  5135. bOnlyFullScan &= tQuery.m_sQuery.IsEmpty();
  5136. bHasLocalDF |= tQuery.m_bLocalDF;
  5137. if ( !tQuery.m_sQuery.IsEmpty() && tQuery.m_bLocalDF )
  5138. bOnlyNoneRanker &= ( tQuery.m_eRanker==SPH_RANK_NONE );
  5139. }
  5140. // bail out queries: full-scan, ranker=none, local_idf=0
  5141. if ( bOnlyFullScan || bOnlyNoneRanker || !bHasLocalDF )
  5142. return;
  5143. CSphVector<char> dQuery ( 512 );
  5144. dQuery.Resize ( 0 );
  5145. for ( const CSphQuery & tQuery : m_dNQueries )
  5146. {
  5147. if ( tQuery.m_sQuery.IsEmpty() || !tQuery.m_bLocalDF || tQuery.m_eRanker==SPH_RANK_NONE )
  5148. continue;
  5149. int iLen = tQuery.m_sQuery.Length();
  5150. auto * pDst = dQuery.AddN ( iLen + 1 );
  5151. memcpy ( pDst, tQuery.m_sQuery.cstr(), iLen );
  5152. dQuery.Last() = ' '; // queries delimiter
  5153. }
  5154. // bail out on empty queries
  5155. if ( !dQuery.GetLength() )
  5156. return;
  5157. dQuery.Add ( '\0' );
  5158. // order indexes by settings
  5159. CSphVector<IndexSettings_t> dLocal ( m_dLocal.GetLength() );
  5160. dLocal.Resize ( 0 );
  5161. ARRAY_FOREACH ( i, m_dLocal )
  5162. {
  5163. dLocal.Add();
  5164. dLocal.Last().m_iLocal = i;
  5165. // TODO: cache settingsFNV on index load
  5166. // FIXME!!! no need to count dictionary hash
  5167. RIdx_c pIndex ( m_dAcquired.Get ( m_dLocal[i].m_sName ) );
  5168. dLocal.Last().m_uHash = pIndex->GetTokenizer()->GetSettingsFNV() ^ pIndex->GetDictionary()->GetSettingsFNV();
  5169. }
  5170. dLocal.Sort ( bind ( &IndexSettings_t::m_uHash ) );
  5171. // gather per-term docs count
  5172. CSphVector < CSphKeywordInfo > dKeywords;
  5173. ARRAY_FOREACH ( i, dLocal )
  5174. {
  5175. int iLocalIndex = dLocal[i].m_iLocal;
  5176. RIdx_c pIndex ( m_dAcquired.Get ( m_dLocal[iLocalIndex].m_sName ) );
  5177. m_iTotalDocs += pIndex->GetStats().m_iTotalDocuments;
  5178. if ( i && dLocal[i].m_uHash==dLocal[i-1].m_uHash )
  5179. {
  5180. dKeywords.Apply ( [] ( CSphKeywordInfo & tKw ) { tKw.m_iDocs = 0; } );
  5181. // no need to tokenize query just fill docs count
  5182. pIndex->FillKeywords ( dKeywords );
  5183. } else
  5184. {
  5185. GetKeywordsSettings_t tSettings;
  5186. tSettings.m_bStats = true;
  5187. dKeywords.Resize ( 0 );
  5188. pIndex->GetKeywords ( dKeywords, dQuery.Begin(), tSettings, NULL );
  5189. // FIXME!!! move duplicate removal to GetKeywords to do less QWord setup and dict searching
  5190. // custom uniq - got rid of word duplicates
  5191. dKeywords.Sort ( bind ( &CSphKeywordInfo::m_sNormalized ) );
  5192. if ( dKeywords.GetLength()>1 )
  5193. {
  5194. int iSrc = 1, iDst = 1;
  5195. while ( iSrc<dKeywords.GetLength() )
  5196. {
  5197. if ( dKeywords[iDst-1].m_sNormalized==dKeywords[iSrc].m_sNormalized )
  5198. iSrc++;
  5199. else
  5200. {
  5201. Swap ( dKeywords[iDst], dKeywords[iSrc] );
  5202. iDst++;
  5203. iSrc++;
  5204. }
  5205. }
  5206. dKeywords.Resize ( iDst );
  5207. }
  5208. }
  5209. for ( auto& tKw: dKeywords )
  5210. {
  5211. int64_t * pDocs = m_hLocalDocs ( tKw.m_sNormalized );
  5212. if ( pDocs )
  5213. *pDocs += tKw.m_iDocs;
  5214. else
  5215. m_hLocalDocs.Add ( tKw.m_iDocs, tKw.m_sNormalized );
  5216. }
  5217. }
  5218. m_bGotLocalDF = true;
  5219. }
  5220. static int GetIndexWeight ( const CSphString& sName, const CSphVector<CSphNamedInt> & dIndexWeights, int iDefaultWeight )
  5221. {
  5222. for ( auto& dWeight : dIndexWeights )
  5223. if ( dWeight.first==sName )
  5224. return dWeight.second;
  5225. // distributed index adds {'*', weight} to all agents in case it got custom weight
  5226. if ( dIndexWeights.GetLength() && dIndexWeights.Last().first=="*" )
  5227. return dIndexWeights[0].second;
  5228. return iDefaultWeight;
  5229. }
  5230. uint64_t CalculateMass ( const CSphIndexStatus & dStats )
  5231. {
  5232. auto iOvermapped = dStats.m_iMapped-dStats.m_iMappedResident;
  5233. if ( iOvermapped<0 ) // it could be negative since resident is rounded up to page edge
  5234. iOvermapped = 0;
  5235. return 1000000 * dStats.m_iNumChunks
  5236. + 10 * iOvermapped
  5237. + dStats.m_iRamUse;
  5238. }
  5239. static uint64_t GetIndexMass ( const CSphString & sName )
  5240. {
  5241. return ServedIndex_c::GetIndexMass ( GetServed ( sName ) );
  5242. }
  5243. // declared to be used in ParseSysVar
  5244. void HandleMysqlShowThreads ( RowBuffer_i & tOut, const SqlStmt_t * pStmt );
  5245. void HandleMysqlShowTables ( RowBuffer_i & tOut, const SqlStmt_t * pStmt );
  5246. void HandleTasks ( RowBuffer_i & tOut );
  5247. void HandleSched ( RowBuffer_i & tOut );
  5248. void HandleShowSessions ( RowBuffer_i& tOut, const SqlStmt_t* pStmt );
  5249. void HandleMysqlDescribe ( RowBuffer_i & tOut, const SqlStmt_t * pStmt );
  5250. void HandleSelectIndexStatus ( RowBuffer_i & tOut, const SqlStmt_t * pStmt );
  5251. void HandleSelectFiles ( RowBuffer_i & tOut, const SqlStmt_t * pStmt );
  5252. bool SearchHandler_c::ParseSysVar ()
  5253. {
  5254. const auto& sVar = m_dLocal.First().m_sName;
  5255. const auto & dSubkeys = m_dNQueries.First ().m_dStringSubkeys;
  5256. if ( sVar=="@@system" )
  5257. {
  5258. if ( !dSubkeys.IsEmpty () )
  5259. {
  5260. bool bSchema = ( dSubkeys.Last ()==".table" );
  5261. bool bValid = true;
  5262. TableFeeder_fn fnFeed;
  5263. if ( dSubkeys[0]==".threads" ) // select .. from @@system.threads
  5264. {
  5265. if ( m_pStmt->m_sThreadFormat.IsEmpty() ) // override format to show all columns by default
  5266. m_pStmt->m_sThreadFormat="all";
  5267. fnFeed = [this] ( RowBuffer_i * pBuf ) { HandleMysqlShowThreads ( *pBuf, m_pStmt ); };
  5268. }
  5269. else if ( dSubkeys[0]==".tables" ) // select .. from @@system.tables
  5270. {
  5271. fnFeed = [this] ( RowBuffer_i * pBuf ) { HandleMysqlShowTables ( *pBuf, m_pStmt ); };
  5272. }
  5273. else if ( dSubkeys[0]==".tasks" ) // select .. from @@system.tasks
  5274. {
  5275. fnFeed = [] ( RowBuffer_i * pBuf ) { HandleTasks ( *pBuf ); };
  5276. }
  5277. else if ( dSubkeys[0]==".sched" ) // select .. from @@system.sched
  5278. {
  5279. fnFeed = [] ( RowBuffer_i * pBuf ) { HandleSched ( *pBuf ); };
  5280. } else if ( dSubkeys[0] == ".sessions" ) // select .. from @@system.sched
  5281. {
  5282. fnFeed = [this] ( RowBuffer_i* pBuf ) { HandleShowSessions ( *pBuf, m_pStmt ); };
  5283. }
  5284. else
  5285. bValid = false;
  5286. if ( bValid )
  5287. {
  5288. cServedIndexRefPtr_c pIndex;
  5289. if ( bSchema )
  5290. {
  5291. m_dLocal.First ().m_sName.SetSprintf( "@@system.%s.table", dSubkeys[0].cstr() );
  5292. pIndex = MakeDynamicIndexSchema ( std::move ( fnFeed ) );
  5293. } else {
  5294. m_dLocal.First ().m_sName.SetSprintf ( "@@system.%s", dSubkeys[0].cstr () );
  5295. pIndex = MakeDynamicIndex ( std::move ( fnFeed ) );
  5296. }
  5297. m_dAcquired.AddIndex ( m_dLocal.First ().m_sName, std::move (pIndex) );
  5298. return true;
  5299. }
  5300. }
  5301. }
  5302. m_sError << "no such variable " << sVar;
  5303. dSubkeys.for_each ( [this] ( const auto& s ) { m_sError << s; } );
  5304. return false;
  5305. }
  5306. bool SearchHandler_c::ParseIdxSubkeys ()
  5307. {
  5308. const auto & sVar = m_dLocal.First ().m_sName;
  5309. const auto & dSubkeys = m_dNQueries.First ().m_dStringSubkeys;
  5310. assert ( !dSubkeys.IsEmpty () );
  5311. bool bSchema = ( dSubkeys.GetLength()>1 && dSubkeys.Last ()==".table" );
  5312. TableFeeder_fn fnFeed;
  5313. if ( dSubkeys[0]==".table" ) // select .. idx.table
  5314. fnFeed = [this] ( RowBuffer_i * pBuf ) { HandleMysqlDescribe ( *pBuf, m_pStmt ); };
  5315. else if ( dSubkeys[0]==".status" ) // select .. idx.status
  5316. fnFeed = [this] ( RowBuffer_i * pBuf ) { HandleSelectIndexStatus ( *pBuf, m_pStmt ); };
  5317. else if ( dSubkeys[0]==".files" ) // select .. from idx.files
  5318. fnFeed = [this] ( RowBuffer_i * pBuf ) { HandleSelectFiles ( *pBuf, m_pStmt ); };
  5319. else
  5320. {
  5321. m_sError << "No such index " << sVar;
  5322. dSubkeys.for_each ([this] (const auto& s) { m_sError << s;});
  5323. return false;
  5324. }
  5325. cServedIndexRefPtr_c pIndex;
  5326. if ( bSchema )
  5327. {
  5328. m_dLocal.First ().m_sName.SetSprintf ( "%s%s.table", sVar.cstr (), dSubkeys[0].cstr () );
  5329. pIndex = MakeDynamicIndexSchema ( std::move ( fnFeed ) );
  5330. } else
  5331. {
  5332. m_dLocal.First ().m_sName.SetSprintf ( "%s%s", sVar.cstr (), dSubkeys[0].cstr () );
  5333. pIndex = MakeDynamicIndex ( std::move ( fnFeed ) );
  5334. }
  5335. m_dAcquired.AddIndex ( m_dLocal.First().m_sName, std::move ( pIndex ) );
  5336. return true;
  5337. }
  5338. ////////////////////////////////////////////////////////////////
  5339. // check for single-query, multi-queue optimization possibility
  5340. ////////////////////////////////////////////////////////////////
  5341. bool SearchHandler_c::CheckMultiQuery() const
  5342. {
  5343. const int iQueries = m_dNQueries.GetLength();
  5344. if ( iQueries<=1 )
  5345. return false;
  5346. const CSphQuery & qFirst = m_dNQueries.First();
  5347. auto dQueries = m_dNQueries.Slice ( 1 );
  5348. // queries over special indexes as status/meta are not capable for multiquery
  5349. if ( !qFirst.m_dStringSubkeys.IsEmpty() )
  5350. return false;
  5351. for ( const CSphQuery & qCheck : dQueries )
  5352. {
  5353. // these parameters must be the same
  5354. if (
  5355. ( qCheck.m_sRawQuery!=qFirst.m_sRawQuery ) || // query string
  5356. ( qCheck.m_dWeights.GetLength ()!=qFirst.m_dWeights.GetLength () ) || // weights count
  5357. ( qCheck.m_dWeights.GetLength () && memcmp ( qCheck.m_dWeights.Begin (), qFirst.m_dWeights.Begin (),
  5358. sizeof ( qCheck.m_dWeights[0] ) * qCheck.m_dWeights.GetLength () ) ) || // weights
  5359. ( qCheck.m_eMode!=qFirst.m_eMode ) || // search mode
  5360. ( qCheck.m_eRanker!=qFirst.m_eRanker ) || // ranking mode
  5361. ( qCheck.m_dFilters.GetLength ()!=qFirst.m_dFilters.GetLength () ) || // attr filters count
  5362. ( qCheck.m_dFilterTree.GetLength ()!=qFirst.m_dFilterTree.GetLength () ) ||
  5363. ( qCheck.m_iCutoff!=qFirst.m_iCutoff ) || // cutoff
  5364. ( qCheck.m_eSort==SPH_SORT_EXPR && qFirst.m_eSort==SPH_SORT_EXPR && qCheck.m_sSortBy!=qFirst.m_sSortBy )
  5365. || // sort expressions
  5366. ( qCheck.m_bGeoAnchor!=qFirst.m_bGeoAnchor ) || // geodist expression
  5367. ( qCheck.m_bGeoAnchor && qFirst.m_bGeoAnchor
  5368. && ( qCheck.m_fGeoLatitude!=qFirst.m_fGeoLatitude
  5369. || qCheck.m_fGeoLongitude!=qFirst.m_fGeoLongitude ) ) ) // some geodist cases
  5370. return false;
  5371. // filters must be the same too
  5372. assert ( qCheck.m_dFilters.GetLength ()==qFirst.m_dFilters.GetLength () );
  5373. assert ( qCheck.m_dFilterTree.GetLength ()==qFirst.m_dFilterTree.GetLength () );
  5374. ARRAY_FOREACH ( i, qCheck.m_dFilters )
  5375. {
  5376. if ( qCheck.m_dFilters[i]!=qFirst.m_dFilters[i] )
  5377. return false;
  5378. }
  5379. ARRAY_FOREACH ( i, qCheck.m_dFilterTree )
  5380. {
  5381. if ( qCheck.m_dFilterTree[i]!=qFirst.m_dFilterTree[i] )
  5382. return false;
  5383. }
  5384. }
  5385. return true;
  5386. }
  5387. // lock local indexes invoked in query
  5388. // Fails if an index is absent and this is not allowed
  5389. bool SearchHandler_c::AcquireInvokedIndexes()
  5390. {
  5391. // if unexistent allowed, short flow
  5392. if ( m_dNQueries.First().m_bIgnoreNonexistentIndexes )
  5393. {
  5394. ARRAY_FOREACH ( i, m_dLocal )
  5395. if ( !m_dAcquired.AddUniqIndex ( m_dLocal[i].m_sName ) )
  5396. m_dLocal.Remove ( i-- );
  5397. return true;
  5398. }
  5399. // _build the list of non-existent
  5400. StringBuilder_c sFailed (", ");
  5401. for ( const auto & dLocal : m_dLocal )
  5402. if ( !m_dAcquired.AddUniqIndex ( dLocal.m_sName ) )
  5403. sFailed << dLocal.m_sName;
  5404. // no absent indexes, viola!
  5405. if ( sFailed.IsEmpty ())
  5406. return true;
  5407. // report failed
  5408. m_sError << "unknown local index(es) '" << sFailed << "' in search request";
  5409. return false;
  5410. }
  5411. // uniq dLocals and copy into m_dLocal only uniq part.
  5412. void SearchHandler_c::UniqLocals ( VecTraits_T<LocalIndex_t> & dLocals )
  5413. {
  5414. int iLen = dLocals.GetLength ();
  5415. if ( !iLen )
  5416. return;
  5417. CSphVector<int> dOrder;
  5418. dOrder.Resize ( dLocals.GetLength() );
  5419. dOrder.FillSeq();
  5420. dOrder.Sort ( Lesser ( [&dLocals] ( int a, int b )
  5421. {
  5422. return ( dLocals[a].m_sName<dLocals[b].m_sName )
  5423. || ( dLocals[a].m_sName==dLocals[b].m_sName && dLocals[a].m_iOrderTag>dLocals[b].m_iOrderTag );
  5424. }));
  5425. int iSrc = 1, iDst = 1;
  5426. while ( iSrc<iLen )
  5427. {
  5428. if ( dLocals[dOrder[iDst-1]].m_sName==dLocals[dOrder[iSrc]].m_sName )
  5429. ++iSrc;
  5430. else
  5431. dOrder[iDst++] = dOrder[iSrc++];
  5432. }
  5433. dOrder.Resize ( iDst );
  5434. m_dLocal.Resize ( iDst );
  5435. ARRAY_FOREACH ( i, dOrder )
  5436. m_dLocal[i] = std::move ( dLocals[dOrder[i]] );
  5437. }
  5438. void SearchHandler_c::CalcTimeStats ( int64_t tmCpu, int64_t tmSubset, const CSphVector<DistrServedByAgent_t> & dDistrServedByAgent )
  5439. {
  5440. // in multi-queue case (1 actual call per N queries), just divide overall query time evenly
  5441. // otherwise (N calls per N queries), divide common query time overheads evenly
  5442. const int iQueries = m_dNQueries.GetLength();
  5443. if ( m_bMultiQueue )
  5444. {
  5445. for ( auto & dResult : m_dNAggrResults )
  5446. {
  5447. dResult.m_iQueryTime = (int)( tmSubset/1000/iQueries );
  5448. dResult.m_iRealQueryTime = (int)( tmSubset/1000/iQueries );
  5449. dResult.m_iCpuTime = tmCpu/iQueries;
  5450. }
  5451. return;
  5452. }
  5453. int64_t tmAccountedWall = 0;
  5454. int64_t tmAccountedCpu = 0;
  5455. for ( const auto & dResult : m_dNAggrResults )
  5456. {
  5457. tmAccountedWall += dResult.m_iQueryTime*1000;
  5458. assert ( ( dResult.m_iCpuTime==0 && dResult.m_iAgentCpuTime==0 ) || // all work was done in this thread
  5459. ( dResult.m_iCpuTime>0 && dResult.m_iAgentCpuTime==0 ) || // children threads work
  5460. ( dResult.m_iAgentCpuTime>0 && dResult.m_iCpuTime==0 ) ); // agents work
  5461. tmAccountedCpu += dResult.m_iCpuTime;
  5462. tmAccountedCpu += dResult.m_iAgentCpuTime;
  5463. }
  5464. // whether we had work done in children threads (dist_threads>1) or in agents
  5465. bool bExternalWork = tmAccountedCpu!=0;
  5466. int64_t tmDeltaWall = ( tmSubset - tmAccountedWall ) / iQueries;
  5467. for ( auto & dResult : m_dNAggrResults )
  5468. {
  5469. dResult.m_iQueryTime += (int)(tmDeltaWall/1000);
  5470. dResult.m_iRealQueryTime = (int)( tmSubset/1000/iQueries );
  5471. dResult.m_iCpuTime = tmCpu/iQueries;
  5472. if ( bExternalWork )
  5473. dResult.m_iCpuTime += tmAccountedCpu;
  5474. }
  5475. // don't forget to add this to stats
  5476. if ( bExternalWork )
  5477. tmCpu += tmAccountedCpu;
  5478. // correct per-index stats from agents
  5479. int iTotalSuccesses = 0;
  5480. for ( const auto & dResult : m_dNAggrResults )
  5481. iTotalSuccesses += dResult.m_iSuccesses;
  5482. if ( !iTotalSuccesses )
  5483. return;
  5484. int64_t tmDelta = tmSubset - tmAccountedWall;
  5485. auto nValidDistrIndexes = dDistrServedByAgent.count_of ( [] ( auto& t ) { return t.m_dStats.any_of ( [] ( auto& i ) { return i.m_iSuccesses; } ); } );
  5486. int64_t nDistrDivider = iTotalSuccesses * nValidDistrIndexes * 1000;
  5487. if ( nDistrDivider )
  5488. for ( auto &tDistrStat : dDistrServedByAgent )
  5489. for ( QueryStat_t& tStat : tDistrStat.m_dStats )
  5490. {
  5491. auto tmDeltaWallAgent = tmDelta * tStat.m_iSuccesses / nDistrDivider;
  5492. tStat.m_uQueryTime += (int)tmDeltaWallAgent;
  5493. }
  5494. auto nValidLocalIndexes = m_dQueryIndexStats.count_of ( [] ( auto& t ) { return t.m_dStats.any_of ( [] ( auto& i ) { return i.m_iSuccesses; } ); } );
  5495. int64_t nLocalDivider = iTotalSuccesses * nValidLocalIndexes * 1000;
  5496. if ( nLocalDivider )
  5497. for ( auto &dQueryIndexStat : m_dQueryIndexStats )
  5498. for ( QueryStat_t& tStat : dQueryIndexStat.m_dStats )
  5499. {
  5500. int64_t tmDeltaWallLocal = tmDelta * tStat.m_iSuccesses / nLocalDivider;
  5501. tStat.m_uQueryTime += (int)tmDeltaWallLocal;
  5502. }
  5503. }
  5504. void SearchHandler_c::CalcPerIndexStats ( const CSphVector<DistrServedByAgent_t> & dDistrServedByAgent ) const
  5505. {
  5506. const int iQueries = m_dNQueries.GetLength();
  5507. // calculate per-index stats
  5508. ARRAY_FOREACH ( iLocal, m_dLocal )
  5509. {
  5510. const auto& pServed = m_dAcquired.Get ( m_dLocal[iLocal].m_sName );
  5511. for ( int iQuery=0; iQuery<iQueries; ++iQuery )
  5512. {
  5513. QueryStat_t & tStat = m_dQueryIndexStats[iLocal].m_dStats[iQuery];
  5514. if ( !tStat.m_iSuccesses )
  5515. continue;
  5516. pServed->m_pStats->AddQueryStat ( tStat.m_uFoundRows, tStat.m_uQueryTime );
  5517. for ( auto &tDistr : dDistrServedByAgent )
  5518. {
  5519. if ( tDistr.m_dLocalNames.Contains ( m_dLocal[iLocal].m_sName ) )
  5520. {
  5521. tDistr.m_dStats[iQuery].m_uQueryTime += tStat.m_uQueryTime;
  5522. tDistr.m_dStats[iQuery].m_uFoundRows += tStat.m_uFoundRows;
  5523. ++tDistr.m_dStats[iQuery].m_iSuccesses;
  5524. }
  5525. }
  5526. }
  5527. }
  5528. for ( auto &tDistr : dDistrServedByAgent )
  5529. {
  5530. auto pServedDistIndex = GetDistr ( tDistr.m_sIndex );
  5531. if ( pServedDistIndex )
  5532. for ( int iQuery=0; iQuery<iQueries; ++iQuery )
  5533. {
  5534. auto & tStat = tDistr.m_dStats[iQuery];
  5535. if ( !tStat.m_iSuccesses )
  5536. continue;
  5537. pServedDistIndex->m_tStats.AddQueryStat ( tStat.m_uFoundRows, tStat.m_uQueryTime );
  5538. }
  5539. }
  5540. }
  5541. void SearchHandler_c::CalcGlobalStats ( int64_t tmCpu, int64_t tmSubset, int64_t tmLocal, const CSphIOStats & tIO, const VecRefPtrsAgentConn_t & dRemotes ) const
  5542. {
  5543. auto & g_tStats = gStats ();
  5544. g_tStats.m_iQueries.fetch_add ( m_dNQueries.GetLength (), std::memory_order_relaxed );
  5545. g_tStats.m_iQueryTime.fetch_add ( tmSubset, std::memory_order_relaxed );
  5546. g_tStats.m_iQueryCpuTime.fetch_add ( tmCpu, std::memory_order_relaxed );
  5547. if ( dRemotes.GetLength() )
  5548. {
  5549. int64_t tmWait = 0;
  5550. for ( const AgentConn_t * pAgent : dRemotes )
  5551. tmWait += pAgent->m_iWaited;
  5552. // do *not* count queries to dist indexes w/o actual remote agents
  5553. g_tStats.m_iDistQueries.fetch_add ( 1, std::memory_order_relaxed );
  5554. g_tStats.m_iDistWallTime.fetch_add ( tmSubset, std::memory_order_relaxed );
  5555. g_tStats.m_iDistLocalTime.fetch_add ( tmLocal, std::memory_order_relaxed );
  5556. g_tStats.m_iDistWaitTime.fetch_add ( tmWait, std::memory_order_relaxed );
  5557. }
  5558. g_tStats.m_iDiskReads.fetch_add ( tIO.m_iReadOps, std::memory_order_relaxed );
  5559. g_tStats.m_iDiskReadTime.fetch_add ( tIO.m_iReadTime, std::memory_order_relaxed );
  5560. g_tStats.m_iDiskReadBytes.fetch_add ( tIO.m_iReadBytes, std::memory_order_relaxed );
  5561. }
  5562. static CSphVector<LocalIndex_t> CollectAllLocalIndexes ( const CSphVector<CSphNamedInt> & dIndexWeights )
  5563. {
  5564. CSphVector<LocalIndex_t> dIndexes;
  5565. int iOrderTag = 0;
  5566. // search through all local indexes
  5567. ServedSnap_t hLocal = g_pLocalIndexes->GetHash();
  5568. for ( auto& tIt : *hLocal )
  5569. {
  5570. if ( !tIt.second ) // fixme! should never be...
  5571. continue;
  5572. auto & dLocal = dIndexes.Add ();
  5573. dLocal.m_sName = tIt.first;
  5574. dLocal.m_iOrderTag = iOrderTag++;
  5575. dLocal.m_iWeight = GetIndexWeight ( tIt.first, dIndexWeights, 1 );
  5576. dLocal.m_iMass = ServedIndex_c::GetIndexMass ( tIt.second );
  5577. }
  5578. return dIndexes;
  5579. }
  5580. // returns true = real indexes, false = sysvar (i.e. only one 'index' named from @@)
  5581. bool SearchHandler_c::BuildIndexList ( int & iDivideLimits, VecRefPtrsAgentConn_t & dRemotes, CSphVector<DistrServedByAgent_t> & dDistrServedByAgent )
  5582. {
  5583. const CSphQuery & tQuery = m_dNQueries.First ();
  5584. if ( tQuery.m_sIndexes=="*" )
  5585. {
  5586. // they're all local, build the list
  5587. m_dLocal = CollectAllLocalIndexes ( tQuery.m_dIndexWeights );
  5588. return true;
  5589. }
  5590. m_dLocal.Reset ();
  5591. int iOrderTag = 0;
  5592. bool bSysVar = tQuery.m_sIndexes.Begins ( "@@" );
  5593. // search through specified local indexes
  5594. StrVec_t dIdxNames;
  5595. if ( bSysVar )
  5596. dIdxNames.Add ( tQuery.m_sIndexes );
  5597. else
  5598. ParseIndexList ( tQuery.m_sIndexes, dIdxNames );
  5599. const int iQueries = m_dNQueries.GetLength ();
  5600. CSphVector<LocalIndex_t> dLocals;
  5601. int iDistCount = 0;
  5602. bool bDivideRemote = false;
  5603. bool bHasLocalsAgents = false;
  5604. for ( const auto& sIndex : dIdxNames )
  5605. {
  5606. auto pDist = GetDistr ( sIndex );
  5607. if ( !pDist )
  5608. {
  5609. auto &dLocal = dLocals.Add ();
  5610. dLocal.m_sName = sIndex;
  5611. dLocal.m_iOrderTag = iOrderTag++;
  5612. dLocal.m_iWeight = GetIndexWeight ( sIndex, tQuery.m_dIndexWeights, 1 );
  5613. dLocal.m_iMass = GetIndexMass ( sIndex );
  5614. } else
  5615. {
  5616. ++iDistCount;
  5617. int iWeight = GetIndexWeight ( sIndex, tQuery.m_dIndexWeights, -1 );
  5618. auto & tDistrStat = dDistrServedByAgent.Add();
  5619. tDistrStat.m_sIndex = sIndex;
  5620. tDistrStat.m_dStats.Resize ( iQueries );
  5621. tDistrStat.m_dStats.ZeroVec();
  5622. for ( const auto& pAgent : pDist->m_dAgents )
  5623. {
  5624. tDistrStat.m_dAgentIds.Add ( dRemotes.GetLength() );
  5625. auto * pConn = new AgentConn_t;
  5626. pConn->SetMultiAgent ( pAgent );
  5627. pConn->m_iStoreTag = iOrderTag++;
  5628. pConn->m_iWeight = iWeight;
  5629. pConn->m_iMyConnectTimeoutMs = pDist->m_iAgentConnectTimeoutMs;
  5630. pConn->m_iMyQueryTimeoutMs = pDist->m_iAgentQueryTimeoutMs;
  5631. dRemotes.Add ( pConn );
  5632. }
  5633. ARRAY_CONSTFOREACH ( j, pDist->m_dLocal )
  5634. {
  5635. const CSphString& sLocalAgent = pDist->m_dLocal[j];
  5636. tDistrStat.m_dLocalNames.Add ( sLocalAgent );
  5637. auto &dLocal = dLocals.Add ();
  5638. dLocal.m_sName = sLocalAgent;
  5639. dLocal.m_iOrderTag = iOrderTag++;
  5640. if ( iWeight!=-1 )
  5641. dLocal.m_iWeight = iWeight;
  5642. dLocal.m_iMass = GetIndexMass ( sLocalAgent );
  5643. dLocal.m_sParentIndex = sIndex;
  5644. bHasLocalsAgents = true;
  5645. }
  5646. bDivideRemote |= pDist->m_bDivideRemoteRanges;
  5647. }
  5648. }
  5649. // set remote divider
  5650. if ( bDivideRemote )
  5651. {
  5652. if ( iDistCount==1 )
  5653. iDivideLimits = dRemotes.GetLength();
  5654. else
  5655. {
  5656. for ( auto& dResult : m_dNAggrResults )
  5657. dResult.m_sWarning.SetSprintf ( "distributed multi-index query '%s' doesn't support divide_remote_ranges", tQuery.m_sIndexes.cstr() );
  5658. }
  5659. }
  5660. // eliminate local dupes that come from distributed indexes
  5661. if ( bHasLocalsAgents )
  5662. UniqLocals ( dLocals );
  5663. else
  5664. m_dLocal.SwapData ( dLocals );
  5665. return !bSysVar;
  5666. }
  5667. // query info - render query into the view
  5668. struct QueryInfo_t : public TaskInfo_t
  5669. {
  5670. DECLARE_RENDER( QueryInfo_t );
  5671. // actually it is 'virtually hazard'. Don't care about query* itself, however later in dtr of Searchandler_t
  5672. // will work with refs to members of it's m_dQueries and retire or whole vec.
  5673. std::atomic<const CSphQuery *> m_pHazardQuery;
  5674. };
  5675. DEFINE_RENDER ( QueryInfo_t )
  5676. {
  5677. auto & tInfo = *(QueryInfo_t *) pSrc;
  5678. dDst.m_sChain << "Query ";
  5679. hazard::Guard_c tGuard;
  5680. auto pQuery = tGuard.Protect ( tInfo.m_pHazardQuery );
  5681. if ( pQuery && session::GetProto()!=Proto_e::MYSQL41 ) // cheat: for mysql query not used, so will not copy it then
  5682. dDst.m_pQuery = std::make_unique<CSphQuery> ( *pQuery );
  5683. }
  5684. // one or more queries against one and same set of indexes
  5685. void SearchHandler_c::RunSubset ( int iStart, int iEnd )
  5686. {
  5687. int iQueries = iEnd - iStart;
  5688. m_dNQueries = m_dQueries.Slice ( iStart, iQueries );
  5689. m_dNAggrResults = m_dAggrResults.Slice ( iStart, iQueries );
  5690. m_dNResults = m_dResults.Slice ( iStart, iQueries );
  5691. m_dNFailuresSet = m_dFailuresSet.Slice ( iStart, iQueries );
  5692. // we've own scoped context here
  5693. auto pQueryInfo = new QueryInfo_t;
  5694. pQueryInfo->m_pHazardQuery.store ( m_dNQueries.begin(), std::memory_order_release );
  5695. ScopedInfo_T<QueryInfo_t> pTlsQueryInfo ( pQueryInfo );
  5696. // all my stats
  5697. int64_t tmSubset = -sphMicroTimer();
  5698. int64_t tmLocal = 0;
  5699. int64_t tmCpu = -sphTaskCpuTimer ();
  5700. CSphScopedProfile tProf ( m_pProfile, SPH_QSTATE_UNKNOWN );
  5701. // prepare for descent
  5702. const CSphQuery & tFirst = m_dNQueries.First();
  5703. m_dNAggrResults.Apply ( [] ( AggrResult_t & r ) { r.m_iSuccesses = 0; } );
  5704. if ( iQueries==1 && m_pProfile )
  5705. {
  5706. m_dNAggrResults.First().m_pProfile = m_pProfile;
  5707. m_tHook.SetProfiler ( m_pProfile );
  5708. }
  5709. // check for facets
  5710. m_bFacetQueue = iQueries>1;
  5711. for ( int iCheck = 1; iCheck<m_dNQueries.GetLength () && m_bFacetQueue; ++iCheck )
  5712. if ( !m_dNQueries[iCheck].m_bFacet )
  5713. m_bFacetQueue = false;
  5714. m_bMultiQueue = m_bFacetQueue || CheckMultiQuery();
  5715. ////////////////////////////
  5716. // build local indexes list
  5717. ////////////////////////////
  5718. VecRefPtrsAgentConn_t dRemotes;
  5719. CSphVector<DistrServedByAgent_t> dDistrServedByAgent;
  5720. int iDivideLimits = 1;
  5721. auto fnError = AtScopeExit ( [this]()
  5722. {
  5723. if ( !m_sError.IsEmpty() )
  5724. m_dNAggrResults.for_each ( [this] ( auto& r ) { r.m_sError = (CSphString) m_sError; } );
  5725. });
  5726. if ( BuildIndexList ( iDivideLimits, dRemotes, dDistrServedByAgent ) )
  5727. {
  5728. // process query to meta, as myindex.status, etc.
  5729. if ( !tFirst.m_dStringSubkeys.IsEmpty () )
  5730. {
  5731. // if apply subkeys ... else return
  5732. if ( !ParseIdxSubkeys () )
  5733. return;
  5734. } else if ( !AcquireInvokedIndexes () ) // usual query processing
  5735. return;
  5736. } else
  5737. {
  5738. // process query to @@*, as @@system.threads, etc.
  5739. if ( !ParseSysVar () )
  5740. return;
  5741. // here we deal
  5742. }
  5743. // at this point m_dLocal contains list of valid local indexes (i.e., existing ones),
  5744. // and these indexes are also rlocked and available by calling m_dAcquired.Get()
  5745. // sanity check
  5746. if ( dRemotes.IsEmpty() && m_dLocal.IsEmpty() )
  5747. {
  5748. m_sError << "no enabled indexes to search";
  5749. return;
  5750. }
  5751. // select lists must have no expressions
  5752. if ( m_bMultiQueue )
  5753. m_bMultiQueue = AllowsMulti ();
  5754. assert ( !m_bFacetQueue || AllowsMulti () );
  5755. if ( !m_bMultiQueue )
  5756. m_bFacetQueue = false;
  5757. ///////////////////////////////////////////////////////////
  5758. // main query loop (with multiple retries for distributed)
  5759. ///////////////////////////////////////////////////////////
  5760. // connect to remote agents and query them, if required
  5761. std::unique_ptr<SearchRequestBuilder_c> tReqBuilder;
  5762. CSphRefcountedPtr<RemoteAgentsObserver_i> tReporter { nullptr };
  5763. std::unique_ptr<ReplyParser_i> tParser;;
  5764. if ( !dRemotes.IsEmpty() )
  5765. {
  5766. SwitchProfile(m_pProfile, SPH_QSTATE_DIST_CONNECT);
  5767. tReqBuilder = std::make_unique<SearchRequestBuilder_c> ( m_dNQueries, iDivideLimits );
  5768. tParser = std::make_unique<SearchReplyParser_c> ( iQueries );
  5769. tReporter = GetObserver();
  5770. // run remote queries. tReporter will tell us when they're finished.
  5771. // also blackholes will be removed from this flow of remotes.
  5772. ScheduleDistrJobs ( dRemotes, tReqBuilder.get (),
  5773. tParser.get (),
  5774. tReporter, tFirst.m_iRetryCount, tFirst.m_iRetryDelay );
  5775. }
  5776. /////////////////////
  5777. // run local queries
  5778. //////////////////////
  5779. // while the remote queries are running, do local searches
  5780. if ( m_dLocal.GetLength() )
  5781. {
  5782. SetupLocalDF();
  5783. SwitchProfile ( m_pProfile, SPH_QSTATE_LOCAL_SEARCH );
  5784. m_bNeedDocIDs = m_dLocal.GetLength()+dRemotes.GetLength()>1;
  5785. tmLocal = -sphMicroTimer();
  5786. tmCpu -= sphTaskCpuTimer ();
  5787. RunLocalSearches();
  5788. tmCpu += sphTaskCpuTimer ();
  5789. tmLocal += sphMicroTimer();
  5790. }
  5791. ///////////////////////
  5792. // poll remote queries
  5793. ///////////////////////
  5794. if ( !dRemotes.IsEmpty() )
  5795. {
  5796. SwitchProfile ( m_pProfile, SPH_QSTATE_DIST_WAIT );
  5797. bool bDistDone = false;
  5798. while ( !bDistDone )
  5799. {
  5800. // don't forget to check incoming replies after send was over
  5801. bDistDone = tReporter->IsDone();
  5802. if ( !bDistDone )
  5803. tReporter->WaitChanges (); /// wait one or more remote queries to complete. Note! M.b. context switch!
  5804. ARRAY_FOREACH ( iAgent, dRemotes )
  5805. {
  5806. AgentConn_t * pAgent = dRemotes[iAgent];
  5807. assert ( !pAgent->IsBlackhole () ); // must not be any blacknole here.
  5808. if ( !pAgent->m_bSuccess )
  5809. continue;
  5810. sphLogDebugv ( "agent %d, state %s, order %d, sock %d", iAgent, pAgent->StateName(), pAgent->m_iStoreTag, pAgent->m_iSock );
  5811. DistrServedByAgent_t * pDistr = nullptr;
  5812. for ( auto &tDistr : dDistrServedByAgent )
  5813. if ( tDistr.m_dAgentIds.Contains ( iAgent ) )
  5814. {
  5815. pDistr = &tDistr;
  5816. break;
  5817. }
  5818. assert ( pDistr );
  5819. // merge this agent's results
  5820. for ( int iRes = 0; iRes<iQueries; ++iRes )
  5821. {
  5822. auto pResult = ( cSearchResult * ) pAgent->m_pResult.get ();
  5823. if ( !pResult )
  5824. continue;
  5825. auto &tRemoteResult = pResult->m_dResults[iRes];
  5826. // copy errors or warnings
  5827. if ( !tRemoteResult.m_sError.IsEmpty() )
  5828. m_dNFailuresSet[iRes].SubmitEx ( tFirst.m_sIndexes, nullptr,
  5829. "agent %s: remote query error: %s",
  5830. pAgent->m_tDesc.GetMyUrl().cstr(), tRemoteResult.m_sError.cstr() );
  5831. if ( !tRemoteResult.m_sWarning.IsEmpty() )
  5832. m_dNFailuresSet[iRes].SubmitEx ( tFirst.m_sIndexes, nullptr,
  5833. "agent %s: remote query warning: %s",
  5834. pAgent->m_tDesc.GetMyUrl().cstr(), tRemoteResult.m_sWarning.cstr() );
  5835. if ( tRemoteResult.m_iSuccesses<=0 )
  5836. continue;
  5837. AggrResult_t & tRes = m_dNAggrResults[iRes];
  5838. ++tRes.m_iSuccesses;
  5839. assert ( tRemoteResult.m_dResults.GetLength() == 1 ); // by design remotes return one chunk
  5840. auto & dRemoteChunk = tRes.m_dResults.Add ();
  5841. ::Swap ( dRemoteChunk, *tRemoteResult.m_dResults.begin () );
  5842. // note how we do NOT add per-index weight here
  5843. // merge this agent's stats
  5844. tRes.m_iTotalMatches += tRemoteResult.m_iTotalMatches;
  5845. tRes.m_bTotalMatchesApprox |= tRemoteResult.m_bTotalMatchesApprox;
  5846. tRes.m_iQueryTime += tRemoteResult.m_iQueryTime;
  5847. tRes.m_iAgentCpuTime += tRemoteResult.m_iCpuTime;
  5848. tRes.m_tAgentIOStats.Add ( tRemoteResult.m_tIOStats );
  5849. tRes.m_iAgentPredictedTime += tRemoteResult.m_iPredictedTime;
  5850. tRes.m_iAgentFetchedDocs += tRemoteResult.m_iAgentFetchedDocs;
  5851. tRes.m_iAgentFetchedHits += tRemoteResult.m_iAgentFetchedHits;
  5852. tRes.m_iAgentFetchedSkips += tRemoteResult.m_iAgentFetchedSkips;
  5853. tRes.m_bHasPrediction |= ( m_dNQueries[iRes].m_iMaxPredictedMsec>0 );
  5854. if ( pDistr )
  5855. {
  5856. pDistr->m_dStats[iRes].m_uQueryTime += tRemoteResult.m_iQueryTime;
  5857. pDistr->m_dStats[iRes].m_uFoundRows += tRemoteResult.m_iTotalMatches;
  5858. ++pDistr->m_dStats[iRes].m_iSuccesses;
  5859. }
  5860. // merge this agent's words
  5861. tRes.MergeWordStats ( tRemoteResult );
  5862. }
  5863. // dismissed
  5864. if ( pAgent->m_pResult )
  5865. pAgent->m_pResult->Reset ();
  5866. pAgent->m_bSuccess = false;
  5867. pAgent->m_sFailure = "";
  5868. }
  5869. } // while ( !bDistDone )
  5870. // submit failures from failed agents
  5871. // copy timings from all agents
  5872. for ( const AgentConn_t * pAgent : dRemotes )
  5873. {
  5874. assert ( !pAgent->IsBlackhole () ); // must not be any blacknole here.
  5875. for ( int j=iStart; j<iEnd; ++j )
  5876. {
  5877. assert ( pAgent->m_iWall>=0 );
  5878. m_dAgentTimes[j].Add ( ( pAgent->m_iWall ) / ( 1000 * iQueries ) );
  5879. }
  5880. if ( !pAgent->m_bSuccess && !pAgent->m_sFailure.IsEmpty() )
  5881. for ( int j=0; j<iQueries; ++j )
  5882. m_dNFailuresSet[j].SubmitEx ( tFirst.m_sIndexes, nullptr, "agent %s: %s",
  5883. pAgent->m_tDesc.GetMyUrl().cstr(), pAgent->m_sFailure.cstr() );
  5884. }
  5885. }
  5886. /////////////////////
  5887. // merge all results
  5888. /////////////////////
  5889. SwitchProfile ( m_pProfile, SPH_QSTATE_AGGREGATE );
  5890. CSphIOStats tIO;
  5891. for ( int iRes=0; iRes<iQueries; ++iRes )
  5892. {
  5893. sph::StringSet hExtra;
  5894. for ( const CSphString & sExtra : m_dExtraSchema )
  5895. hExtra.Add ( sExtra );
  5896. AggrResult_t & tRes = m_dNAggrResults[iRes];
  5897. const CSphQuery & tQuery = m_dNQueries[iRes];
  5898. // minimize sorters needs these pointers
  5899. tIO.Add ( tRes.m_tIOStats );
  5900. // if there were no successful searches at all, this is an error
  5901. if ( !tRes.m_iSuccesses )
  5902. {
  5903. StringBuilder_c sFailures;
  5904. m_dNFailuresSet[iRes].BuildReport ( sFailures );
  5905. sFailures.MoveTo (tRes.m_sError);
  5906. continue;
  5907. }
  5908. if ( tRes.m_dResults.IsEmpty () ) // fixup. It is easier to have single empty result, then check each time.
  5909. {
  5910. auto& tEmptyRes = tRes.m_dResults.Add ();
  5911. tEmptyRes.m_tSchema = tRes.m_tSchema;
  5912. }
  5913. // minimize schema and remove dupes
  5914. // assuming here ( tRes.m_tSchema==tRes.m_dSchemas[0] )
  5915. const CSphFilterSettings * pAggrFilter = nullptr;
  5916. if ( m_bMaster && !tQuery.m_tHaving.m_sAttrName.IsEmpty() )
  5917. pAggrFilter = &tQuery.m_tHaving;
  5918. const CSphVector<CSphQueryItem> & dItems = ( tQuery.m_dRefItems.GetLength() ? tQuery.m_dRefItems : tQuery.m_dItems );
  5919. if ( tRes.m_iSuccesses>1 || dItems.GetLength() || pAggrFilter )
  5920. {
  5921. if ( m_bMaster && tRes.m_iSuccesses && dItems.GetLength() && tQuery.m_sGroupBy.IsEmpty() && tRes.GetLength()==0 )
  5922. {
  5923. for ( auto& dItem : dItems )
  5924. {
  5925. if ( dItem.m_sExpr=="count(*)" || ( dItem.m_sExpr=="@distinct" ) )
  5926. tRes.m_dZeroCount.Add ( dItem.m_sAlias );
  5927. }
  5928. }
  5929. bool bOk = MinimizeAggrResult ( tRes, tQuery, !m_dLocal.IsEmpty(), hExtra, m_pProfile, pAggrFilter, m_bFederatedUser, m_bMaster );
  5930. if ( !bOk )
  5931. {
  5932. tRes.m_iSuccesses = 0;
  5933. continue;
  5934. }
  5935. } else if ( !tRes.m_dResults.IsEmpty() )
  5936. {
  5937. tRes.m_tSchema = tRes.m_dResults.First ().m_tSchema;
  5938. Debug ( tRes.m_bOneSchema = true; )
  5939. }
  5940. if ( !m_dNFailuresSet[iRes].IsEmpty() )
  5941. {
  5942. StringBuilder_c sFailures;
  5943. m_dNFailuresSet[iRes].BuildReport ( sFailures );
  5944. sFailures.MoveTo ( tRes.m_sWarning );
  5945. }
  5946. ////////////
  5947. // finalize
  5948. ////////////
  5949. tRes.m_iOffset = Max ( tQuery.m_iOffset, tQuery.m_iOuterOffset );
  5950. auto iLimit = ( tQuery.m_iOuterLimit ? tQuery.m_iOuterLimit : tQuery.m_iLimit );
  5951. tRes.m_iCount = Max ( Min ( iLimit, tRes.GetLength()-tRes.m_iOffset ), 0 );
  5952. }
  5953. /////////////////////////////////
  5954. // functions on a table argument
  5955. /////////////////////////////////
  5956. for ( int i=0; i<iQueries; ++i )
  5957. {
  5958. AggrResult_t & tRes = m_dNAggrResults[i];
  5959. auto& pTableFunc = m_dTables[iStart+i];
  5960. // FIXME! log such queries properly?
  5961. if ( pTableFunc )
  5962. {
  5963. SwitchProfile ( m_pProfile, SPH_QSTATE_TABLE_FUNC );
  5964. if ( !pTableFunc->Process ( &tRes, tRes.m_sError ) )
  5965. tRes.m_iSuccesses = 0;
  5966. }
  5967. }
  5968. /////////
  5969. // stats
  5970. /////////
  5971. tmSubset += sphMicroTimer();
  5972. tmCpu += sphTaskCpuTimer();
  5973. CalcTimeStats ( tmCpu, tmSubset, dDistrServedByAgent );
  5974. CalcPerIndexStats ( dDistrServedByAgent );
  5975. CalcGlobalStats ( tmCpu, tmSubset, tmLocal, tIO, dRemotes );
  5976. }
  5977. bool CheckCommandVersion ( WORD uVer, WORD uDaemonVersion, ISphOutputBuffer & tOut )
  5978. {
  5979. if ( ( uVer>>8)!=( uDaemonVersion>>8) )
  5980. {
  5981. SendErrorReply ( tOut, "major command version mismatch (expected v.%d.x, got v.%d.%d)",
  5982. uDaemonVersion>>8, uVer>>8, uVer&0xff );
  5983. return false;
  5984. }
  5985. if ( uVer>uDaemonVersion )
  5986. {
  5987. SendErrorReply ( tOut, "client version is higher than daemon version (client is v.%d.%d, daemon is v.%d.%d)",
  5988. uVer>>8, uVer&0xff, uDaemonVersion>>8, uDaemonVersion&0xff );
  5989. return false;
  5990. }
  5991. return true;
  5992. }
  5993. bool IsMaxedOut ()
  5994. {
  5995. if ( session::GetVip () )
  5996. return false;
  5997. if ( g_iThdQueueMax!=0 )
  5998. return GlobalWorkPool()->Works() > g_iThdQueueMax; // that is "jobs_queue_size" param of searchd conf, "work_queue_length" in 'show status', or "Queue:" in 'status'
  5999. if ( g_iMaxConnection!=0 )
  6000. return myinfo::CountClients() > g_iMaxConnection; // that is "max_connections" param of searchd.conf, "workers_clients" in 'show status', or "Clients:" in 'status'
  6001. return false;
  6002. }
  6003. bool IsReadOnly ()
  6004. {
  6005. return session::GetReadOnly();
  6006. }
  6007. bool sphCheckWeCanModify()
  6008. {
  6009. return !IsReadOnly();
  6010. }
  6011. bool sphCheckWeCanModify ( StmtErrorReporter_i& tOut )
  6012. {
  6013. if ( sphCheckWeCanModify() )
  6014. return true;
  6015. tOut.Error ( "connection is read-only");
  6016. return false;
  6017. }
  6018. bool sphCheckWeCanModify ( const char* szStmt, RowBuffer_i& tOut )
  6019. {
  6020. if ( sphCheckWeCanModify() )
  6021. return true;
  6022. tOut.Error ( szStmt, "connection is read-only" );
  6023. return false;
  6024. }
  6025. void HandleCommandSearch ( ISphOutputBuffer & tOut, WORD uVer, InputBuffer_c & tReq )
  6026. {
  6027. MEMORY ( MEM_API_SEARCH );
  6028. if ( !CheckCommandVersion ( uVer, VER_COMMAND_SEARCH, tOut ) )
  6029. return;
  6030. const WORD MIN_VERSION = 0x119;
  6031. if ( uVer<MIN_VERSION )
  6032. {
  6033. SendErrorReply ( tOut, "client version is too old; upgrade your client (client is v.%d.%d, min is v.%d.%d)", uVer>>8, uVer&0xff, MIN_VERSION>>8, MIN_VERSION&0xff );
  6034. return;
  6035. }
  6036. int iMasterVer = tReq.GetInt();
  6037. if ( iMasterVer<0 || iMasterVer>VER_COMMAND_SEARCH_MASTER )
  6038. {
  6039. SendErrorReply ( tOut, "master-agent version mismatch; update me first, then update master!" );
  6040. return;
  6041. }
  6042. WORD uMasterVer { WORD (iMasterVer) };
  6043. bool bAgentMode = ( uMasterVer>0 );
  6044. // parse request
  6045. int iQueries = tReq.GetDword ();
  6046. if ( g_iMaxBatchQueries>0 && ( iQueries<=0 || iQueries>g_iMaxBatchQueries ) )
  6047. {
  6048. SendErrorReply ( tOut, "bad multi-query count %d (must be in 1..%d range)", iQueries, g_iMaxBatchQueries );
  6049. return;
  6050. }
  6051. SearchHandler_c tHandler ( iQueries, nullptr, QUERY_API, ( iMasterVer==0 ) );
  6052. for ( auto &dQuery : tHandler.m_dQueries )
  6053. if ( !ParseSearchQuery ( tReq, tOut, dQuery, uVer, uMasterVer ) )
  6054. return;
  6055. if ( !tHandler.m_dQueries.IsEmpty() )
  6056. {
  6057. QueryType_e eQueryType = tHandler.m_dQueries[0].m_eQueryType;
  6058. #ifndef NDEBUG
  6059. // we assume that all incoming queries have the same type
  6060. for ( const auto & i: tHandler.m_dQueries )
  6061. assert ( i.m_eQueryType==eQueryType );
  6062. #endif
  6063. std::unique_ptr<QueryParser_i> pParser;
  6064. if ( eQueryType==QUERY_JSON )
  6065. pParser = sphCreateJsonQueryParser();
  6066. else
  6067. pParser = sphCreatePlainQueryParser();
  6068. assert ( pParser );
  6069. tHandler.SetQueryParser ( std::move ( pParser ), eQueryType );
  6070. const CSphQuery & q = tHandler.m_dQueries[0];
  6071. myinfo::SetTaskInfo ( R"(api-search query="%s" comment="%s" index="%s")", q.m_sQuery.scstr (), q.m_sComment.scstr (), q.m_sIndexes.scstr () );
  6072. }
  6073. // run queries, send response
  6074. tHandler.RunQueries();
  6075. auto tReply = APIAnswer ( tOut, VER_COMMAND_SEARCH );
  6076. ARRAY_FOREACH ( i, tHandler.m_dQueries )
  6077. SendResult ( uVer, tOut, tHandler.m_dAggrResults[i], bAgentMode, tHandler.m_dQueries[i], uMasterVer );
  6078. int64_t iTotalPredictedTime = 0;
  6079. int64_t iTotalAgentPredictedTime = 0;
  6080. for ( const auto& dResult : tHandler.m_dAggrResults )
  6081. {
  6082. iTotalPredictedTime += dResult.m_iPredictedTime;
  6083. iTotalAgentPredictedTime += dResult.m_iAgentPredictedTime;
  6084. }
  6085. auto & g_tStats = gStats ();
  6086. g_tStats.m_iPredictedTime.fetch_add ( iTotalPredictedTime, std::memory_order_relaxed );
  6087. g_tStats.m_iAgentPredictedTime.fetch_add ( iTotalAgentPredictedTime, std::memory_order_relaxed );
  6088. ScWL_t dLastMetaLock ( g_tLastMetaLock );
  6089. g_tLastMeta = tHandler.m_dAggrResults.Last();
  6090. }
  6091. //////////////////////////////////////////////////////////////////////////
  6092. // TABLE FUNCTIONS
  6093. //////////////////////////////////////////////////////////////////////////
  6094. // table functions take an arbitrary result set as their input,
  6095. // and return a new, processed, (completely) different one as their output
  6096. //
  6097. // 1st argument should be the input result set, but a table function
  6098. // can optionally take and handle more arguments
  6099. //
  6100. // table function can completely (!) change the result set
  6101. // including (!) the schema
  6102. //
  6103. // for now, only builtin table functions are supported
  6104. // UDFs are planned when the internal call interface is stabilized
  6105. #define LOC_ERROR(_msg) { sError = _msg; return false; }
  6106. #define LOC_ERROR1(_msg,_arg1) { sError.SetSprintf ( _msg, _arg1 ); return false; }
  6107. class CSphTableFuncRemoveRepeats final : public ISphTableFunc
  6108. {
  6109. CSphString m_sCol;
  6110. int m_iOffset;
  6111. int m_iLimit;
  6112. public:
  6113. bool ValidateArgs ( const StrVec_t & dArgs, const CSphQuery &, CSphString & sError ) final
  6114. {
  6115. if ( dArgs.GetLength()!=3 )
  6116. LOC_ERROR ( "REMOVE_REPEATS() requires 4 arguments (result_set, column, offset, limit)" );
  6117. if ( !isdigit ( *dArgs[1].cstr() ) )
  6118. LOC_ERROR ( "REMOVE_REPEATS() argument 3 (offset) must be integer" );
  6119. if ( !isdigit ( *dArgs[2].cstr() ) )
  6120. LOC_ERROR ( "REMOVE_REPEATS() argument 4 (limit) must be integer" );
  6121. m_sCol = dArgs[0];
  6122. m_iOffset = atoi ( dArgs[1].cstr() );
  6123. m_iLimit = atoi ( dArgs[2].cstr() );
  6124. if ( !m_iLimit )
  6125. LOC_ERROR ( "REMOVE_REPEATS() argument 4 (limit) must be greater than 0" );
  6126. return true;
  6127. }
  6128. bool Process ( AggrResult_t * pResult, CSphString & sError ) final
  6129. {
  6130. assert ( pResult );
  6131. assert ( pResult->m_bOneSchema );
  6132. assert ( pResult->m_bSingle );
  6133. assert ( !pResult->m_dResults.IsEmpty () );
  6134. auto& dMatches = pResult->m_dResults.First().m_dMatches;
  6135. if ( dMatches.IsEmpty() )
  6136. return true;
  6137. // get subset expressing 'LIMIT N,M'
  6138. // LIMIT N,M clause must be applied before (!) table function
  6139. // so we scan source matches N to N+M-1
  6140. //
  6141. // within those matches, we filter out repeats in a given column,
  6142. // skip first m_iOffset eligible ones, and emit m_iLimit more
  6143. auto dSubMatches = dMatches.Slice ( pResult->m_iOffset, pResult->m_iCount );
  6144. if ( dSubMatches.IsEmpty() )
  6145. return true;
  6146. const CSphColumnInfo * pCol = pResult->m_tSchema.GetAttr ( m_sCol.cstr() );
  6147. if ( !pCol )
  6148. LOC_ERROR1 ( "REMOVE_REPEATS() argument 2 (column %s) not found in result set", m_sCol.cstr() );
  6149. ESphAttr t = pCol->m_eAttrType;
  6150. if ( t!=SPH_ATTR_INTEGER && t!=SPH_ATTR_BIGINT && t!=SPH_ATTR_TOKENCOUNT && t!=SPH_ATTR_STRINGPTR && t!=SPH_ATTR_STRING )
  6151. LOC_ERROR1 ( "REMOVE_REPEATS() argument 2 (column %s) must be of INTEGER, BIGINT, or STRINGPTR type", m_sCol.cstr() );
  6152. // we need to initialize the "last seen" value with a key that
  6153. // is guaranteed to be different from the 1st match that we will scan
  6154. // hence (val-1) for scalars, and NULL for strings
  6155. SphAttr_t iLastValue = ( t==SPH_ATTR_STRING || t==SPH_ATTR_STRINGPTR )
  6156. ? 0
  6157. : ( dSubMatches.First().GetAttr ( pCol->m_tLocator ) - 1 );
  6158. int iOutPos = 0;
  6159. for ( auto& dMatch : dSubMatches )
  6160. {
  6161. // get value, skip repeats
  6162. SphAttr_t iCur = dMatch.GetAttr ( pCol->m_tLocator );
  6163. if ( iCur==iLastValue )
  6164. continue;
  6165. if ( iCur && iLastValue && t==SPH_ATTR_STRINGPTR )
  6166. {
  6167. auto a = sphUnpackPtrAttr ((const BYTE *) iCur );
  6168. auto b = sphUnpackPtrAttr ((const BYTE *) iLastValue );
  6169. if ( a.second==b.second && memcmp ( a.first, b.first, a.second )==0 )
  6170. continue;
  6171. }
  6172. iLastValue = iCur;
  6173. // skip eligible rows according to tablefunc offset
  6174. if ( m_iOffset>0 )
  6175. {
  6176. --m_iOffset;
  6177. continue;
  6178. }
  6179. // emit!
  6180. Swap ( dMatches[iOutPos], dMatch );
  6181. // break if we reached the tablefunc limit
  6182. if ( ++iOutPos==m_iLimit )
  6183. break;
  6184. }
  6185. // adjust the result set limits
  6186. pResult->ClampMatches ( iOutPos );
  6187. pResult->m_iOffset = 0;
  6188. pResult->m_iCount = dMatches.GetLength();
  6189. return true;
  6190. }
  6191. };
  6192. std::unique_ptr<ISphTableFunc> CreateRemoveRepeats()
  6193. {
  6194. return std::make_unique<CSphTableFuncRemoveRepeats>();
  6195. }
  6196. #undef LOC_ERROR1
  6197. #undef LOC_ERROR
  6198. //////////////////////////////////////////////////////////////////////////
  6199. // SQL PARSER
  6200. //////////////////////////////////////////////////////////////////////////
  6201. // FIXME? verify or generate these automatically somehow?
  6202. static const char * g_dSqlStmts[] =
  6203. {
  6204. "parse_error", "dummy", "select", "insert", "replace", "delete", "show_warnings",
  6205. "show_status", "show_meta", "set", "begin", "commit", "rollback", "call",
  6206. "desc", "show_tables", "create_table", "create_table_like", "drop_table", "show_create_table", "update", "create_func",
  6207. "drop_func", "attach_index", "flush_rtindex", "flush_ramchunk", "show_variables", "truncate_rtindex",
  6208. "select_sysvar", "show_collation", "show_character_set", "optimize_index", "show_agent_status",
  6209. "show_index_status", "show_profile", "alter_add", "alter_drop", "show_plan",
  6210. "select_dual", "show_databases", "create_plugin", "drop_plugin", "show_plugins", "show_threads",
  6211. "facet", "alter_reconfigure", "show_index_settings", "flush_index", "reload_plugins", "reload_index",
  6212. "flush_hostnames", "flush_logs", "reload_indexes", "sysfilters", "debug", "alter_killlist_target",
  6213. "alter_index_settings", "join_cluster", "cluster_create", "cluster_delete", "cluster_index_add",
  6214. "cluster_index_delete", "cluster_update", "explain", "import_table", "freeze_indexes", "unfreeze_indexes",
  6215. "show_settings", "alter_rebuild_si", "kill",
  6216. };
  6217. STATIC_ASSERT ( sizeof(g_dSqlStmts)/sizeof(g_dSqlStmts[0])==STMT_TOTAL, STMT_DESC_SHOULD_BE_SAME_AS_STMT_TOTAL );
  6218. //////////////////////////////////////////////////////////////////////////
  6219. class CSphMatchVariant
  6220. {
  6221. public:
  6222. inline static SphAttr_t ToInt ( const SqlInsert_t & tVal )
  6223. {
  6224. switch ( tVal.m_iType )
  6225. {
  6226. case SqlInsert_t::QUOTED_STRING : return strtoul ( tVal.m_sVal.cstr(), NULL, 10 ); // FIXME? report conversion error?
  6227. case SqlInsert_t::CONST_INT: return int(tVal.m_iVal);
  6228. case SqlInsert_t::CONST_FLOAT: return int(tVal.m_fVal); // FIXME? report conversion error
  6229. }
  6230. return 0;
  6231. }
  6232. inline static SphAttr_t ToBigInt ( const SqlInsert_t & tVal )
  6233. {
  6234. switch ( tVal.m_iType )
  6235. {
  6236. case SqlInsert_t::QUOTED_STRING : return strtoll ( tVal.m_sVal.cstr(), NULL, 10 ); // FIXME? report conversion error?
  6237. case SqlInsert_t::CONST_INT: return tVal.m_iVal;
  6238. case SqlInsert_t::CONST_FLOAT: return int64_t(tVal.m_fVal); // FIXME? report conversion error?
  6239. }
  6240. return 0;
  6241. }
  6242. static bool ConvertPlainAttr ( const SqlInsert_t & tVal, ESphAttr eTargetType, SphAttr_t & tAttr )
  6243. {
  6244. tAttr = 0;
  6245. switch ( eTargetType )
  6246. {
  6247. case SPH_ATTR_INTEGER:
  6248. case SPH_ATTR_TIMESTAMP:
  6249. case SPH_ATTR_BOOL:
  6250. case SPH_ATTR_TOKENCOUNT:
  6251. tAttr = ToInt(tVal);
  6252. break;
  6253. case SPH_ATTR_BIGINT:
  6254. tAttr = ToBigInt(tVal);
  6255. break;
  6256. case SPH_ATTR_FLOAT:
  6257. if ( tVal.m_iType==SqlInsert_t::QUOTED_STRING )
  6258. tAttr = sphF2DW ( (float)strtod ( tVal.m_sVal.cstr(), NULL ) ); // FIXME? report conversion error?
  6259. else if ( tVal.m_iType==SqlInsert_t::CONST_INT )
  6260. tAttr = sphF2DW ( float(tVal.m_iVal) ); // FIXME? report conversion error?
  6261. else if ( tVal.m_iType==SqlInsert_t::CONST_FLOAT )
  6262. tAttr = sphF2DW ( tVal.m_fVal );
  6263. break;
  6264. case SPH_ATTR_STRINGPTR:
  6265. break;
  6266. default:
  6267. return false;
  6268. };
  6269. return true;
  6270. }
  6271. inline static void SetAttr ( CSphMatch & tMatch, const CSphAttrLocator & tLoc, const SqlInsert_t & tVal, ESphAttr eTargetType )
  6272. {
  6273. SphAttr_t tAttr;
  6274. if ( ConvertPlainAttr ( tVal, eTargetType, tAttr ) )
  6275. tMatch.SetAttr ( tLoc, tAttr );
  6276. }
  6277. inline static void SetDefaultAttr ( CSphMatch & tMatch, const CSphAttrLocator & tLoc, ESphAttr eTargetType )
  6278. {
  6279. SqlInsert_t tVal;
  6280. tVal.m_iType = SqlInsert_t::CONST_INT;
  6281. tVal.m_iVal = 0;
  6282. SetAttr ( tMatch, tLoc, tVal, eTargetType );
  6283. }
  6284. };
  6285. /////////////////////////////////////////////////////////////////////////////
  6286. // EXCERPTS HANDLER
  6287. /////////////////////////////////////////////////////////////////////////////
  6288. enum eExcerpt_Flags
  6289. {
  6290. EXCERPT_FLAG_REMOVESPACES = 1, // deprecated
  6291. EXCERPT_FLAG_EXACTPHRASE = 2, // deprecated
  6292. EXCERPT_FLAG_SINGLEPASSAGE = 4,
  6293. EXCERPT_FLAG_USEBOUNDARIES = 8,
  6294. EXCERPT_FLAG_WEIGHTORDER = 16,
  6295. EXCERPT_FLAG_QUERY = 32, // deprecated
  6296. EXCERPT_FLAG_FORCE_ALL_WORDS = 64,
  6297. EXCERPT_FLAG_LOAD_FILES = 128,
  6298. EXCERPT_FLAG_ALLOW_EMPTY = 256,
  6299. EXCERPT_FLAG_EMIT_ZONES = 512,
  6300. EXCERPT_FLAG_FILES_SCATTERED = 1024,
  6301. EXCERPT_FLAG_FORCEPASSAGES = 2048
  6302. };
  6303. int PackAPISnippetFlags ( const SnippetQuerySettings_t &q, bool bOnlyScattered = false )
  6304. {
  6305. int iRawFlags = q.m_iLimitPassages ? EXCERPT_FLAG_SINGLEPASSAGE : 0;
  6306. iRawFlags |= q.m_bUseBoundaries ? EXCERPT_FLAG_USEBOUNDARIES : 0;
  6307. iRawFlags |= q.m_bWeightOrder ? EXCERPT_FLAG_WEIGHTORDER : 0;
  6308. iRawFlags |= q.m_bForceAllWords ? EXCERPT_FLAG_FORCE_ALL_WORDS : 0;
  6309. if ( !bOnlyScattered || !( q.m_uFilesMode & 2 ) )
  6310. iRawFlags |= ( q.m_uFilesMode & 1 ) ? EXCERPT_FLAG_LOAD_FILES : 0;
  6311. iRawFlags |= q.m_bAllowEmpty ? EXCERPT_FLAG_ALLOW_EMPTY : 0;
  6312. iRawFlags |= q.m_bEmitZones ? EXCERPT_FLAG_EMIT_ZONES : 0;
  6313. iRawFlags |= ( q.m_uFilesMode & 2 ) ? EXCERPT_FLAG_FILES_SCATTERED : 0;
  6314. iRawFlags |= q.m_bForcePassages ? EXCERPT_FLAG_FORCEPASSAGES : 0;
  6315. return iRawFlags;
  6316. }
  6317. struct ExcerptQuery_t
  6318. {
  6319. int64_t m_iSize = 0; ///< file size, to sort to work-queue order
  6320. CSphString m_sSource; ///< source data
  6321. CSphString m_sError;
  6322. CSphVector<BYTE> m_dResult; ///< query result
  6323. };
  6324. class SnippetRemote_c : public RequestBuilder_i, public ReplyParser_i
  6325. {
  6326. public:
  6327. SnippetRemote_c ( VecTraits_T<ExcerptQuery_t> & dQueries, const SnippetQuerySettings_t& q )
  6328. : m_dQueries ( dQueries )
  6329. , m_tSettings ( q )
  6330. {}
  6331. void BuildRequest ( const AgentConn_t & tAgent, ISphOutputBuffer & tOut ) const final;
  6332. bool ParseReply ( MemInputBuffer_c & tReq, AgentConn_t & ) const final;
  6333. private:
  6334. VecTraits_T<ExcerptQuery_t> & m_dQueries;
  6335. const SnippetQuerySettings_t & m_tSettings;
  6336. mutable std::atomic<int> m_iWorker {0};
  6337. bool ParseReplyScattered ( MemInputBuffer_c & tReq, const VecTraits_T<int>& dDocs ) const;
  6338. bool ParseReplyNonScattered ( MemInputBuffer_c & tReq, const VecTraits_T<int> & dDocs ) const;
  6339. public:
  6340. CSphVector<const VecTraits_T<int> *> m_dTasks;
  6341. };
  6342. void SnippetRemote_c::BuildRequest ( const AgentConn_t & tAgent, ISphOutputBuffer & tOut ) const
  6343. {
  6344. // it sends either all queries to each agent or sequence of queries to current agent
  6345. auto iWorker = tAgent.m_iStoreTag;
  6346. if ( iWorker<0 )
  6347. {
  6348. iWorker = m_iWorker.fetch_add ( 1, std::memory_order_relaxed );
  6349. tAgent.m_iStoreTag = iWorker;
  6350. }
  6351. auto tHdr = APIHeader ( tOut, SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT );
  6352. tOut.SendInt ( 0 );
  6353. tOut.SendInt ( PackAPISnippetFlags ( m_tSettings, true ) );
  6354. tOut.SendString ( tAgent.m_tDesc.m_sIndexes.cstr () );
  6355. tOut.SendString ( m_tSettings.m_sQuery.cstr() );
  6356. tOut.SendString ( m_tSettings.m_sBeforeMatch.cstr() );
  6357. tOut.SendString ( m_tSettings.m_sAfterMatch.cstr() );
  6358. tOut.SendString ( m_tSettings.m_sChunkSeparator.cstr() );
  6359. tOut.SendInt ( m_tSettings.m_iLimit );
  6360. tOut.SendInt ( m_tSettings.m_iAround );
  6361. tOut.SendInt ( m_tSettings.m_iLimitPassages );
  6362. tOut.SendInt ( m_tSettings.m_iLimitWords );
  6363. tOut.SendInt ( m_tSettings.m_iPassageId );
  6364. tOut.SendString ( m_tSettings.m_sStripMode.cstr() );
  6365. tOut.SendString ( PassageBoundarySz ( m_tSettings.m_ePassageSPZ ) );
  6366. const auto& dDocs = *m_dTasks[iWorker];
  6367. tOut.SendInt ( dDocs.GetLength() );
  6368. for ( int iDoc : dDocs )
  6369. tOut.SendString ( m_dQueries[iDoc].m_sSource.cstr() );
  6370. }
  6371. bool SnippetRemote_c::ParseReply ( MemInputBuffer_c & tReq, AgentConn_t & tAgent ) const
  6372. {
  6373. auto& tDocs = *m_dTasks[tAgent.m_iStoreTag];
  6374. if ( m_tSettings.m_uFilesMode & 2 ) // scattered files
  6375. return ParseReplyScattered ( tReq, tDocs );
  6376. return ParseReplyNonScattered ( tReq, tDocs );
  6377. }
  6378. bool SnippetRemote_c::ParseReplyScattered ( MemInputBuffer_c & tReq, const VecTraits_T<int> & dDocs ) const
  6379. {
  6380. bool bOk = true;
  6381. for ( int iDoc : dDocs )
  6382. {
  6383. ExcerptQuery_t & tQuery = m_dQueries[iDoc];
  6384. CSphVector<BYTE> & dRes = tQuery.m_dResult;
  6385. if ( !tReq.GetString(dRes) || dRes.IsEmpty() )
  6386. {
  6387. bOk = false;
  6388. dRes.Resize(0);
  6389. } else
  6390. tQuery.m_sError = "";
  6391. }
  6392. return bOk;
  6393. }
  6394. bool SnippetRemote_c::ParseReplyNonScattered ( MemInputBuffer_c & tReq, const VecTraits_T<int> & dDocs ) const
  6395. {
  6396. for ( int iDoc : dDocs )
  6397. {
  6398. ExcerptQuery_t & tQuery = m_dQueries[iDoc];
  6399. tReq.GetString ( tQuery.m_dResult );
  6400. tQuery.m_iSize = -1; // means 'processed'
  6401. }
  6402. return true;
  6403. }
  6404. static int64_t GetSnippetDataSize ( const CSphVector<ExcerptQuery_t> &dSnippets )
  6405. {
  6406. int64_t iSize = 0;
  6407. for ( const auto & dSnippet: dSnippets )
  6408. {
  6409. if ( dSnippet.m_iSize>0 )
  6410. iSize += dSnippet.m_iSize;
  6411. else if ( !dSnippet.m_iSize )
  6412. iSize += dSnippet.m_sSource.Length ();
  6413. }
  6414. iSize /= 100;
  6415. return iSize;
  6416. }
  6417. static VecRefPtrsAgentConn_t GetDistrAgents ( const cDistributedIndexRefPtr_t& pDist )
  6418. {
  6419. assert ( pDist );
  6420. VecRefPtrsAgentConn_t tRemotes;
  6421. for ( const auto& pAgent : pDist->m_dAgents )
  6422. {
  6423. auto * pConn = new AgentConn_t;
  6424. pConn->SetMultiAgent ( pAgent );
  6425. pConn->m_iMyConnectTimeoutMs = pDist->m_iAgentConnectTimeoutMs;
  6426. pConn->m_iMyQueryTimeoutMs = pDist->m_iAgentQueryTimeoutMs;
  6427. tRemotes.Add ( pConn );
  6428. }
  6429. return tRemotes;
  6430. }
  6431. // collect source sizes. For absent files set -1.
  6432. static bool CollectSourceSizes ( CSphVector<ExcerptQuery_t> & dQueries, bool bFileMode, bool bNeedAll, CSphString & sError )
  6433. {
  6434. // collect source sizes
  6435. if ( !bFileMode )
  6436. {
  6437. dQueries.Apply ( [] ( ExcerptQuery_t & dQuery ) { dQuery.m_iSize = dQuery.m_sSource.Length (); } );
  6438. return true;
  6439. }
  6440. for ( auto & dQuery : dQueries )
  6441. {
  6442. CSphString sFilename, sStatError;
  6443. sFilename.SetSprintf ( "%s%s", g_sSnippetsFilePrefix.cstr (), dQuery.m_sSource.scstr () );
  6444. if ( !TestEscaping ( g_sSnippetsFilePrefix, sFilename ) )
  6445. {
  6446. sError.SetSprintf ( "File '%s' escapes '%s' scope", sFilename.scstr (), g_sSnippetsFilePrefix.scstr () );
  6447. return false;
  6448. }
  6449. auto iFileSize = sphGetFileSize ( sFilename, &sStatError );
  6450. if ( iFileSize<0 )
  6451. {
  6452. if ( bNeedAll )
  6453. {
  6454. sError = sStatError;
  6455. return false;
  6456. }
  6457. dQuery.m_iSize = -1;
  6458. } else
  6459. dQuery.m_iSize = iFileSize;
  6460. }
  6461. return true;
  6462. }
  6463. // helper, called both for single and for multi snippets
  6464. static inline bool MakeSingleLocalSnippetWithFields ( ExcerptQuery_t & tQuery, const SnippetQuerySettings_t & q,
  6465. SnippetBuilder_c * pBuilder, const VecTraits_T<int>& dFields )
  6466. {
  6467. assert ( pBuilder );
  6468. std::unique_ptr<TextSource_i> pSource = CreateSnippetSource ( q.m_uFilesMode, (const BYTE*)tQuery.m_sSource.cstr(), tQuery.m_sSource.Length() );
  6469. SnippetResult_t tRes;
  6470. if ( !pBuilder->Build ( pSource, tRes ) )
  6471. {
  6472. tQuery.m_sError = std::move ( tRes.m_sError );
  6473. return false;
  6474. }
  6475. tQuery.m_dResult = pBuilder->PackResult ( tRes, dFields );
  6476. return true;
  6477. };
  6478. // boring single snippet
  6479. static inline bool MakeSingleLocalSnippet ( ExcerptQuery_t & tQuery, const SnippetQuerySettings_t & q,
  6480. SnippetBuilder_c * pBuilder, CSphString& sError )
  6481. {
  6482. CSphVector<int> dStubFields;
  6483. dStubFields.Add ( 0 );
  6484. if ( MakeSingleLocalSnippetWithFields ( tQuery, q, pBuilder, dStubFields ) )
  6485. return true;
  6486. sError = tQuery.m_sError;
  6487. return false;
  6488. }
  6489. struct SnippedBuilderCtxRef_t
  6490. {
  6491. SnippetBuilder_c * m_pBuilder;
  6492. SnippedBuilderCtxRef_t ( SnippetBuilder_c * pBuilder ) : m_pBuilder ( pBuilder ) {}
  6493. inline static bool IsClonable () { return true; }
  6494. };
  6495. struct SnippedBuilderCtxClone_t : public SnippedBuilderCtxRef_t, ISphNoncopyable
  6496. {
  6497. explicit SnippedBuilderCtxClone_t ( const SnippedBuilderCtxRef_t& dParent )
  6498. : SnippedBuilderCtxRef_t { dParent.m_pBuilder->MakeClone() }
  6499. {}
  6500. // dtr is only for clones!
  6501. ~SnippedBuilderCtxClone_t() { SafeDelete (m_pBuilder); }
  6502. };
  6503. // Starts or performs parallel snippets creation with throttling
  6504. static void MakeSnippetsCoro ( const VecTraits_T<int>& dTasks, CSphVector<ExcerptQuery_t> & dQueries,
  6505. const SnippetQuerySettings_t& q, SnippetBuilder_c * pBuilder)
  6506. {
  6507. assert ( pBuilder );
  6508. auto iJobs = dTasks.GetLength();
  6509. if ( !iJobs )
  6510. return;
  6511. sphLogDebug ( "MakeSnippetsCoro invoked for %d tasks", iJobs );
  6512. CSphVector<int> dStubFields;
  6513. dStubFields.Add ( 0 );
  6514. // the context
  6515. ClonableCtx_T<SnippedBuilderCtxRef_t, SnippedBuilderCtxClone_t, Threads::ECONTEXT::UNORDERED> dCtx { pBuilder };
  6516. auto pDispatcher = Dispatcher::Make ( iJobs, 0, GetEffectiveBaseDispatcherTemplate(), dCtx.IsSingle() );
  6517. dCtx.LimitConcurrency ( pDispatcher->GetConcurrency() );
  6518. Coro::ExecuteN ( dCtx.Concurrency ( iJobs ), [&]
  6519. {
  6520. sphLogDebug ( "MakeSnippetsCoro Coro started" );
  6521. auto pSource = pDispatcher->MakeSource();
  6522. int iJob = -1; // make it consumed
  6523. if ( !pSource->FetchTask ( iJob ) )
  6524. {
  6525. sphLogDebug ( "Early finish parallel MakeSnippetsCoro because of empty queue" );
  6526. return; // already nothing to do, early finish.
  6527. }
  6528. auto tJobContext = dCtx.CloneNewContext();
  6529. auto& tCtx = tJobContext.first;
  6530. sphLogDebug ( "MakeSnippetsCoro cloned context %d", tJobContext.second );
  6531. Threads::Coro::Throttler_c tThrottler ( session::GetThrottlingPeriodMS () );
  6532. while (true)
  6533. {
  6534. myinfo::SetTaskInfo ( "s %d:", iJob );
  6535. sphLogDebugv ( "MakeSnippetsCoro %d %d[%d]", tJobContext.second, iJob, dTasks[iJob] );
  6536. MakeSingleLocalSnippetWithFields ( dQueries[dTasks[iJob]], q, tCtx.m_pBuilder, dStubFields );
  6537. sphLogDebug ( "MakeSnippetsCoro Coro loop tick %d finished", iJob );
  6538. iJob = -1; // mark it consumed
  6539. if ( !pSource->FetchTask ( iJob ) )
  6540. return; // already nothing to do, early finish.
  6541. // yield and reschedule every quant of time. It gives work to other tasks
  6542. tThrottler.ThrottleAndKeepCrashQuery ();
  6543. }
  6544. });
  6545. }
  6546. // divide set of tasks from dTasks into chunks, having most balanced aggregate iSize in each.
  6547. static CSphVector<CSphVector<int>> DivideTasks ( const VecTraits_T<int> & dTasks,
  6548. const VecTraits_T<ExcerptQuery_t> & dQueries, int iWorkers )
  6549. {
  6550. CSphVector<CSphVector<int>> dResults;
  6551. auto iTasks = dTasks.GetLength();
  6552. auto iLimit = Min ( iWorkers, iTasks );
  6553. if ( iWorkers>=iTasks )
  6554. {
  6555. dResults.Resize ( iLimit );
  6556. for ( int i=0; i<iLimit; ++i )
  6557. dResults[i].Add ( dTasks[i] );
  6558. } else
  6559. {
  6560. // helpers
  6561. using ItemsQueue_c = TimeoutQueue_c;
  6562. using EnqueuedItem_t = EnqueuedTimeout_t;
  6563. ItemsQueue_c qTasks;
  6564. struct PriorityVec_t : EnqueuedItem_t { int m_iRefIdx; };
  6565. CSphVector<PriorityVec_t> dPriorityResults ( iWorkers );
  6566. dResults.Resize ( iWorkers );
  6567. // initially fill the queue
  6568. ARRAY_FOREACH ( i, dPriorityResults )
  6569. {
  6570. dResults[i].Add ( dTasks[i] );
  6571. dPriorityResults[i].m_iTimeoutTimeUS = dQueries[dTasks[i]].m_iSize;
  6572. dPriorityResults[i].m_iRefIdx = i;
  6573. qTasks.Change ( &dPriorityResults[i] );
  6574. }
  6575. // update the queue
  6576. for ( int i=iWorkers; i<iTasks; ++i )
  6577. {
  6578. auto * pBest = (PriorityVec_t *) qTasks.Root ();
  6579. dResults[pBest->m_iRefIdx].Add ( dTasks[i] );
  6580. pBest->m_iTimeoutTimeUS += dQueries[dTasks[i]].m_iSize;
  6581. qTasks.Change ( pBest );
  6582. }
  6583. }
  6584. return dResults;
  6585. }
  6586. // remote scattered snippets (with local pass)
  6587. // * dLocal subset is run on local host
  6588. // * dRemote subset is send to each remote agent
  6589. static void MakeRemoteScatteredSnippets ( CSphVector<ExcerptQuery_t> & dQueries,
  6590. cDistributedIndexRefPtr_t pDist,
  6591. SnippetBuilder_c * pBuilder,
  6592. const SnippetQuerySettings_t & q,
  6593. const VecTraits_T<int>& dLocal,
  6594. const VecTraits_T<int>& dAbsent )
  6595. {
  6596. assert ( pDist );
  6597. assert ( pBuilder );
  6598. // and finally most interesting remote case with possibly scattered.
  6599. auto dAgents = GetDistrAgents ( pDist );
  6600. int iRemoteAgents = dAgents.GetLength();
  6601. SnippetRemote_c tRemotes ( dQueries, q );
  6602. tRemotes.m_dTasks.Resize ( iRemoteAgents );
  6603. // on scattered case - just push the chain of locally absent files to all remotes
  6604. for ( auto & pTask : tRemotes.m_dTasks )
  6605. pTask = &dAbsent;
  6606. // query remote building
  6607. CSphRefcountedPtr<RemoteAgentsObserver_i> tReporter ( GetObserver () );
  6608. ScheduleDistrJobs ( dAgents, &tRemotes, &tRemotes, tReporter );
  6609. // start local building and wait it to finish
  6610. MakeSnippetsCoro ( dLocal, dQueries, q, pBuilder );
  6611. // wait remotes to finish also
  6612. tReporter->Finish ();
  6613. auto iSuccesses = ( int ) tReporter->GetSucceeded ();
  6614. auto iAgentsDone = ( int ) tReporter->GetFinished ();
  6615. if ( iSuccesses!=iRemoteAgents )
  6616. sphWarning ( "Remote snippets: some of the agents didn't answered: %d queried, %d finished, %d succeeded",
  6617. iRemoteAgents, iAgentsDone, iSuccesses );
  6618. }
  6619. // remote non scattered snippets (with local pass)
  6620. // non-scattered assumes, each host has full set of sources, so we don't need to check absent here.
  6621. // * divide set of sources among remotes and local host, balancing size.
  6622. // * assume dPresent has indexes of monotonically decreasing sizes, that's need for balancing.
  6623. static void MakeRemoteNonScatteredSnippets ( CSphVector<ExcerptQuery_t> & dQueries,
  6624. cDistributedIndexRefPtr_t pDist,
  6625. SnippetBuilder_c * pBuilder,
  6626. const SnippetQuerySettings_t & q,
  6627. const VecTraits_T<int>& dPresent )
  6628. {
  6629. assert ( pDist );
  6630. assert ( pBuilder );
  6631. auto dAgents = GetDistrAgents ( pDist );
  6632. int iRemoteAgents = dAgents.GetLength();
  6633. SnippetRemote_c tRemotes ( dQueries, q );
  6634. tRemotes.m_dTasks.Resize ( iRemoteAgents );
  6635. // on non-scattered - distribute set of sources to workers, having 1 local worker in mind.
  6636. auto dJobSet = DivideTasks ( dPresent, dQueries, iRemoteAgents+1 ) ; // +1 since we also will work locally.
  6637. auto& dLocalSet = dJobSet[iRemoteAgents];
  6638. for ( int i = 0; i<iRemoteAgents; ++i )
  6639. tRemotes.m_dTasks[i] = &dJobSet[i];
  6640. // query remote building
  6641. CSphRefcountedPtr<RemoteAgentsObserver_i> tReporter ( GetObserver () );
  6642. ScheduleDistrJobs ( dAgents, &tRemotes, &tRemotes, tReporter );
  6643. // start local building and wait it to finish
  6644. MakeSnippetsCoro ( dLocalSet, dQueries, q, pBuilder );
  6645. // wait remotes to finish also
  6646. tReporter->Finish ();
  6647. auto iSuccesses = ( int ) tReporter->GetSucceeded ();
  6648. auto iAgentsDone = ( int ) tReporter->GetFinished ();
  6649. if ( iSuccesses==iRemoteAgents )
  6650. return;
  6651. sphWarning ( "Remote snippets: some of the agents didn't answered: %d queried, %d finished, %d succeeded",
  6652. iRemoteAgents, iAgentsDone, iSuccesses );
  6653. // let's collect failures and make one more pass over them
  6654. CSphVector<int> dFailed;
  6655. // collect failed nodes
  6656. dPresent.Apply ( [&] ( int iDoc ) {
  6657. if ( dQueries[iDoc].m_iSize<0 )
  6658. dFailed.Add(iDoc);
  6659. });
  6660. if ( dFailed.IsEmpty() )
  6661. return;
  6662. // failsafe - one more turn for failed queries on local agent
  6663. sphWarning ( "Snippets: failsafe for %d failed items", (int) dFailed.GetLength() );
  6664. MakeSnippetsCoro ( dFailed, dQueries, q, pBuilder );
  6665. }
  6666. bool MakeSnippets ( CSphString sIndex, CSphVector<ExcerptQuery_t> & dQueries,
  6667. const SnippetQuerySettings_t& q, CSphString & sError )
  6668. {
  6669. assert ( !dQueries.IsEmpty() );
  6670. // When both load_files & load_files_scattered set, absent files will be reported as errors.
  6671. // load_files_scattered without load_files just omits the absent files (returns empty strings).
  6672. auto bScattered = !!( q.m_uFilesMode & 2 );
  6673. auto bNeedAllFiles = !!( q.m_uFilesMode & 1 );
  6674. auto pDist = GetDistr ( sIndex );
  6675. bool bRemote = pDist && !pDist->m_dAgents.IsEmpty ();
  6676. if ( bRemote )
  6677. {
  6678. if ( pDist->m_dLocal.GetLength()!=1 )
  6679. {
  6680. sError.SetSprintf ( "%s", "distributed index for snippets must have exactly one local agent" );
  6681. return false;
  6682. }
  6683. if ( !q.m_uFilesMode )
  6684. {
  6685. sError.SetSprintf ( "%s", "distributed index for snippets available only when using external files" );
  6686. return false;
  6687. }
  6688. // for remotes index is 1-st local agent of the distr, so move on!
  6689. sIndex = pDist->m_dLocal[0];
  6690. }
  6691. auto pServed = GetServed ( sIndex );
  6692. if ( !pServed )
  6693. {
  6694. sError.SetSprintf ( "unknown local index '%s' in search request", sIndex.cstr() );
  6695. return false;
  6696. }
  6697. RIdx_c pLocalIndex { pServed };
  6698. assert ( pLocalIndex );
  6699. ///////////////////
  6700. /// do highlighting
  6701. ///////////////////
  6702. auto pBuilder = std::make_unique<SnippetBuilder_c>();
  6703. pBuilder->Setup ( pLocalIndex, q );
  6704. if ( !pBuilder->SetQuery ( q.m_sQuery.cstr(), true, sError ) )
  6705. return false;
  6706. // boring single snippet
  6707. if ( dQueries.GetLength ()==1 )
  6708. return MakeSingleLocalSnippet ( dQueries[0], q, pBuilder.get(), sError );
  6709. if ( !CollectSourceSizes ( dQueries, q.m_uFilesMode, !bScattered, sError ) )
  6710. return false;
  6711. // set correct data size for snippets
  6712. myinfo::SetTaskInfo ( R"(snippet datasize=%.1Dk query="%s")", GetSnippetDataSize ( dQueries ), q.m_sQuery.scstr () );
  6713. // collect list of existing and empty sources
  6714. CSphVector<int> dPresent;
  6715. CSphVector<int> dAbsent;
  6716. ARRAY_FOREACH ( i, dQueries )
  6717. {
  6718. if ( dQueries[i].m_iSize<0 )
  6719. dAbsent.Add(i);
  6720. else
  6721. dPresent.Add(i);
  6722. }
  6723. // check if all files are available locally - then we need no remote pass.
  6724. if ( bScattered && dAbsent.IsEmpty() )
  6725. bRemote = false;
  6726. if ( bNeedAllFiles && !dAbsent.IsEmpty() )
  6727. for ( int i : dAbsent )
  6728. dQueries[i].m_sError.SetSprintf ( "absenthead: failed to stat %s", dQueries[i].m_sSource.cstr () );
  6729. // tough jobs first (sort inverse)
  6730. if ( !bScattered )
  6731. dPresent.Sort ( Lesser ( [&dQueries] ( int a, int b ) { return dQueries[a].m_iSize>dQueries[b].m_iSize; } ) );
  6732. if ( !bRemote )
  6733. {
  6734. // multithreaded, but no remote agents.
  6735. MakeSnippetsCoro ( dPresent, dQueries, q, pBuilder.get() );
  6736. } else
  6737. {
  6738. assert ( pDist );
  6739. // multithreaded with remotes (scattered and full)
  6740. if ( bScattered )
  6741. MakeRemoteScatteredSnippets ( dQueries, pDist, pBuilder.get(), q, dPresent, dAbsent );
  6742. else
  6743. MakeRemoteNonScatteredSnippets ( dQueries, pDist, pBuilder.get (), q, dPresent );
  6744. }
  6745. StringBuilder_c sErrors ( "; " );
  6746. dQueries.Apply ( [&] ( const ExcerptQuery_t & tQuery ) { sErrors << tQuery.m_sError; } );
  6747. sErrors.MoveTo ( sError );
  6748. return sError.IsEmpty();
  6749. }
  6750. // throw out tailing \0 if any
  6751. inline static void FixupResultTail (CSphVector<BYTE> & dData)
  6752. {
  6753. if ( !dData.IsEmpty() && !dData.Last () )
  6754. dData.Pop ();
  6755. }
  6756. void HandleCommandExcerpt ( ISphOutputBuffer & tOut, int iVer, InputBuffer_c & tReq )
  6757. {
  6758. if ( !CheckCommandVersion ( iVer, VER_COMMAND_EXCERPT, tOut ) )
  6759. return;
  6760. /////////////////////////////
  6761. // parse and process request
  6762. /////////////////////////////
  6763. const int EXCERPT_MAX_ENTRIES = 1024;
  6764. // v.1.1
  6765. SnippetQuerySettings_t q;
  6766. tReq.GetInt (); // mode field is for now reserved and ignored
  6767. int iFlags = tReq.GetInt ();
  6768. CSphString sIndex = tReq.GetString ();
  6769. q.m_sQuery = tReq.GetString ();
  6770. q.m_sBeforeMatch = tReq.GetString ();
  6771. q.m_sAfterMatch = tReq.GetString ();
  6772. q.m_sChunkSeparator = tReq.GetString ();
  6773. q.m_iLimit = tReq.GetInt ();
  6774. q.m_iAround = tReq.GetInt ();
  6775. if ( iVer>=0x102 )
  6776. {
  6777. q.m_iLimitPassages = tReq.GetInt();
  6778. q.m_iLimitWords = tReq.GetInt();
  6779. q.m_iPassageId = tReq.GetInt();
  6780. q.m_sStripMode = tReq.GetString();
  6781. if ( q.m_sStripMode!="none" && q.m_sStripMode!="index" && q.m_sStripMode!="strip" && q.m_sStripMode!="retain" )
  6782. {
  6783. SendErrorReply ( tOut, "unknown html_strip_mode=%s", q.m_sStripMode.cstr() );
  6784. return;
  6785. }
  6786. }
  6787. q.Setup();
  6788. CSphString sPassageBoundaryMode;
  6789. if ( iVer>=0x103 )
  6790. q.m_ePassageSPZ = GetPassageBoundary ( tReq.GetString() );
  6791. q.m_bUseBoundaries = ( iFlags & EXCERPT_FLAG_USEBOUNDARIES )!=0;
  6792. q.m_bWeightOrder = ( iFlags & EXCERPT_FLAG_WEIGHTORDER )!=0;
  6793. q.m_bForceAllWords = ( iFlags & EXCERPT_FLAG_FORCE_ALL_WORDS )!=0;
  6794. if ( iFlags & EXCERPT_FLAG_SINGLEPASSAGE )
  6795. q.m_iLimitPassages = 1;
  6796. q.m_uFilesMode = ( iFlags & EXCERPT_FLAG_LOAD_FILES )?1:0;
  6797. bool bScattered = ( iFlags & EXCERPT_FLAG_FILES_SCATTERED )!=0;
  6798. q.m_uFilesMode |= bScattered?2:0;
  6799. q.m_bAllowEmpty = ( iFlags & EXCERPT_FLAG_ALLOW_EMPTY )!=0;
  6800. q.m_bEmitZones = ( iFlags & EXCERPT_FLAG_EMIT_ZONES )!=0;
  6801. q.m_bForcePassages = ( iFlags & EXCERPT_FLAG_FORCEPASSAGES )!=0;
  6802. bool bExactPhrase = ( iFlags & EXCERPT_FLAG_EXACTPHRASE )!=0;
  6803. if ( bExactPhrase )
  6804. {
  6805. SendErrorReply ( tOut, "exact_phrase is deprecated" );
  6806. return;
  6807. }
  6808. int iCount = tReq.GetInt ();
  6809. if ( iCount<=0 || iCount>EXCERPT_MAX_ENTRIES )
  6810. {
  6811. SendErrorReply ( tOut, "invalid entries count %d", iCount );
  6812. return;
  6813. }
  6814. CSphString sError;
  6815. if ( !sphCheckOptionsSPZ ( q, q.m_ePassageSPZ, sError ) )
  6816. {
  6817. SendErrorReply ( tOut, "%s", sError.cstr() );
  6818. return;
  6819. }
  6820. CSphVector<ExcerptQuery_t> dQueries { iCount };
  6821. for ( auto & dQuery : dQueries )
  6822. {
  6823. dQuery.m_sSource = tReq.GetString (); // fetch data
  6824. if ( tReq.GetError() )
  6825. {
  6826. SendErrorReply ( tOut, "invalid or truncated request" );
  6827. return;
  6828. }
  6829. }
  6830. myinfo::SetTaskInfo ( R"(api-snippet datasize=%.1Dk query="%s")", GetSnippetDataSize ( dQueries ), q.m_sQuery.scstr ());
  6831. if ( !MakeSnippets ( sIndex, dQueries, q, sError ) )
  6832. {
  6833. SendErrorReply ( tOut, "%s", sError.cstr() );
  6834. return;
  6835. }
  6836. ////////////////
  6837. // serve result
  6838. ////////////////
  6839. for ( const auto & i : dQueries )
  6840. {
  6841. // handle errors
  6842. if ( !bScattered && !i.m_sError.IsEmpty() )
  6843. {
  6844. SendErrorReply ( tOut, "highlighting failed: %s", i.m_sError.cstr() );
  6845. return;
  6846. }
  6847. }
  6848. auto tReply = APIAnswer ( tOut, VER_COMMAND_EXCERPT );
  6849. for ( const auto & i : dQueries )
  6850. tOut.SendArray ( i.m_dResult );
  6851. }
  6852. /////////////////////////////////////////////////////////////////////////////
  6853. // KEYWORDS HANDLER
  6854. /////////////////////////////////////////////////////////////////////////////
  6855. static bool DoGetKeywords ( const CSphString & sIndex, const CSphString & sQuery, const GetKeywordsSettings_t & tSettings, CSphVector <CSphKeywordInfo> & dKeywords, CSphString & sError, SearchFailuresLog_c & tFailureLog );
  6856. static void HandleCommandKeywords ( ISphOutputBuffer & tOut, WORD uVer, InputBuffer_c & tReq )
  6857. {
  6858. if ( !CheckCommandVersion ( uVer, VER_COMMAND_KEYWORDS, tOut ) )
  6859. return;
  6860. GetKeywordsSettings_t tSettings;
  6861. CSphString sQuery = tReq.GetString ();
  6862. CSphString sIndex = tReq.GetString ();
  6863. tSettings.m_bStats = !!tReq.GetInt ();
  6864. if ( uVer>=0x101 )
  6865. {
  6866. tSettings.m_bFoldLemmas = !!tReq.GetInt ();
  6867. tSettings.m_bFoldBlended = !!tReq.GetInt ();
  6868. tSettings.m_bFoldWildcards = !!tReq.GetInt ();
  6869. tSettings.m_iExpansionLimit = tReq.GetInt ();
  6870. }
  6871. CSphString sError;
  6872. SearchFailuresLog_c tFailureLog;
  6873. CSphVector < CSphKeywordInfo > dKeywords;
  6874. bool bOk = DoGetKeywords ( sIndex, sQuery, tSettings, dKeywords, sError, tFailureLog );
  6875. if ( !bOk )
  6876. {
  6877. SendErrorReply ( tOut, "%s", sError.cstr() );
  6878. return;
  6879. }
  6880. // just log distribute index error as command has no warning filed to pass such error into
  6881. if ( !tFailureLog.IsEmpty() )
  6882. {
  6883. StringBuilder_c sErrorBuf;
  6884. tFailureLog.BuildReport ( sErrorBuf );
  6885. sphWarning ( "%s", sErrorBuf.cstr() );
  6886. }
  6887. auto tReply = APIAnswer ( tOut, VER_COMMAND_KEYWORDS );
  6888. tOut.SendInt ( dKeywords.GetLength () );
  6889. for ( auto & dKeyword : dKeywords )
  6890. {
  6891. tOut.SendString ( dKeyword.m_sTokenized.cstr () );
  6892. tOut.SendString ( dKeyword.m_sNormalized.cstr () );
  6893. if ( uVer>=0x101 )
  6894. tOut.SendInt ( dKeyword.m_iQpos );
  6895. if ( tSettings.m_bStats )
  6896. {
  6897. tOut.SendInt ( dKeyword.m_iDocs );
  6898. tOut.SendInt ( dKeyword.m_iHits );
  6899. }
  6900. }
  6901. }
  6902. /////////////////////////////////////////////////////////////////////////////
  6903. // UPDATES HANDLER
  6904. /////////////////////////////////////////////////////////////////////////////
  6905. class UpdateRequestBuilder_c : public RequestBuilder_i
  6906. {
  6907. public:
  6908. explicit UpdateRequestBuilder_c ( AttrUpdateSharedPtr_t pUpd ) : m_pUpd ( pUpd ) {}
  6909. void BuildRequest ( const AgentConn_t & tAgent, ISphOutputBuffer& tOut ) const final;
  6910. protected:
  6911. AttrUpdateSharedPtr_t m_pUpd;
  6912. };
  6913. class UpdateReplyParser_c : public ReplyParser_i
  6914. {
  6915. public:
  6916. explicit UpdateReplyParser_c ( int * pUpd )
  6917. : m_pUpdated ( pUpd )
  6918. {}
  6919. bool ParseReply ( MemInputBuffer_c & tReq, AgentConn_t & ) const final
  6920. {
  6921. *m_pUpdated += tReq.GetDword ();
  6922. return true;
  6923. }
  6924. protected:
  6925. int * m_pUpdated;
  6926. };
  6927. void UpdateRequestBuilder_c::BuildRequest ( const AgentConn_t & tAgent, ISphOutputBuffer & tOut ) const
  6928. {
  6929. const char * sIndexes = tAgent.m_tDesc.m_sIndexes.cstr();
  6930. assert ( m_pUpd->m_dAttributes.all_of ( [&] ( const TypedAttribute_t & tAttr ) { return ( tAttr.m_eType!=SPH_ATTR_INT64SET ); } ) );
  6931. auto& tUpd = *m_pUpd;
  6932. // API header
  6933. auto tHdr = APIHeader ( tOut, SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE );
  6934. tOut.SendString ( sIndexes );
  6935. tOut.SendInt ( tUpd.m_dAttributes.GetLength() );
  6936. tOut.SendInt ( tUpd.m_bIgnoreNonexistent ? 1 : 0 );
  6937. for ( const auto & i : tUpd.m_dAttributes )
  6938. {
  6939. tOut.SendString ( i.m_sName.cstr() );
  6940. UpdateType_e eUpdate;
  6941. switch ( i.m_eType )
  6942. {
  6943. case SPH_ATTR_UINT32SET: eUpdate = UPDATE_MVA32; break;
  6944. case SPH_ATTR_STRING:
  6945. case SPH_ATTR_JSON: eUpdate = UPDATE_STRING; break;
  6946. default: eUpdate = UPDATE_INT; break;
  6947. };
  6948. tOut.SendInt ( eUpdate );
  6949. }
  6950. tOut.SendInt ( tUpd.m_dDocids.GetLength() );
  6951. ARRAY_FOREACH ( iDoc, tUpd.m_dDocids )
  6952. {
  6953. tOut.SendUint64 ( tUpd.m_dDocids[iDoc] );
  6954. const DWORD* pPool = tUpd.m_dPool.Begin() + tUpd.GetRowOffset ( iDoc );
  6955. for ( const auto & i : tUpd.m_dAttributes )
  6956. {
  6957. DWORD uVal = *pPool++;
  6958. switch ( i.m_eType )
  6959. {
  6960. case SPH_ATTR_UINT32SET:
  6961. {
  6962. // size down in case of MVA
  6963. // MVA stored as mva64 in pool but API could handle only mva32 due to HandleCommandUpdate
  6964. // SphinxQL only could work either mva32 or mva64 and only SphinxQL could receive mva64 updates
  6965. // SphinxQL master communicate to agent via SphinxqlRequestBuilder_c
  6966. const DWORD * pEnd = pPool + uVal;
  6967. tOut.SendDword ( uVal/2 );
  6968. while ( pPool<pEnd )
  6969. {
  6970. auto iVal = *(int64_t*)pPool;
  6971. tOut.SendDword ( iVal&0xFFFFFFFF );
  6972. pPool += 2;
  6973. }
  6974. }
  6975. break;
  6976. case SPH_ATTR_STRING:
  6977. case SPH_ATTR_JSON:
  6978. {
  6979. DWORD uBlobLen = *pPool++;
  6980. tOut.SendDword ( uBlobLen );
  6981. tOut.SendBytes ( tUpd.m_dBlobs.Begin()+uVal, uBlobLen );
  6982. }
  6983. break;
  6984. default:
  6985. tOut.SendDword ( uVal );
  6986. break;
  6987. }
  6988. }
  6989. }
  6990. }
  6991. static void DoCommandUpdate ( const CSphString & sIndex, const CSphString& sCluster, const char * sDistributed, AttrUpdateSharedPtr_t pUpd,
  6992. bool bBlobUpdate, int & iSuccesses, int & iUpdated, SearchFailuresLog_c & dFails )
  6993. {
  6994. int iUpd = 0;
  6995. CSphString sError, sWarning;
  6996. RtAccum_t tAcc;
  6997. ReplicationCommand_t* pCmd = tAcc.AddCommand ( ReplicationCommand_e::UPDATE_API, sIndex, sCluster );
  6998. assert ( pCmd );
  6999. pCmd->m_pUpdateAPI = std::move(pUpd);
  7000. pCmd->m_bBlobUpdate = bBlobUpdate;
  7001. HandleCmdReplicate ( tAcc, sError, sWarning, iUpd );
  7002. if ( iUpd<0 )
  7003. {
  7004. dFails.Submit ( sIndex, sDistributed, sError.cstr() );
  7005. } else
  7006. {
  7007. iUpdated += iUpd;
  7008. ++iSuccesses;
  7009. if ( sWarning.Length() )
  7010. dFails.Submit ( sIndex, sDistributed, sWarning.cstr() );
  7011. }
  7012. }
  7013. using DistrPtrs_t = VecRefPtrs_t< const DistributedIndex_t *>;
  7014. static bool ExtractDistributedIndexes ( const StrVec_t &dNames, DistrPtrs_t &dDistributed, CSphString& sMissed )
  7015. {
  7016. dDistributed.Reset();
  7017. dDistributed.Resize( dNames.GetLength () );
  7018. dDistributed.ZeroVec ();
  7019. ARRAY_FOREACH ( i, dNames )
  7020. {
  7021. if ( !g_pLocalIndexes->Contains ( dNames[i] ) )
  7022. {
  7023. // search amongst distributed and copy for further processing
  7024. dDistributed[i] = GetDistr ( dNames[i] );
  7025. if ( !dDistributed[i] )
  7026. {
  7027. sMissed = dNames[i];
  7028. return false;
  7029. }
  7030. dDistributed[i]->AddRef ();
  7031. }
  7032. }
  7033. return true;
  7034. }
  7035. void HandleCommandUpdate ( ISphOutputBuffer & tOut, int iVer, InputBuffer_c & tReq )
  7036. {
  7037. if ( !CheckCommandVersion ( iVer, VER_COMMAND_UPDATE, tOut ) )
  7038. return;
  7039. // parse request
  7040. CSphString sIndexes = tReq.GetString ();
  7041. AttrUpdateSharedPtr_t pUpd { new CSphAttrUpdate };
  7042. CSphAttrUpdate& tUpd = *pUpd;
  7043. CSphVector<DWORD> dMva;
  7044. tUpd.m_dAttributes.Resize ( tReq.GetDword() ); // FIXME! check this
  7045. if ( iVer>=0x103 )
  7046. tUpd.m_bIgnoreNonexistent = ( tReq.GetDword() & 1 )!=0;
  7047. bool bBlobUpdate = false;
  7048. for ( auto & i : tUpd.m_dAttributes )
  7049. {
  7050. i.m_sName = tReq.GetString();
  7051. if ( i.m_sName==sphGetDocidName() )
  7052. return SendErrorReply ( tOut, "'id' attribute cannot be updated" );
  7053. i.m_eType = SPH_ATTR_INTEGER;
  7054. if ( iVer>=0x102 )
  7055. {
  7056. auto eUpdate = (UpdateType_e)tReq.GetDword();
  7057. switch ( eUpdate )
  7058. {
  7059. case UPDATE_MVA32:
  7060. i.m_eType = SPH_ATTR_UINT32SET;
  7061. bBlobUpdate = true;
  7062. break;
  7063. case UPDATE_STRING:
  7064. case UPDATE_JSON:
  7065. i.m_eType = SPH_ATTR_STRING;
  7066. bBlobUpdate = true;
  7067. break;
  7068. default:
  7069. break;
  7070. }
  7071. }
  7072. }
  7073. int iNumUpdates = tReq.GetInt (); // FIXME! check this
  7074. tUpd.m_dDocids.Reserve ( iNumUpdates );
  7075. tUpd.m_dRowOffset.Reserve ( iNumUpdates );
  7076. for ( int i=0; i<iNumUpdates; ++i )
  7077. {
  7078. // v.1.0 always sends 32-bit ids; v.1.1+ always send 64-bit ones
  7079. uint64_t uDocid = ( iVer>=0x101 ) ? tReq.GetUint64 () : tReq.GetDword ();
  7080. tUpd.m_dDocids.Add ( uDocid );
  7081. tUpd.m_dRowOffset.Add ( tUpd.m_dPool.GetLength() );
  7082. for ( const auto & iAttr : tUpd.m_dAttributes )
  7083. {
  7084. switch ( iAttr.m_eType )
  7085. {
  7086. case SPH_ATTR_UINT32SET:
  7087. {
  7088. DWORD uCount = tReq.GetDword ();
  7089. if ( !uCount )
  7090. {
  7091. tUpd.m_dPool.Add ( 0 );
  7092. continue;
  7093. }
  7094. dMva.Resize ( uCount );
  7095. for ( DWORD j=0; j<uCount; j++ )
  7096. dMva[j] = tReq.GetDword();
  7097. dMva.Uniq(); // don't need dupes within MVA
  7098. tUpd.m_dPool.Add ( dMva.GetLength()*2 );
  7099. ARRAY_FOREACH ( j, dMva )
  7100. {
  7101. *(int64_t*)tUpd.m_dPool.AddN(2) = dMva[j]; // dummy expander mva32 -> mva64
  7102. }
  7103. }
  7104. break;
  7105. case SPH_ATTR_STRING:
  7106. {
  7107. DWORD uLen = tReq.GetDword();
  7108. tUpd.m_dPool.Add ( tUpd.m_dBlobs.GetLength() );
  7109. tUpd.m_dPool.Add ( uLen );
  7110. if ( uLen )
  7111. {
  7112. // extra zeroes for json parser
  7113. BYTE * pAdded = tUpd.m_dBlobs.AddN ( uLen+2 );
  7114. if ( !tReq.GetBytes ( pAdded, uLen ) )
  7115. return SendErrorReply ( tOut, "error reading string" );
  7116. pAdded[uLen] = 0;
  7117. pAdded[uLen+1] = 0;
  7118. }
  7119. }
  7120. break;
  7121. default:
  7122. tUpd.m_dPool.Add ( tReq.GetDword() );
  7123. break;
  7124. }
  7125. }
  7126. }
  7127. if ( tReq.GetError() )
  7128. return SendErrorReply ( tOut, "invalid or truncated request" );
  7129. // check index names
  7130. StrVec_t dIndexNames;
  7131. ParseIndexList ( sIndexes, dIndexNames );
  7132. if ( dIndexNames.IsEmpty() )
  7133. return SendErrorReply ( tOut, "no valid indexes in update request" );
  7134. DistrPtrs_t dDistributed;
  7135. // copy distributed indexes description
  7136. CSphString sMissed;
  7137. if ( !ExtractDistributedIndexes ( dIndexNames, dDistributed, sMissed ) )
  7138. return SendErrorReply ( tOut, "unknown index '%s' in update request", sMissed.cstr() );
  7139. // do update
  7140. SearchFailuresLog_c dFails;
  7141. int iSuccesses = 0;
  7142. int iUpdated = 0;
  7143. ARRAY_FOREACH ( iIdx, dIndexNames )
  7144. {
  7145. const CSphString & sReqIndex = dIndexNames[iIdx];
  7146. auto pLocal = GetServed ( sReqIndex );
  7147. if ( pLocal )
  7148. {
  7149. DoCommandUpdate ( sReqIndex, pLocal->m_sCluster, nullptr, pUpd, bBlobUpdate, iSuccesses, iUpdated, dFails );
  7150. } else if ( dDistributed[iIdx] )
  7151. {
  7152. auto * pDist = dDistributed[iIdx];
  7153. assert ( !pDist->IsEmpty() );
  7154. for ( const CSphString & sLocal : pDist->m_dLocal )
  7155. {
  7156. auto pServed = GetServed ( sLocal );
  7157. if ( !pServed )
  7158. continue;
  7159. DoCommandUpdate ( sLocal, pServed->m_sCluster, sReqIndex.cstr(), pUpd, bBlobUpdate, iSuccesses, iUpdated, dFails );
  7160. }
  7161. // update remote agents
  7162. if ( !dDistributed[iIdx]->m_dAgents.IsEmpty() )
  7163. {
  7164. VecRefPtrsAgentConn_t dAgents;
  7165. pDist->GetAllHosts ( dAgents );
  7166. // connect to remote agents and query them
  7167. UpdateRequestBuilder_c tReqBuilder ( pUpd );
  7168. UpdateReplyParser_c tParser ( &iUpdated );
  7169. iSuccesses += PerformRemoteTasks ( dAgents, &tReqBuilder, &tParser );
  7170. }
  7171. }
  7172. }
  7173. // serve reply to client
  7174. StringBuilder_c sReport;
  7175. dFails.BuildReport ( sReport );
  7176. if ( !iSuccesses )
  7177. return SendErrorReply ( tOut, "%s", sReport.cstr() );
  7178. auto tReply = APIAnswer ( tOut, VER_COMMAND_UPDATE, dFails.IsEmpty() ? SEARCHD_OK : SEARCHD_WARNING );
  7179. if ( !dFails.IsEmpty() )
  7180. tOut.SendString ( sReport.cstr () );
  7181. tOut.SendInt ( iUpdated );
  7182. }
  7183. //////////////////////////////////////////////////////////////////////////
  7184. // STATUS HANDLER
  7185. //////////////////////////////////////////////////////////////////////////
  7186. void BuildStatus ( VectorLike & dStatus )
  7187. {
  7188. auto & g_tStats = gStats ();
  7189. const char * OFF = "OFF";
  7190. const int64_t iQueriesDiv = Max ( g_tStats.m_iQueries.load ( std::memory_order_relaxed ), 1 );
  7191. const int64_t iDistQueriesDiv = Max ( g_tStats.m_iDistQueries.load ( std::memory_order_relaxed ), 1 );
  7192. const int64_t iDiv1000 = iQueriesDiv * 1000;
  7193. const int64_t iDDiv1000 = iDistQueriesDiv * 1000;
  7194. dStatus.SetColName ( "Counter" );
  7195. // FIXME? non-transactional!!!
  7196. dStatus.MatchTupletf ( "uptime", "%u", (DWORD) time ( nullptr )-g_tStats.m_uStarted );
  7197. dStatus.MatchTupletf ( "connections", "%l", g_tStats.m_iConnections.load ( std::memory_order_relaxed ) );
  7198. dStatus.MatchTupletf ( "maxed_out", "%l", g_tStats.m_iMaxedOut.load ( std::memory_order_relaxed ) );
  7199. dStatus.MatchTuplet ( "version" , g_sStatusVersion.cstr() );
  7200. dStatus.MatchTuplet ( "mysql_version", g_sMySQLVersion.cstr() );
  7201. for ( auto i=0; i<SEARCHD_COMMAND_TOTAL; ++i)
  7202. {
  7203. if ( i==SEARCHD_COMMAND_UNUSED_6 )
  7204. continue;
  7205. dStatus.MatchTupletf ( szCommand ( i ), "%l", g_tStats.m_iCommandCount[i].load ( std::memory_order_relaxed ) );
  7206. }
  7207. auto iConnects = g_tStats.m_iAgentConnectTFO.load ( std::memory_order_relaxed )
  7208. +g_tStats.m_iAgentConnect.load ( std::memory_order_relaxed );
  7209. dStatus.MatchTupletf ( "agent_connect", "%l", iConnects );
  7210. dStatus.MatchTupletf ( "agent_tfo", "%l", g_tStats.m_iAgentConnectTFO.load ( std::memory_order_relaxed ) );
  7211. dStatus.MatchTupletf ( "agent_retry", "%l", g_tStats.m_iAgentRetry.load ( std::memory_order_relaxed ) );
  7212. dStatus.MatchTupletf ( "queries", "%l", g_tStats.m_iQueries.load ( std::memory_order_relaxed ) );
  7213. dStatus.MatchTupletf ( "dist_queries", "%l", g_tStats.m_iDistQueries.load ( std::memory_order_relaxed ) );
  7214. // status of thread pool
  7215. dStatus.MatchTupletf ( "workers_total", "%d", GlobalWorkPool ()->WorkingThreads () );
  7216. dStatus.MatchTupletf ( "workers_active", "%d", myinfo::CountTasks () );
  7217. dStatus.MatchTupletf ( "workers_clients", "%d", myinfo::CountClients () );
  7218. dStatus.MatchTupletf ( "workers_clients_vip", "%u", session::GetVips() );
  7219. dStatus.MatchTupletf ( "work_queue_length", "%d", GlobalWorkPool ()->Works () );
  7220. assert ( g_pDistIndexes );
  7221. auto pDistSnapshot = g_pDistIndexes->GetHash();
  7222. for ( auto& tIt : *pDistSnapshot )
  7223. {
  7224. const char * sIdx = tIt.first.cstr();
  7225. const auto& dAgents = tIt.second->m_dAgents;
  7226. StringBuilder_c sKey;
  7227. ARRAY_FOREACH ( i, dAgents )
  7228. {
  7229. MultiAgentDescRefPtr_c pMultiAgent = dAgents[i];
  7230. MultiAgentDesc_c& dMultiAgent = *pMultiAgent;
  7231. ARRAY_FOREACH ( j, dMultiAgent )
  7232. {
  7233. const auto pMetrics = dMultiAgent[j].m_pMetrics;
  7234. for ( int k = 0; k<eMaxAgentStat; ++k )
  7235. {
  7236. sKey.Clear();
  7237. sKey.Sprintf ( "ag_%s_%d_%d_%s", sIdx, i+1, j+1, sAgentStatsNames[k] );
  7238. dStatus.MatchTupletf ( sKey.cstr (), "%l", pMetrics->m_dCounters[k].load (std::memory_order_relaxed ) );
  7239. }
  7240. for ( int k = 0; k<ehMaxStat; ++k )
  7241. {
  7242. sKey.Clear ();
  7243. sKey.Sprintf ( "ag_%s_%d_%d_%s", sIdx, i+1, j+1, sAgentStatsNames[eMaxAgentStat+k] );
  7244. const char * sFmt = ( k==ehTotalMsecs || k==ehAverageMsecs || k==ehMaxMsecs ) ? "%0.3F" : "%l";
  7245. dStatus.MatchTupletf ( sKey.cstr (), sFmt, pMetrics->m_dMetrics[k] );
  7246. }
  7247. }
  7248. }
  7249. }
  7250. dStatus.MatchTupletf ( "query_wall", "%0.3F", g_tStats.m_iQueryTime.load ( std::memory_order_relaxed ) / 1000 );
  7251. if ( g_bCpuStats )
  7252. dStatus.MatchTupletf ( "query_cpu", "%0.3F", g_tStats.m_iQueryCpuTime.load ( std::memory_order_relaxed ) / 1000 );
  7253. else
  7254. dStatus.MatchTuplet ( "query_cpu", OFF);
  7255. dStatus.MatchTupletf ( "dist_wall", "%0.3F", g_tStats.m_iDistWallTime.load ( std::memory_order_relaxed ) / 1000 );
  7256. dStatus.MatchTupletf ( "dist_local", "%0.3F", g_tStats.m_iDistLocalTime.load ( std::memory_order_relaxed ) / 1000 );
  7257. dStatus.MatchTupletf ( "dist_wait", "%0.3F", g_tStats.m_iDistWaitTime.load ( std::memory_order_relaxed ) / 1000 );
  7258. if ( g_bIOStats )
  7259. {
  7260. dStatus.MatchTupletf ( "query_reads", "%l", g_tStats.m_iDiskReads.load ( std::memory_order_relaxed ) );
  7261. dStatus.MatchTupletf ( "query_readkb", "%l", g_tStats.m_iDiskReadBytes.load ( std::memory_order_relaxed )/ 1024 );
  7262. dStatus.MatchTupletf ( "query_readtime", "%l", g_tStats.m_iDiskReadTime.load ( std::memory_order_relaxed ) );
  7263. } else
  7264. {
  7265. dStatus.MatchTuplet ( "query_reads", OFF );
  7266. dStatus.MatchTuplet ( "query_readkb", OFF );
  7267. dStatus.MatchTuplet ( "query_readtime", OFF );
  7268. }
  7269. if ( g_tStats.m_iPredictedTime.load ( std::memory_order_relaxed )
  7270. || g_tStats.m_iAgentPredictedTime.load ( std::memory_order_relaxed ) )
  7271. {
  7272. dStatus.MatchTupletf ( "predicted_time", "%l", g_tStats.m_iPredictedTime.load ( std::memory_order_relaxed ) );
  7273. dStatus.MatchTupletf ( "dist_predicted_time", "%l", g_tStats.m_iAgentPredictedTime.load ( std::memory_order_relaxed ) );
  7274. }
  7275. dStatus.MatchTupletf ( "avg_query_wall", "%0.3F", g_tStats.m_iQueryTime.load ( std::memory_order_relaxed ) / iDiv1000 );
  7276. if ( g_bCpuStats )
  7277. dStatus.MatchTupletf ( "avg_query_cpu", "%0.3F", g_tStats.m_iQueryCpuTime.load ( std::memory_order_relaxed ) / iDiv1000 );
  7278. else
  7279. dStatus.MatchTuplet ( "avg_query_cpu", OFF );
  7280. dStatus.MatchTupletf ( "avg_dist_wall", "%0.3F", g_tStats.m_iDistWallTime.load ( std::memory_order_relaxed ) / iDDiv1000 );
  7281. dStatus.MatchTupletf ( "avg_dist_local", "%0.3F", g_tStats.m_iDistLocalTime.load ( std::memory_order_relaxed ) / iDDiv1000 );
  7282. dStatus.MatchTupletf ( "avg_dist_wait", "%0.3F", g_tStats.m_iDistWaitTime.load ( std::memory_order_relaxed ) / iDDiv1000 );
  7283. if ( g_bIOStats )
  7284. {
  7285. dStatus.MatchTupletf ( "avg_query_reads", "%0.1F", g_tStats.m_iDiskReads.load ( std::memory_order_relaxed ) * 10 / iQueriesDiv );
  7286. dStatus.MatchTupletf ( "avg_query_readkb", "%0.1F", g_tStats.m_iDiskReadBytes.load ( std::memory_order_relaxed ) * 10 / (iQueriesDiv*1024) );
  7287. dStatus.MatchTupletf ( "avg_query_readtime", "%0.3F", g_tStats.m_iDiskReadTime.load ( std::memory_order_relaxed ) / iDiv1000 );
  7288. } else
  7289. {
  7290. dStatus.MatchTuplet ( "avg_query_reads", OFF );
  7291. dStatus.MatchTuplet ( "avg_query_readkb", OFF );
  7292. dStatus.MatchTuplet ( "avg_query_readtime", OFF );
  7293. }
  7294. const QcacheStatus_t & s = QcacheGetStatus();
  7295. dStatus.MatchTupletf ( "qcache_max_bytes", "%l", s.m_iMaxBytes );
  7296. dStatus.MatchTupletf ( "qcache_thresh_msec", "%d", s.m_iThreshMs );
  7297. dStatus.MatchTupletf ( "qcache_ttl_sec", "%d", s.m_iTtlS );
  7298. dStatus.MatchTupletf ( "qcache_cached_queries", "%d", s.m_iCachedQueries );
  7299. dStatus.MatchTupletf ( "qcache_used_bytes", "%l", s.m_iUsedBytes );
  7300. dStatus.MatchTupletf ( "qcache_hits", "%l", s.m_iHits );
  7301. // clusters
  7302. ReplicateClustersStatus ( dStatus );
  7303. }
  7304. // that is returned to MySQL 'statistic' command ('status' in mysql cli)
  7305. void BuildStatusOneline ( StringBuilder_c & sOut )
  7306. {
  7307. auto iThreads = GlobalWorkPool ()->WorkingThreads ();
  7308. auto iQueue = GlobalWorkPool ()->Works ();
  7309. auto iTasks = myinfo::CountTasks ();
  7310. auto & g_tStats = gStats ();
  7311. sOut.StartBlock ( " " );
  7312. sOut
  7313. << "Uptime:" << (DWORD) time ( NULL )-g_tStats.m_uStarted
  7314. << " Threads:" << iThreads
  7315. << " Queue:" << iQueue
  7316. << " Clients:" << myinfo::CountClients()
  7317. << " Vip clients:" << session::GetVips()
  7318. << " Tasks:" << iTasks
  7319. << " Queries:" << g_tStats.m_iQueries.load ( std::memory_order_relaxed );
  7320. sOut.Sprintf ( " Wall: %t", (int64_t)g_tStats.m_iQueryTime.load ( std::memory_order_relaxed ) );
  7321. sOut.Sprintf ( " CPU: %t", (int64_t)g_tStats.m_iQueryCpuTime.load ( std::memory_order_relaxed ) );
  7322. sOut.Sprintf ( "\nQueue/Th: %0.1F%", iQueue * 10 / iThreads );
  7323. sOut.Sprintf ( " Tasks/Th: %0.1F%", iTasks * 10 / iThreads );
  7324. }
  7325. void BuildOneAgentStatus ( VectorLike & dStatus, HostDashboardRefPtr_t pDash, const char * sPrefix="agent" )
  7326. {
  7327. assert ( pDash );
  7328. {
  7329. ScRL_t tGuard ( pDash->m_dMetricsLock );
  7330. if ( dStatus.MatchAddf ( "%s_hostname", sPrefix ) )
  7331. dStatus.Add ( pDash->m_tHost.GetMyUrl ().cstr () );
  7332. if ( dStatus.MatchAddf ( "%s_references", sPrefix ) )
  7333. dStatus.Addf( "%d", (int) pDash->GetRefcount()-1 ); // -1 since we currently also 'use' the agent, reading it's stats
  7334. if ( dStatus.MatchAddf ( "%s_ping", sPrefix ) )
  7335. dStatus.Add ( pDash->m_iNeedPing ? "yes" : "no" );
  7336. if ( dStatus.MatchAddf ( "%s_has_perspool", sPrefix ) )
  7337. dStatus.Add ( pDash->m_pPersPool ? "yes" : "no" );
  7338. if ( dStatus.MatchAddf ( "%s_need_resolve", sPrefix ) )
  7339. dStatus.Add ( pDash->m_tHost.m_bNeedResolve ? "yes" : "no" );
  7340. uint64_t iCur = sphMicroTimer();
  7341. uint64_t iLastAccess = iCur - pDash->m_iLastQueryTime;
  7342. if ( dStatus.MatchAddf ( "%s_lastquery", sPrefix ) )
  7343. dStatus.Addf ( "%.2F", iLastAccess / 10000 );
  7344. iLastAccess = iCur - pDash->m_iLastAnswerTime;
  7345. if ( dStatus.MatchAddf ( "%s_lastanswer", sPrefix ) )
  7346. dStatus.Addf ( "%.2F", iLastAccess / 10000 );
  7347. uint64_t iLastTimer = pDash->m_iLastAnswerTime-pDash->m_iLastQueryTime;
  7348. if ( dStatus.MatchAddf ( "%s_lastperiodmsec", sPrefix ) )
  7349. dStatus.Addf ( "%.3D", iLastTimer );
  7350. if ( dStatus.MatchAddf ( "%s_pingtripmsec", sPrefix ) )
  7351. dStatus.Addf ( "%.3F", pDash->m_uPingTripUS );
  7352. if ( dStatus.MatchAddf ( "%s_errorsarow", sPrefix ) )
  7353. dStatus.Addf ( "%l", pDash->m_iErrorsARow );
  7354. }
  7355. int iPeriods = 1;
  7356. while ( iPeriods>0 )
  7357. {
  7358. HostMetricsSnapshot_t dMetricsSnapshot;
  7359. pDash->GetCollectedMetrics ( dMetricsSnapshot, iPeriods );
  7360. {
  7361. for ( int j = 0; j<ehMaxStat+eMaxAgentStat; ++j )
  7362. // hack. Avoid microseconds in human-readable statistic
  7363. if ( j==ehTotalMsecs && dStatus.MatchAddf ( "%s_%dperiods_msecsperqueryy", sPrefix, iPeriods ) )
  7364. {
  7365. if ( dMetricsSnapshot[ehConnTries]>0 )
  7366. dStatus.Addf ( "%.2F", dMetricsSnapshot[ehTotalMsecs] / dMetricsSnapshot[ehConnTries] / 10 );
  7367. else
  7368. dStatus.Add ( "n/a" );
  7369. } else if ( dStatus.MatchAddf ( "%s_%dperiods_%s", sPrefix, iPeriods, sAgentStatsNames[j] ) )
  7370. {
  7371. if ( j==ehMaxMsecs || j==ehAverageMsecs )
  7372. dStatus.Addf ( "%.2F", dMetricsSnapshot[j] / 10 );
  7373. else
  7374. dStatus.Addf ( "%l", dMetricsSnapshot[j] );
  7375. }
  7376. }
  7377. if ( iPeriods==1 )
  7378. iPeriods = 5;
  7379. else if ( iPeriods==5 )
  7380. iPeriods = STATS_DASH_PERIODS;
  7381. else if ( iPeriods==STATS_DASH_PERIODS )
  7382. iPeriods = -1;
  7383. }
  7384. }
  7385. static bool BuildDistIndexStatus ( VectorLike & dStatus, const CSphString& sIndex )
  7386. {
  7387. auto pDistr = GetDistr ( sIndex );
  7388. if ( !pDistr )
  7389. return false;
  7390. ARRAY_FOREACH ( i, pDistr->m_dLocal )
  7391. {
  7392. if ( dStatus.MatchAddf ( "dstindex_local_%d", i+1 ) )
  7393. dStatus.Add ( pDistr->m_dLocal[i].cstr() );
  7394. }
  7395. CSphString sKey;
  7396. ARRAY_FOREACH ( i, pDistr->m_dAgents )
  7397. {
  7398. MultiAgentDescRefPtr_c pAgents = pDistr->m_dAgents[i];
  7399. const MultiAgentDesc_c& tAgents = *pAgents;
  7400. if ( dStatus.MatchAddf ( "dstindex_%d_is_ha", i+1 ) )
  7401. dStatus.Add ( tAgents.IsHA()? "1": "0" );
  7402. auto dWeights = tAgents.GetWeights ();
  7403. ARRAY_FOREACH ( j, tAgents )
  7404. {
  7405. if ( tAgents.IsHA() )
  7406. sKey.SetSprintf ( "dstindex_%dmirror%d", i+1, j+1 );
  7407. else
  7408. sKey.SetSprintf ( "dstindex_%dagent", i+1 );
  7409. const AgentDesc_t & dDesc = tAgents[j];
  7410. if ( dStatus.MatchAddf ( "%s_id", sKey.cstr () ) )
  7411. dStatus.Addf ( "%s:%s", dDesc.GetMyUrl ().cstr (), dDesc.m_sIndexes.cstr () );
  7412. if ( tAgents.IsHA() && dStatus.MatchAddf ( "%s_probability_weight", sKey.cstr () ) )
  7413. dStatus.Addf ( "%0.2f%%", dWeights[j] );
  7414. if ( dStatus.MatchAddf ( "%s_is_blackhole", sKey.cstr () ) )
  7415. dStatus.Add ( dDesc.m_bBlackhole ? "1" : "0" );
  7416. if ( dStatus.MatchAddf ( "%s_is_persistent", sKey.cstr () ) )
  7417. dStatus.Add ( dDesc.m_bPersistent ? "1" : "0" );
  7418. }
  7419. }
  7420. return true;
  7421. }
  7422. /* commented out as not used
  7423. static bool operator < ( const IteratorDesc_t & tA, const IteratorDesc_t & tB )
  7424. {
  7425. if ( tA.m_sAttr < tB.m_sAttr )
  7426. return true;
  7427. return tA.m_sType<tB.m_sType;
  7428. }
  7429. static bool operator == ( const IteratorDesc_t & tA, const IteratorDesc_t & tB )
  7430. {
  7431. return tA.m_sAttr==tB.m_sAttr && tA.m_sType==tB.m_sType;
  7432. }
  7433. */
  7434. void BuildAgentStatus ( VectorLike &dStatus, const CSphString& sIndexOrAgent )
  7435. {
  7436. if ( !sIndexOrAgent.IsEmpty() )
  7437. {
  7438. if ( !BuildDistIndexStatus ( dStatus, sIndexOrAgent ) )
  7439. {
  7440. auto pAgent = Dashboard::FindAgent ( sIndexOrAgent );
  7441. if ( pAgent )
  7442. BuildOneAgentStatus ( dStatus, pAgent );
  7443. else
  7444. dStatus.MatchTupletf ( "status_error", "No such distributed index or agent: %s", sIndexOrAgent.cstr () );
  7445. }
  7446. return;
  7447. }
  7448. dStatus.SetColName ( "Key" );
  7449. dStatus.MatchTupletf( "status_period_seconds", "%d", g_uHAPeriodKarmaS );
  7450. dStatus.MatchTupletf ( "status_stored_periods", "%d", STATS_DASH_PERIODS );
  7451. auto dDashes = Dashboard::GetActiveHosts();
  7452. CSphString sPrefix;
  7453. ARRAY_FOREACH ( i, dDashes )
  7454. {
  7455. sPrefix.SetSprintf ( "ag_%d", i );
  7456. BuildOneAgentStatus ( dStatus, dDashes[i], sPrefix.cstr() );
  7457. }
  7458. }
  7459. static void AddIOStatsToMeta ( VectorLike & dStatus, const CSphIOStats & tStats, const char * sPrefix )
  7460. {
  7461. if ( dStatus.MatchAddf ( "%s%s", sPrefix, "io_read_time" ) )
  7462. dStatus.Addf( "%.3F", tStats.m_iReadTime);
  7463. if ( dStatus.MatchAddf ( "%s%s", sPrefix, "io_read_ops" ) )
  7464. dStatus.Addf ( "%u", tStats.m_iReadOps );
  7465. if ( dStatus.MatchAddf ( "%s%s", sPrefix, "io_read_kbytes" ) )
  7466. dStatus.Addf ( "%d.%d", (int) ( tStats.m_iReadBytes / 1024 ), (int) ( tStats.m_iReadBytes % 1024 ) / 100 );
  7467. if ( dStatus.MatchAddf ( "%s%s", sPrefix, "io_write_time" ) )
  7468. dStatus.Addf ( "%.3F", tStats.m_iWriteTime );
  7469. if ( dStatus.MatchAddf ( "%s%s", sPrefix, "io_write_ops" ) )
  7470. dStatus.Addf ( "%u", tStats.m_iWriteOps );
  7471. if ( dStatus.MatchAddf ( "%s%s", sPrefix, "io_write_kbytes" ) )
  7472. dStatus.Addf ( "%d.%d", (int)( tStats.m_iWriteBytes/1024 ), (int)( tStats.m_iWriteBytes%1024 )/100 );
  7473. }
  7474. void BuildMeta ( VectorLike & dStatus, const CSphQueryResultMeta & tMeta )
  7475. {
  7476. if ( !tMeta.m_sError.IsEmpty() )
  7477. dStatus.MatchTuplet ( "error", tMeta.m_sError.cstr () );
  7478. if ( !tMeta.m_sWarning.IsEmpty() )
  7479. dStatus.MatchTuplet ( "warning", tMeta.m_sWarning.cstr () );
  7480. dStatus.MatchTupletf ( "total", "%d", tMeta.m_iMatches );
  7481. dStatus.MatchTupletf ( "total_found", "%l", tMeta.m_iTotalMatches );
  7482. dStatus.MatchTupletf ( "total_relation", "%s", tMeta.m_bTotalMatchesApprox ? "gte" : "eq" );
  7483. dStatus.MatchTupletf ( "time", "%.3F", tMeta.m_iQueryTime );
  7484. if ( tMeta.m_iMultiplier>1 )
  7485. dStatus.MatchTupletf ( "multiplier", "%d", tMeta.m_iMultiplier );
  7486. if ( g_bCpuStats )
  7487. {
  7488. dStatus.MatchTupletf ( "cpu_time", "%.3F", tMeta.m_iCpuTime );
  7489. dStatus.MatchTupletf ( "agents_cpu_time", "%.3F", tMeta.m_iAgentCpuTime );
  7490. }
  7491. if ( g_bIOStats )
  7492. {
  7493. AddIOStatsToMeta ( dStatus, tMeta.m_tIOStats, "" );
  7494. AddIOStatsToMeta ( dStatus, tMeta.m_tAgentIOStats, "agent_" );
  7495. }
  7496. if ( tMeta.m_bHasPrediction )
  7497. {
  7498. dStatus.MatchTupletf ( "local_fetched_docs", "%d", tMeta.m_tStats.m_iFetchedDocs );
  7499. dStatus.MatchTupletf ( "local_fetched_hits", "%d", tMeta.m_tStats.m_iFetchedHits );
  7500. dStatus.MatchTupletf ( "local_fetched_skips", "%d", tMeta.m_tStats.m_iSkips );
  7501. dStatus.MatchTupletf ( "predicted_time", "%l", tMeta.m_iPredictedTime );
  7502. if ( tMeta.m_iAgentPredictedTime )
  7503. dStatus.MatchTupletf ( "dist_predicted_time", "%l", tMeta.m_iAgentPredictedTime );
  7504. if ( tMeta.m_iAgentFetchedDocs || tMeta.m_iAgentFetchedHits || tMeta.m_iAgentFetchedSkips )
  7505. {
  7506. dStatus.MatchTupletf ( "dist_fetched_docs", "%d", tMeta.m_tStats.m_iFetchedDocs+tMeta.m_iAgentFetchedDocs );
  7507. dStatus.MatchTupletf ( "dist_fetched_hits", "%d", tMeta.m_tStats.m_iFetchedHits+tMeta.m_iAgentFetchedHits );
  7508. dStatus.MatchTupletf ( "dist_fetched_skips", "%d", tMeta.m_tStats.m_iSkips+tMeta.m_iAgentFetchedSkips );
  7509. }
  7510. }
  7511. auto dWords = tMeta.MakeSortedWordStat();
  7512. ARRAY_CONSTFOREACH( iWord, dWords )
  7513. {
  7514. auto * pWord = dWords[iWord];
  7515. assert ( pWord );
  7516. if ( dStatus.MatchAddf ( "keyword[%d]", iWord ) )
  7517. dStatus.Add ( pWord->first );
  7518. if ( dStatus.MatchAddf ( "docs[%d]", iWord ) )
  7519. dStatus.Addf ( "%l", pWord->second.first );
  7520. if ( dStatus.MatchAddf ( "hits[%d]", iWord ) )
  7521. dStatus.Addf ( "%l", pWord->second.second );
  7522. }
  7523. StringBuilder_c sIterators { ", " };
  7524. for ( const auto & i : tMeta.m_tIteratorStats.m_dIterators )
  7525. sIterators.Appendf ( "%s:%s (%d%%)", i.m_sAttr.cstr(), i.m_sType.cstr(), int(float(i.m_iUsed)/tMeta.m_tIteratorStats.m_iTotal*100.0f) );
  7526. if ( !sIterators.IsEmpty() )
  7527. dStatus.MatchTuplet ( "index", sIterators.cstr() );
  7528. }
  7529. void HandleCommandStatus ( ISphOutputBuffer & tOut, WORD uVer, InputBuffer_c & tReq )
  7530. {
  7531. if ( !CheckCommandVersion ( uVer, VER_COMMAND_STATUS, tOut ) )
  7532. return;
  7533. bool bGlobalStat = tReq.GetDword ()!=0;
  7534. VectorLike dStatus;
  7535. if ( bGlobalStat )
  7536. BuildStatus ( dStatus );
  7537. else
  7538. {
  7539. ScRL_t dMetaRlock ( g_tLastMetaLock );
  7540. auto & g_tStats = gStats ();
  7541. BuildMeta ( dStatus, g_tLastMeta );
  7542. if ( g_tStats.m_iPredictedTime.load ( std::memory_order_relaxed )
  7543. || g_tStats.m_iAgentPredictedTime.load ( std::memory_order_relaxed ) )
  7544. {
  7545. dStatus.MatchTupletf ( "predicted_time", "%l", g_tStats.m_iPredictedTime.load ( std::memory_order_relaxed ) );
  7546. dStatus.MatchTupletf ( "dist_predicted_time", "%l", g_tStats.m_iAgentPredictedTime.load ( std::memory_order_relaxed ) );
  7547. }
  7548. }
  7549. auto tReply = APIAnswer ( tOut, VER_COMMAND_STATUS );
  7550. tOut.SendInt ( dStatus.GetLength () / dStatus.Header ().GetLength () ); // rows
  7551. tOut.SendInt ( dStatus.Header ().GetLength () ); // cols
  7552. for ( const auto & dLines : dStatus )
  7553. tOut.SendString ( dLines.cstr() );
  7554. }
  7555. //////////////////////////////////////////////////////////////////////////
  7556. // FLUSH HANDLER
  7557. //////////////////////////////////////////////////////////////////////////
  7558. void HandleCommandFlush ( ISphOutputBuffer & tOut, WORD uVer )
  7559. {
  7560. if ( !CheckCommandVersion ( uVer, VER_COMMAND_FLUSHATTRS, tOut ) )
  7561. return;
  7562. int iTag = CommandFlush ();
  7563. // return last flush tag, just for the fun of it
  7564. auto tReply = APIAnswer ( tOut, VER_COMMAND_FLUSHATTRS );
  7565. tOut.SendInt ( iTag );
  7566. }
  7567. /////////////////////////////////////////////////////////////////////////////
  7568. // GENERAL HANDLER
  7569. /////////////////////////////////////////////////////////////////////////////
  7570. void HandleCommandSphinxql ( ISphOutputBuffer & tOut, WORD uVer, InputBuffer_c & tReq ); // definition is below
  7571. void HandleCommandJson ( ISphOutputBuffer & tOut, WORD uVer, InputBuffer_c & tReq );
  7572. void StatCountCommand ( SearchdCommand_e eCmd );
  7573. void HandleCommandUserVar ( ISphOutputBuffer & tOut, WORD uVer, InputBuffer_c & tReq );
  7574. void HandleCommandCallPq ( ISphOutputBuffer &tOut, WORD uVer, InputBuffer_c &tReq );
  7575. /// ping/pong exchange over API
  7576. void HandleCommandPing ( ISphOutputBuffer & tOut, WORD uVer, InputBuffer_c & tReq )
  7577. {
  7578. if ( !CheckCommandVersion ( uVer, VER_COMMAND_PING, tOut ) )
  7579. return;
  7580. // parse ping
  7581. int iCookie = tReq.GetInt();
  7582. if ( tReq.GetError () )
  7583. return;
  7584. // return last flush tag, just for the fun of it
  7585. auto tReply = APIAnswer ( tOut, VER_COMMAND_PING );
  7586. tOut.SendInt ( iCookie ); // echo the cookie back
  7587. }
  7588. void ExecuteApiCommand ( SearchdCommand_e eCommand, WORD uCommandVer, int iLength, InputBuffer_c & tBuf, ISphOutputBuffer & tOut )
  7589. {
  7590. auto & tSess = session::Info();
  7591. tSess.SetTaskState ( TaskState_e::QUERY );
  7592. // set on query guard
  7593. auto& tCrashQuery = GlobalCrashQueryGetRef();
  7594. tCrashQuery.m_eType = QUERY_API;
  7595. tCrashQuery.m_dQuery = { tBuf.GetBufferPtr(), iLength };
  7596. tCrashQuery.m_uCMD = eCommand;
  7597. tCrashQuery.m_uVer = uCommandVer;
  7598. // handle known commands
  7599. assert ( eCommand<SEARCHD_COMMAND_WRONG );
  7600. // count commands
  7601. StatCountCommand ( eCommand );
  7602. myinfo::SetCommand ( g_dApiCommands[eCommand] );
  7603. sphLogDebugv ( "conn %s(%d): got command %d, handling", tSess.szClientName(), tSess.GetConnID(), eCommand );
  7604. switch ( eCommand )
  7605. {
  7606. case SEARCHD_COMMAND_SEARCH: HandleCommandSearch ( tOut, uCommandVer, tBuf ); break;
  7607. case SEARCHD_COMMAND_EXCERPT: HandleCommandExcerpt ( tOut, uCommandVer, tBuf ); break;
  7608. case SEARCHD_COMMAND_KEYWORDS: HandleCommandKeywords ( tOut, uCommandVer, tBuf ); break;
  7609. case SEARCHD_COMMAND_UPDATE: HandleCommandUpdate ( tOut, uCommandVer, tBuf ); break;
  7610. case SEARCHD_COMMAND_STATUS: HandleCommandStatus ( tOut, uCommandVer, tBuf ); break;
  7611. case SEARCHD_COMMAND_FLUSHATTRS:HandleCommandFlush ( tOut, uCommandVer ); break;
  7612. case SEARCHD_COMMAND_SPHINXQL: HandleCommandSphinxql ( tOut, uCommandVer, tBuf ); break;
  7613. case SEARCHD_COMMAND_JSON: HandleCommandJson ( tOut, uCommandVer, tBuf ); break;
  7614. case SEARCHD_COMMAND_PING: HandleCommandPing ( tOut, uCommandVer, tBuf ); break;
  7615. case SEARCHD_COMMAND_UVAR: HandleCommandUserVar ( tOut, uCommandVer, tBuf ); break;
  7616. case SEARCHD_COMMAND_CALLPQ: HandleCommandCallPq ( tOut, uCommandVer, tBuf ); break;
  7617. case SEARCHD_COMMAND_CLUSTERPQ: HandleCommandClusterPq ( tOut, uCommandVer, tBuf, tSess.szClientName () ); break;
  7618. case SEARCHD_COMMAND_GETFIELD: HandleCommandGetField ( tOut, uCommandVer, tBuf ); break;
  7619. case SEARCHD_COMMAND_PERSIST: break; // already processes, here just for stat
  7620. default: assert ( 0 && "internal error: unhandled command" ); break;
  7621. }
  7622. }
  7623. void StmtErrorReporter_i::Error ( const char * sTemplate, ... )
  7624. {
  7625. StringBuilder_c sBuf;
  7626. va_list ap;
  7627. va_start ( ap, sTemplate );
  7628. sBuf.vAppendf ( sTemplate, ap );
  7629. va_end ( ap );
  7630. ErrorEx ( MYSQL_ERR_PARSE_ERROR, sBuf.cstr () );
  7631. }
  7632. class StmtErrorReporter_c final : public StmtErrorReporter_i
  7633. {
  7634. public:
  7635. explicit StmtErrorReporter_c ( RowBuffer_i & tBuffer, const char* szStmt = nullptr )
  7636. : m_tRowBuffer ( tBuffer )
  7637. , m_szStmt ( szStmt )
  7638. {}
  7639. void Ok ( int iAffectedRows, const CSphString & sWarning, int64_t iLastInsertId ) final
  7640. {
  7641. m_tRowBuffer.Ok ( iAffectedRows, ( sWarning.IsEmpty() ? 0 : 1 ), nullptr, false, iLastInsertId );
  7642. }
  7643. void Ok ( int iAffectedRows, int nWarnings ) final
  7644. {
  7645. m_tRowBuffer.Ok ( iAffectedRows, nWarnings );
  7646. }
  7647. void ErrorEx ( MysqlErrors_e iErr, const char * sError ) final
  7648. {
  7649. m_tRowBuffer.Error ( m_szStmt, sError, iErr );
  7650. }
  7651. RowBuffer_i * GetBuffer() final { return &m_tRowBuffer; }
  7652. private:
  7653. RowBuffer_i & m_tRowBuffer;
  7654. const char * m_szStmt;
  7655. };
  7656. struct StringPtrTraits_t
  7657. {
  7658. CSphVector<BYTE> m_dPackedData;
  7659. CSphFixedVector<int> m_dOff { 0 };
  7660. CSphVector<BYTE> m_dParserBuf;
  7661. // remap offsets to string pointers
  7662. void SavePointersTo ( VecTraits_T<const char *> &dStrings, bool bSkipInvalid=true ) const
  7663. {
  7664. if ( bSkipInvalid )
  7665. ARRAY_FOREACH ( i, m_dOff )
  7666. {
  7667. int iOff = m_dOff[i];
  7668. if ( iOff<0 )
  7669. continue;
  7670. dStrings[i] = ( const char * ) m_dPackedData.Begin () + iOff;
  7671. }
  7672. else
  7673. ARRAY_FOREACH ( i, m_dOff )
  7674. {
  7675. int iOff = m_dOff[i];
  7676. dStrings[i] = ( iOff>=0 ? ( const char * ) m_dPackedData.Begin () + iOff : nullptr );
  7677. }
  7678. }
  7679. void Reset ()
  7680. {
  7681. m_dPackedData.Resize ( 0 );
  7682. m_dParserBuf.Resize ( 0 );
  7683. m_dOff.Fill ( -1 );
  7684. }
  7685. BYTE * ReserveBlob ( int iBlobSize, int iOffset )
  7686. {
  7687. if ( !iBlobSize )
  7688. return nullptr;
  7689. m_dOff[iOffset] = m_dPackedData.GetLength ();
  7690. BYTE * pPacked = m_dPackedData.AddN ( sphCalcPackedLength(iBlobSize) );
  7691. pPacked += ZipToPtrBE ( pPacked, iBlobSize );
  7692. return pPacked;
  7693. }
  7694. };
  7695. static void BsonToSqlInsert ( const bson::Bson_c& dBson, SqlInsert_t& tAttr )
  7696. {
  7697. switch ( dBson.GetType () )
  7698. {
  7699. case JSON_INT32:
  7700. case JSON_INT64: tAttr.m_iType = SqlInsert_t::CONST_INT;
  7701. tAttr.m_iVal = dBson.Int ();
  7702. break;
  7703. case JSON_DOUBLE: tAttr.m_iType = SqlInsert_t::CONST_FLOAT;
  7704. tAttr.m_fVal = float ( dBson.Double () );
  7705. break;
  7706. case JSON_STRING: tAttr.m_iType = SqlInsert_t::QUOTED_STRING;
  7707. tAttr.m_sVal = dBson.String ();
  7708. default: break;
  7709. }
  7710. }
  7711. template<typename T>
  7712. static int CopyBsonValues ( CSphVector<int64_t> & dMva, const bson::Bson_c & dBson )
  7713. {
  7714. auto dValues = bson::Vector<T> ( dBson );
  7715. int64_t * pDst = dMva.AddN ( dValues.GetLength() );
  7716. ARRAY_FOREACH ( i, dValues )
  7717. pDst[i] = dValues[i];
  7718. return dValues.GetLength();
  7719. }
  7720. // save bson array to 64 bit mvaint64 mva
  7721. static int BsonArrayToMva ( CSphVector<int64_t> & dMva, const bson::Bson_c & dBson )
  7722. {
  7723. int iOff = dMva.GetLength ();
  7724. dMva.Add ();
  7725. int iValues = 0;
  7726. if ( dBson.GetType ()==JSON_INT64_VECTOR )
  7727. iValues = CopyBsonValues<int64_t> ( dMva, dBson );
  7728. else if ( dBson.GetType ()==JSON_INT32_VECTOR )
  7729. iValues = CopyBsonValues<DWORD> ( dMva, dBson );
  7730. else
  7731. { // slowest path - m.b. need conversion of every value
  7732. bson::BsonIterator_c dIter ( dBson );
  7733. iValues = dIter.NumElems();
  7734. int64_t * pDst = dMva.AddN(iValues);
  7735. for ( ; dIter; dIter.Next () )
  7736. *pDst++ = dIter.Int ();
  7737. }
  7738. if ( !iValues ) // empty mva; discard resize
  7739. {
  7740. dMva.Resize ( iOff );
  7741. return -1;
  7742. }
  7743. auto pDst = &dMva[iOff + 1];
  7744. sphSort ( pDst, iValues );
  7745. iValues = sphUniq ( pDst, iValues );
  7746. dMva[iOff] = iValues;
  7747. dMva.Resize ( iOff + iValues + 1 );
  7748. return iOff;
  7749. }
  7750. static bool ParseBsonDocument ( const VecTraits_T<BYTE> & dDoc, const SchemaItemHash_c & tLoc, const CSphString & sIdAlias, int iRow, VecTraits_T<VecTraits_T<const char>>& dFields, CSphMatch & tDoc,
  7751. StringPtrTraits_t & tStrings, CSphVector<int64_t> & dMva, const CSphSchema &tSchema, Warner_c & sMsg )
  7752. {
  7753. using namespace bson;
  7754. Bson_c dBson ( dDoc );
  7755. if ( dDoc.IsEmpty () )
  7756. return false;
  7757. SqlInsert_t tAttr;
  7758. const SchemaItemVariant_t * pId = sIdAlias.IsEmpty () ? nullptr : tLoc.Find ( sphFNV64 ( sIdAlias.cstr() ) );
  7759. const CSphColumnInfo * pIdCol = tSchema.GetAttr ( sphGetDocidName () );
  7760. assert(pIdCol);
  7761. CSphAttrLocator tIdLoc = pIdCol->m_tLocator;
  7762. tIdLoc.m_bDynamic = true;
  7763. BsonIterator_c dChild ( dBson );
  7764. for ( ; dChild; dChild.Next () )
  7765. {
  7766. CSphString sName = dChild.GetName ();
  7767. sphColumnToLowercase ( const_cast<char *>( sName.cstr() ) );
  7768. const SchemaItemVariant_t * pItem = tLoc.Find ( sphFNV64 ( sName.cstr() ) );
  7769. // FIXME!!! warn on unknown JSON fields
  7770. if ( pItem )
  7771. {
  7772. if ( pItem->m_iField!=-1 && dChild.IsString () )
  7773. {
  7774. // stripper prior to build hits does not preserve field length
  7775. // but works with \0 strings and could walk all document and modifies it and alter field length
  7776. const VecTraits_T<const char> tField = Vector<const char> ( dChild );
  7777. if ( tField.GetLength() )
  7778. {
  7779. int64_t iOff = tStrings.m_dPackedData.GetLength();
  7780. // copy field content with tail zeroes
  7781. BYTE * pDst = tStrings.m_dPackedData.AddN ( tField.GetLength() + 1 + CSphString::GetGap() );
  7782. memcpy ( pDst, tField.Begin(), tField.GetLength() );
  7783. memset ( pDst + tField.GetLength(), 0, 1 + CSphString::GetGap() );
  7784. // pack offset into pointer then restore pointer after m_dPackedData filed
  7785. dFields[pItem->m_iField] = VecTraits_T<const char> ( (const char *)iOff, tField.GetLength() );
  7786. } else
  7787. {
  7788. dFields[pItem->m_iField] = tField;
  7789. }
  7790. if ( pItem==pId )
  7791. sMsg.Warn ( "field '%s' requested as docs_id identifier, but it is field!", sName.cstr() );
  7792. } else
  7793. {
  7794. BsonToSqlInsert ( dChild, tAttr );
  7795. CSphMatchVariant::SetAttr ( tDoc, pItem->m_tLoc, tAttr, pItem->m_eType );
  7796. if ( pId==pItem )
  7797. tDoc.SetAttr ( tIdLoc, (DocID_t)dChild.Int() );
  7798. switch ( pItem->m_eType )
  7799. {
  7800. case SPH_ATTR_JSON:
  7801. assert ( pItem->m_iStr!=-1 );
  7802. {
  7803. if ( dChild.IsAssoc() || dChild.IsArray() )
  7804. {
  7805. // just save bson blob
  7806. BYTE * pDst = tStrings.ReserveBlob ( dChild.StandaloneSize(), pItem->m_iStr );
  7807. dChild.BsonToBson ( pDst );
  7808. } else
  7809. {
  7810. sMsg.Warn ( "JSON item (%s) should be object or array, got=%s", sName.cstr(), JsonTypeName ( dChild.GetType() ) );
  7811. }
  7812. }
  7813. break;
  7814. case SPH_ATTR_STRING:
  7815. assert ( pItem->m_iStr!=-1 );
  7816. {
  7817. auto dStrBlob = RawBlob ( dChild );
  7818. if ( dStrBlob.second )
  7819. {
  7820. tStrings.m_dOff[pItem->m_iStr] = tStrings.m_dPackedData.GetLength ();
  7821. BYTE * sDst = tStrings.m_dPackedData.AddN ( 1 + dStrBlob.second + CSphString::GetGap () );
  7822. memcpy ( sDst, dStrBlob.first, dStrBlob.second );
  7823. memset ( sDst + dStrBlob.second, 0, 1 + CSphString::GetGap () );
  7824. }
  7825. }
  7826. break;
  7827. case SPH_ATTR_UINT32SET:
  7828. case SPH_ATTR_INT64SET:
  7829. assert ( pItem->m_iMva!=-1 );
  7830. if ( dChild.IsArray() )
  7831. {
  7832. int iOff = BsonArrayToMva ( dMva, dChild );
  7833. if ( iOff>=0 )
  7834. dMva[pItem->m_iMva] = iOff;
  7835. } else
  7836. {
  7837. sMsg.Warn ( "MVA item (%s) should be array, got %s", sName.cstr(), JsonTypeName ( dChild.GetType() ) );
  7838. }
  7839. default:
  7840. break;
  7841. }
  7842. }
  7843. } else if ( !sIdAlias.IsEmpty() && sIdAlias==sName )
  7844. {
  7845. ((CSphMatch &)tDoc).SetAttr ( tIdLoc, (DocID_t)dChild.Int() );
  7846. }
  7847. }
  7848. return true;
  7849. }
  7850. static void FixParsedMva ( const CSphVector<int64_t> & dParsed, CSphVector<int64_t> & dMva, int iCount )
  7851. {
  7852. if ( !iCount )
  7853. return;
  7854. // dParsed:
  7855. // 0 - iCount elements: offset to MVA values with leading MVA element count
  7856. // Could be not in right order
  7857. dMva.Resize ( 0 );
  7858. for ( int i=0; i<iCount; ++i )
  7859. {
  7860. int iOff = dParsed[i];
  7861. if ( !iOff )
  7862. {
  7863. dMva.Add ( 0 );
  7864. continue;
  7865. }
  7866. DWORD uMvaCount = dParsed[iOff];
  7867. int64_t * pMva = dMva.AddN ( uMvaCount + 1 );
  7868. *pMva++ = uMvaCount;
  7869. memcpy ( pMva, dParsed.Begin() + iOff + 1, sizeof(dMva[0]) * uMvaCount );
  7870. }
  7871. }
  7872. class PqRequestBuilder_c : public RequestBuilder_i
  7873. {
  7874. const BlobVec_t &m_dDocs;
  7875. const PercolateOptions_t &m_tOpts;
  7876. mutable std::atomic<int> m_iWorker {0};
  7877. int m_iStart;
  7878. int m_iStep;
  7879. public:
  7880. explicit PqRequestBuilder_c ( const BlobVec_t &dDocs, const PercolateOptions_t &tOpts, int iStart=0, int iStep=0 )
  7881. : m_dDocs ( dDocs )
  7882. , m_tOpts ( tOpts )
  7883. , m_iStart ( iStart )
  7884. , m_iStep ( iStep)
  7885. {}
  7886. void BuildRequest ( const AgentConn_t &tAgent, ISphOutputBuffer &tOut ) const final
  7887. {
  7888. // it sends either all queries to each agent or sequence of queries to current agent
  7889. auto iWorker = tAgent.m_iStoreTag;
  7890. if ( iWorker<0 )
  7891. {
  7892. iWorker = m_iWorker.fetch_add ( 1, std::memory_order_relaxed );
  7893. tAgent.m_iStoreTag = iWorker;
  7894. }
  7895. const char * sIndex = tAgent.m_tDesc.m_sIndexes.cstr ();
  7896. auto tHdr = APIHeader ( tOut, SEARCHD_COMMAND_CALLPQ, VER_COMMAND_CALLPQ );
  7897. DWORD uFlags = 0;
  7898. if ( m_tOpts.m_bGetDocs )
  7899. uFlags = 1;
  7900. if ( m_tOpts.m_bGetQuery )
  7901. uFlags |= 2;
  7902. if ( m_tOpts.m_bJsonDocs )
  7903. uFlags |= 4;
  7904. if ( m_tOpts.m_bVerbose )
  7905. uFlags |= 8;
  7906. if ( m_tOpts.m_bSkipBadJson )
  7907. uFlags |= 16;
  7908. tOut.SendDword ( uFlags );
  7909. tOut.SendString ( m_tOpts.m_sIdAlias.cstr () );
  7910. tOut.SendString ( sIndex );
  7911. // send docs (all or chunk)
  7912. int iStart = 0;
  7913. int iStep = m_dDocs.GetLength();
  7914. if ( m_iStep ) // sparsed case, calculate the interval.
  7915. {
  7916. iStart = m_iStart + m_iStep * iWorker;
  7917. iStep = Min ( iStep - iStart, m_iStep );
  7918. }
  7919. tOut.SendInt ( iStart );
  7920. tOut.SendInt ( iStep );
  7921. for ( int i=iStart; i<iStart+iStep; ++i)
  7922. tOut.SendArray ( m_dDocs[i] );
  7923. }
  7924. };
  7925. class PqReplyParser_c : public ReplyParser_i
  7926. {
  7927. public:
  7928. bool ParseReply ( MemInputBuffer_c &tReq, AgentConn_t &tAgent ) const final
  7929. {
  7930. // auto &dQueries = m_pWorker->m_dQueries;
  7931. // int iDoc = m_pWorker->m_dTasks[tAgent.m_iStoreTag].m_iHead;
  7932. if ( !tAgent.m_pResult )
  7933. tAgent.m_pResult = std::make_unique<CPqResult>();
  7934. auto pResult = (CPqResult*)tAgent.m_pResult.get();
  7935. auto &dResult = pResult->m_dResult;
  7936. auto uFlags = tReq.GetDword ();
  7937. bool bDumpDocs = !!(uFlags & 1U);
  7938. bool bQuery = !!(uFlags & 2U);
  7939. bool bDeduplicatedDocs = !!(uFlags & 4U);
  7940. dResult.m_bGetDocs = bDumpDocs;
  7941. dResult.m_bGetQuery = bQuery;
  7942. CSphVector<int> dDocs;
  7943. CSphVector<DocID_t> dDocids;
  7944. dDocids.Add(0); // just to keep docids 1-based and so, simplify processing by avoid checks.
  7945. int iRows = tReq.GetInt ();
  7946. dResult.m_dQueryDesc.Reset ( iRows );
  7947. for ( auto &tDesc : dResult.m_dQueryDesc )
  7948. {
  7949. tDesc.m_iQUID = tReq.GetUint64 ();
  7950. if ( bDumpDocs )
  7951. {
  7952. int iCount = tReq.GetInt ();
  7953. dDocs.Add ( iCount );
  7954. if ( bDeduplicatedDocs )
  7955. {
  7956. for ( int iDoc = 0; iDoc<iCount; ++iDoc )
  7957. {
  7958. dDocs.Add ( dDocids.GetLength () );
  7959. dDocids.Add ( ( int64_t ) tReq.GetUint64 () );
  7960. }
  7961. } else
  7962. {
  7963. for ( int iDoc = 0; iDoc<iCount; ++iDoc )
  7964. dDocs.Add ( tReq.GetInt () );
  7965. }
  7966. }
  7967. if ( bQuery )
  7968. {
  7969. auto uDescFlags = tReq.GetDword ();
  7970. if ( uDescFlags & 1U )
  7971. tDesc.m_sQuery = tReq.GetString ();
  7972. if ( uDescFlags & 2U )
  7973. tDesc.m_sTags = tReq.GetString ();
  7974. if ( uDescFlags & 4U )
  7975. tDesc.m_sFilters = tReq.GetString ();
  7976. tDesc.m_bQL = !!(uDescFlags & 8U);
  7977. }
  7978. }
  7979. // meta
  7980. dResult.m_tmTotal = tReq.GetUint64 ();
  7981. dResult.m_tmSetup = tReq.GetUint64 ();
  7982. dResult.m_iQueriesMatched = tReq.GetInt();
  7983. dResult.m_iQueriesFailed = tReq.GetInt ();
  7984. dResult.m_iDocsMatched = tReq.GetInt ();
  7985. dResult.m_iTotalQueries = tReq.GetInt ();
  7986. dResult.m_iOnlyTerms = tReq.GetInt ();
  7987. dResult.m_iEarlyOutQueries = tReq.GetInt ();
  7988. auto iDts = tReq.GetInt();
  7989. dResult.m_dQueryDT.Reset ( iDts );
  7990. for ( int& iDt : dResult.m_dQueryDT )
  7991. iDt = tReq.GetInt();
  7992. dResult.m_sMessages.Warn ( tReq.GetString () );
  7993. auto iDocs = dDocs.GetLength ();
  7994. dResult.m_dDocs.Set ( dDocs.LeakData (), iDocs );
  7995. if ( dDocids.GetLength()>1 )
  7996. {
  7997. iDocs = dDocids.GetLength ();
  7998. pResult->m_dDocids.Set ( dDocids.LeakData (), iDocs );
  7999. }
  8000. return true;
  8001. }
  8002. };
  8003. static void SendAPIPercolateReply ( ISphOutputBuffer & tOut, const CPqResult & tResult, int iShift=0 )
  8004. {
  8005. auto tReply = APIAnswer ( tOut, VER_COMMAND_CALLPQ );
  8006. CSphVector<int64_t> dTmpDocs;
  8007. int iDocOff = -1;
  8008. const PercolateMatchResult_t &tRes = tResult.m_dResult;
  8009. const CSphFixedVector<DocID_t> &dDocids = tResult.m_dDocids;
  8010. bool bHasDocids = !dDocids.IsEmpty ();
  8011. bool bDumpDocs = tRes.m_bGetDocs;
  8012. bool bQuery = tRes.m_bGetQuery;
  8013. DWORD uFlags = 0;
  8014. if ( bDumpDocs )
  8015. uFlags = 1;
  8016. if ( bQuery )
  8017. uFlags |=2;
  8018. if ( bHasDocids )
  8019. uFlags |=4;
  8020. tOut.SendDword ( uFlags );
  8021. tOut.SendInt ( tRes.m_dQueryDesc.GetLength () );
  8022. for ( const auto &tDesc : tRes.m_dQueryDesc )
  8023. {
  8024. tOut.SendUint64 ( tDesc.m_iQUID );
  8025. if ( bDumpDocs )
  8026. {
  8027. // document count + document id(s)
  8028. auto iCount = ( int ) ( tRes.m_dDocs[++iDocOff] );
  8029. if ( bHasDocids ) // need de-duplicate docs
  8030. {
  8031. dTmpDocs.Resize ( iCount );
  8032. for ( int iDoc = 0; iDoc<iCount; ++iDoc )
  8033. {
  8034. int iRow = tRes.m_dDocs[++iDocOff];
  8035. dTmpDocs[iDoc] = dDocids[iRow];
  8036. }
  8037. dTmpDocs.Uniq ();
  8038. tOut.SendInt ( dTmpDocs.GetLength());
  8039. for ( auto dTmpDoc : dTmpDocs )
  8040. tOut.SendUint64 ( dTmpDoc );
  8041. } else
  8042. {
  8043. tOut.SendInt ( iCount );
  8044. for ( int iDoc = 0; iDoc<iCount; ++iDoc )
  8045. tOut.SendInt ( iShift+tRes.m_dDocs[++iDocOff] );
  8046. }
  8047. }
  8048. if ( bQuery )
  8049. {
  8050. DWORD uDescFlags = 0;
  8051. if ( !tDesc.m_sQuery.IsEmpty ())
  8052. uDescFlags |=1;
  8053. if ( !tDesc.m_sTags.IsEmpty () )
  8054. uDescFlags |= 2;
  8055. if ( !tDesc.m_sFilters.IsEmpty () )
  8056. uDescFlags |= 4;
  8057. if ( tDesc.m_bQL )
  8058. uDescFlags |= 8;
  8059. tOut.SendDword ( uDescFlags );
  8060. if ( uDescFlags & 1 )
  8061. tOut.SendString ( tDesc.m_sQuery.cstr () );
  8062. if ( uDescFlags & 2 )
  8063. tOut.SendString ( tDesc.m_sTags.cstr () );
  8064. if ( uDescFlags & 4 )
  8065. tOut.SendString ( tDesc.m_sFilters.cstr () );
  8066. }
  8067. }
  8068. // send meta
  8069. tOut.SendUint64 ( tRes.m_tmTotal );
  8070. tOut.SendUint64 ( tRes.m_tmSetup );
  8071. tOut.SendInt ( tRes.m_iQueriesMatched );
  8072. tOut.SendInt ( tRes.m_iQueriesFailed );
  8073. tOut.SendInt ( tRes.m_iDocsMatched );
  8074. tOut.SendInt ( tRes.m_iTotalQueries );
  8075. tOut.SendInt ( tRes.m_iOnlyTerms );
  8076. tOut.SendInt ( tRes.m_iEarlyOutQueries );
  8077. tOut.SendInt ( tRes.m_dQueryDT.GetLength () );
  8078. for ( int iDT : tRes.m_dQueryDT )
  8079. tOut.SendInt ( iDT );
  8080. tOut.SendString ( tRes.m_sMessages.sWarning () );
  8081. }
  8082. static void SendMysqlPercolateReply ( RowBuffer_i & tOut, const CPqResult & tResult, int iShift=0 )
  8083. {
  8084. // shortcuts
  8085. const PercolateMatchResult_t &tRes = tResult.m_dResult;
  8086. const CSphFixedVector<DocID_t> &dDocids = tResult.m_dDocids;
  8087. bool bDumpDocs = tRes.m_bGetDocs;
  8088. bool bQuery = tRes.m_bGetQuery;
  8089. // result set header packet. We will attach EOF manually at the end.
  8090. int iColumns = bDumpDocs ? 2 : 1;
  8091. if ( bQuery )
  8092. iColumns += 3;
  8093. tOut.HeadBegin ( iColumns );
  8094. tOut.HeadColumn ( "id", MYSQL_COL_LONGLONG );
  8095. if ( bDumpDocs )
  8096. tOut.HeadColumn ( "documents" );
  8097. if ( bQuery )
  8098. {
  8099. tOut.HeadColumn ( "query" );
  8100. tOut.HeadColumn ( "tags" );
  8101. tOut.HeadColumn ( "filters" );
  8102. }
  8103. // EOF packet is sent explicitly due to non-default params.
  8104. auto iWarns = tRes.m_sMessages.WarnEmpty () ? 0 : 1;
  8105. tOut.HeadEnd ( false, iWarns );
  8106. CSphVector<int64_t> dTmpDocs;
  8107. int iDocOff = -1;
  8108. StringBuilder_c sDocs;
  8109. for ( const auto &tDesc : tRes.m_dQueryDesc )
  8110. {
  8111. tOut.PutNumAsString ( tDesc.m_iQUID );
  8112. if ( bDumpDocs )
  8113. {
  8114. sDocs.StartBlock ( "," );
  8115. // document count + document id(s)
  8116. auto iCount = ( int ) ( tRes.m_dDocs[++iDocOff] );
  8117. if ( dDocids.GetLength () ) // need de-duplicate docs
  8118. {
  8119. dTmpDocs.Resize ( iCount );
  8120. for ( int iDoc = 0; iDoc<iCount; ++iDoc )
  8121. {
  8122. RowID_t tRow = tRes.m_dDocs[++iDocOff];
  8123. dTmpDocs[iDoc] = dDocids[tRow];
  8124. }
  8125. dTmpDocs.Uniq ();
  8126. for ( auto dTmpDoc : dTmpDocs )
  8127. sDocs.Sprintf ( "%l", dTmpDoc );
  8128. } else
  8129. {
  8130. for ( int iDoc = 0; iDoc<iCount; ++iDoc )
  8131. {
  8132. RowID_t tRow = tRes.m_dDocs[++iDocOff];
  8133. sDocs.Sprintf ( "%u", tRow + iShift );
  8134. }
  8135. }
  8136. tOut.PutString ( sDocs );
  8137. sDocs.Clear ();
  8138. }
  8139. if ( bQuery )
  8140. {
  8141. tOut.PutString ( tDesc.m_sQuery );
  8142. tOut.PutString ( tDesc.m_sTags );
  8143. tOut.PutString ( tDesc.m_sFilters );
  8144. }
  8145. tOut.Commit ();
  8146. }
  8147. tOut.Eof ( false, iWarns );
  8148. }
  8149. // process one(!) local(!) pq index
  8150. static void PQLocalMatch ( const BlobVec_t & dDocs, const CSphString & sIndex, const PercolateOptions_t & tOpt, CSphSessionAccum & tAcc, CPqResult & tResult, int iStart, int iDocs )
  8151. {
  8152. CSphString sWarning, sError;
  8153. auto &sMsg = tResult.m_dResult.m_sMessages;
  8154. tResult.m_dResult.m_bGetDocs = tOpt.m_bGetDocs;
  8155. tResult.m_dResult.m_bVerbose = tOpt.m_bVerbose;
  8156. tResult.m_dResult.m_bGetQuery = tOpt.m_bGetQuery;
  8157. sMsg.Clear ();
  8158. if ( !iDocs || ( iStart + iDocs )>dDocs.GetLength () )
  8159. iDocs = dDocs.GetLength () - iStart;
  8160. if ( !iDocs )
  8161. return sMsg.Warn ( "No more docs for sparse matching" );
  8162. auto pServed = GetServed ( sIndex );
  8163. if ( !pServed )
  8164. return sMsg.Err ( "unknown local index '%s' in search request", sIndex.cstr () );
  8165. if ( pServed->m_eType!=IndexType_e::PERCOLATE )
  8166. return sMsg.Err ( "index '%s' is not percolate", sIndex.cstr () );
  8167. RIdx_T<PercolateIndex_i*> pIndex { pServed };
  8168. RtAccum_t * pAccum = tAcc.GetAcc ( pIndex, sError );
  8169. sMsg.Err ( sError );
  8170. if ( !sMsg.ErrEmpty () )
  8171. return;
  8172. const CSphSchema & tSchema = pIndex->GetInternalSchema();
  8173. int iFieldsCount = tSchema.GetFieldsCount();
  8174. InsertDocData_t tDoc(tSchema);
  8175. // set defaults
  8176. int iAttrsCount = tSchema.GetAttrsCount ();
  8177. for ( int i = 0; i<iAttrsCount; ++i )
  8178. {
  8179. const CSphColumnInfo & tCol = tSchema.GetAttr(i);
  8180. CSphAttrLocator tLoc = tCol.m_tLocator;
  8181. tLoc.m_bDynamic = true;
  8182. CSphMatchVariant::SetDefaultAttr ( tDoc.m_tDoc, tLoc, tCol.m_eAttrType );
  8183. }
  8184. int iStrCounter = 0;
  8185. int iMvaCounter = 0;
  8186. SchemaItemHash_c hSchemaLocators;
  8187. if ( tOpt.m_bJsonDocs )
  8188. {
  8189. // hash attrs
  8190. for ( int i = 0; i<iAttrsCount; ++i )
  8191. {
  8192. const CSphColumnInfo &tCol = tSchema.GetAttr ( i );
  8193. SchemaItemVariant_t tAttr;
  8194. tAttr.m_tLoc = tCol.m_tLocator;
  8195. tAttr.m_tLoc.m_bDynamic = true; /// was just set above
  8196. tAttr.m_eType = tCol.m_eAttrType;
  8197. if ( tCol.m_eAttrType==SPH_ATTR_STRING || tCol.m_eAttrType==SPH_ATTR_JSON )
  8198. tAttr.m_iStr = iStrCounter++;
  8199. if ( tCol.m_eAttrType==SPH_ATTR_UINT32SET || tCol.m_eAttrType==SPH_ATTR_INT64SET )
  8200. tAttr.m_iMva = iMvaCounter++;
  8201. hSchemaLocators.Add ( sphFNV64 ( tCol.m_sName.cstr () ), tAttr );
  8202. }
  8203. for ( int i = 0; i<iFieldsCount; ++i )
  8204. {
  8205. const CSphColumnInfo &tField = tSchema.GetField ( i );
  8206. SchemaItemVariant_t tAttr;
  8207. tAttr.m_iField = i;
  8208. hSchemaLocators.Add ( sphFNV64 ( tField.m_sName.cstr () ), tAttr );
  8209. }
  8210. } else
  8211. {
  8212. // even without JSON docs MVA should match to schema definition on inserting data into accumulator
  8213. for ( int i = 0; i<iAttrsCount; ++i )
  8214. {
  8215. const CSphColumnInfo &tCol = tSchema.GetAttr ( i );
  8216. if ( tCol.m_eAttrType==SPH_ATTR_UINT32SET || tCol.m_eAttrType==SPH_ATTR_INT64SET )
  8217. ++iMvaCounter;
  8218. }
  8219. }
  8220. const CSphColumnInfo * pId = tSchema.GetAttr ( sphGetDocidName () );
  8221. assert( pId );
  8222. CSphAttrLocator tIdLoc = pId->m_tLocator;
  8223. tIdLoc.m_bDynamic = true;
  8224. int iDocsNoIdCount = 0;
  8225. bool bAutoId = tOpt.m_sIdAlias.IsEmpty ();
  8226. tResult.m_dDocids.Reset ( bAutoId ? 0 : iDocs + 1 );
  8227. int64_t uSeqDocid = 1;
  8228. tDoc.m_dStrings.Resize(iStrCounter);
  8229. StringPtrTraits_t tStrings;
  8230. tStrings.m_dOff.Reset ( iStrCounter );
  8231. CSphVector<int64_t> dMvaParsed ( iMvaCounter );
  8232. CSphString sTokenFilterOpts;
  8233. RowID_t tRowID = 0;
  8234. for ( auto iDoc = iStart; iDoc<iStart+iDocs; ++iDoc )
  8235. {
  8236. // doc-id
  8237. tDoc.m_tDoc.SetAttr ( tIdLoc, 0 );
  8238. tDoc.m_dFields[0] = dDocs[iDoc];
  8239. dMvaParsed.Resize ( iMvaCounter );
  8240. dMvaParsed.Fill ( 0 );
  8241. if ( tOpt.m_bJsonDocs )
  8242. {
  8243. // reset all back to defaults
  8244. tDoc.m_dFields.Fill ( { nullptr, 0 } );
  8245. for ( int i = 0; i<iAttrsCount; ++i )
  8246. {
  8247. const CSphColumnInfo &tCol = tSchema.GetAttr ( i );
  8248. CSphAttrLocator tLoc = tCol.m_tLocator;
  8249. tLoc.m_bDynamic = true;
  8250. CSphMatchVariant::SetDefaultAttr ( tDoc.m_tDoc, tLoc, tCol.m_eAttrType );
  8251. }
  8252. tStrings.Reset();
  8253. if ( !ParseBsonDocument ( dDocs[iDoc], hSchemaLocators, tOpt.m_sIdAlias, iDoc, tDoc.m_dFields, tDoc.m_tDoc, tStrings, dMvaParsed, tSchema, sMsg ) )
  8254. {
  8255. // for now the only case of fail - if provided bson is empty (null) document.
  8256. if ( tOpt.m_bSkipBadJson )
  8257. {
  8258. sMsg.Warn ( "ERROR: Document %d is empty", iDoc + tOpt.m_iShift + 1 );
  8259. continue;
  8260. }
  8261. sMsg.Err ( "Document %d is empty", iDoc + tOpt.m_iShift + 1 );
  8262. break;
  8263. }
  8264. tStrings.SavePointersTo ( tDoc.m_dStrings, false );
  8265. // convert back offset into tStrings buffer into pointers
  8266. for ( VecTraits_T<const char> & tField : tDoc.m_dFields )
  8267. {
  8268. if ( !tField.GetLength() )
  8269. continue;
  8270. int64_t iOff = int64_t( tField.Begin() );
  8271. int iLen = tField.GetLength();
  8272. tField = VecTraits_T<const char> ( (const char *)( tStrings.m_dPackedData.Begin()+iOff ), iLen );
  8273. }
  8274. }
  8275. FixParsedMva ( dMvaParsed, tDoc.m_dMvas, iMvaCounter );
  8276. if ( !sMsg.ErrEmpty () )
  8277. break;
  8278. tDoc.m_tDoc.m_tRowID = ( RowID_t ) tRowID++;
  8279. if ( !bAutoId )
  8280. {
  8281. // in user-provides-id mode let's skip all docs without id
  8282. if ( !sphGetDocID ( tDoc.m_tDoc.m_pDynamic ) )
  8283. {
  8284. ++iDocsNoIdCount;
  8285. continue;
  8286. }
  8287. // store provided doc-id for result set sending
  8288. tResult.m_dDocids[uSeqDocid] = ( int64_t ) sphGetDocID ( tDoc.m_tDoc.m_pDynamic );
  8289. tDoc.m_tDoc.SetAttr ( tIdLoc, uSeqDocid++ );
  8290. } else
  8291. tDoc.m_tDoc.SetAttr ( tIdLoc, iDoc + 1 ); // +1 since docid is 1-based
  8292. // PQ work with sequential document numbers, 0 element unused
  8293. // add document
  8294. pIndex->AddDocument ( tDoc, true, sTokenFilterOpts, sError, sWarning, pAccum );
  8295. sMsg.Err ( sError );
  8296. sMsg.Warn ( sWarning );
  8297. if ( !sMsg.ErrEmpty() )
  8298. break;
  8299. }
  8300. // fire exit
  8301. if ( !sMsg.ErrEmpty() )
  8302. {
  8303. pIndex->RollBack ( pAccum ); // clean up collected data
  8304. return;
  8305. }
  8306. pIndex->MatchDocuments ( pAccum, tResult.m_dResult );
  8307. if ( iDocsNoIdCount )
  8308. sMsg.Warn ( "skipped %d document(s) without id field '%s'", iDocsNoIdCount, tOpt.m_sIdAlias.cstr() );
  8309. }
  8310. void PercolateMatchDocuments ( const BlobVec_t & dDocs, const PercolateOptions_t & tOpts, CSphSessionAccum & tAcc, CPqResult & tResult )
  8311. {
  8312. CSphString sIndex = tOpts.m_sIndex;
  8313. CSphString sWarning, sError;
  8314. StrVec_t dLocalIndexes;
  8315. const auto * pLocalIndexes = &dLocalIndexes;
  8316. VecRefPtrsAgentConn_t dAgents;
  8317. auto pDist = GetDistr ( sIndex );
  8318. if ( pDist )
  8319. {
  8320. for ( const auto& pAgent : pDist->m_dAgents )
  8321. {
  8322. auto * pConn = new AgentConn_t;
  8323. pConn->SetMultiAgent ( pAgent );
  8324. pConn->m_iMyConnectTimeoutMs = pDist->m_iAgentConnectTimeoutMs;
  8325. pConn->m_iMyQueryTimeoutMs = pDist->m_iAgentQueryTimeoutMs;
  8326. dAgents.Add ( pConn );
  8327. }
  8328. pLocalIndexes = &pDist->m_dLocal;
  8329. } else
  8330. dLocalIndexes.Add ( sIndex );
  8331. // at this point we know total num of involved indexes,
  8332. // and can eventually split (sparse) docs among them.
  8333. int iChunks = 0;
  8334. if ( tOpts.m_eMode==PercolateOptions_t::unknown || tOpts.m_eMode==PercolateOptions_t::sparsed)
  8335. iChunks = dAgents.GetLength () + pLocalIndexes->GetLength ();
  8336. int iStart = 0;
  8337. int iStep = iChunks>1 ? ( ( dDocs.GetLength () - 1 ) / iChunks + 1 ) : 0;
  8338. bool bHaveRemotes = !dAgents.IsEmpty ();
  8339. int iSuccesses = 0;
  8340. int iAgentsDone = 0;
  8341. std::unique_ptr<PqRequestBuilder_c> pReqBuilder;
  8342. std::unique_ptr<ReplyParser_i> pParser;
  8343. CSphRefcountedPtr<RemoteAgentsObserver_i> pReporter { nullptr };
  8344. if ( bHaveRemotes )
  8345. {
  8346. pReqBuilder = std::make_unique<PqRequestBuilder_c> ( dDocs, tOpts, iStart, iStep );
  8347. iStart += iStep * dAgents.GetLength ();
  8348. pParser = std::make_unique<PqReplyParser_c>();
  8349. pReporter = GetObserver();
  8350. ScheduleDistrJobs ( dAgents, pReqBuilder.get(), pParser.get(), pReporter );
  8351. }
  8352. LazyVector_T <CPqResult> dLocalResults;
  8353. for ( const auto & sPqIndex : *pLocalIndexes )
  8354. {
  8355. auto & dResult = dLocalResults.Add();
  8356. PQLocalMatch ( dDocs, sPqIndex, tOpts, tAcc, dResult, iStart, iStep );
  8357. iStart += iStep;
  8358. }
  8359. if ( bHaveRemotes )
  8360. {
  8361. assert ( pReporter );
  8362. pReporter->Finish ();
  8363. iSuccesses = ( int ) pReporter->GetSucceeded ();
  8364. iAgentsDone = ( int ) pReporter->GetFinished ();
  8365. }
  8366. LazyVector_T<CPqResult*> dAllResults;
  8367. for ( auto & dLocalRes : dLocalResults )
  8368. dAllResults.Add ( &dLocalRes );
  8369. CPqResult dMsgs; // fake resultset just to grab errors from remotes
  8370. if ( iAgentsDone>iSuccesses )
  8371. dAllResults.Add ( &dMsgs );
  8372. if ( iAgentsDone )
  8373. {
  8374. for ( auto * pAgent : dAgents )
  8375. {
  8376. if ( !pAgent->m_bSuccess )
  8377. {
  8378. dMsgs.m_dResult.m_sMessages.Err ( pAgent->m_sFailure );
  8379. continue;
  8380. }
  8381. auto pResult = ( CPqResult * ) pAgent->m_pResult.get ();
  8382. if ( !pResult )
  8383. continue;
  8384. dAllResults.Add ( pResult );
  8385. }
  8386. }
  8387. MergePqResults ( dAllResults, tResult, iChunks<2 );
  8388. if ( iSuccesses!=iAgentsDone )
  8389. {
  8390. sphWarning ( "Remote PQ: some of the agents didn't answered: %d queried, %d finished, %d succeeded"
  8391. , dAgents.GetLength (), iAgentsDone, iSuccesses );
  8392. }
  8393. }
  8394. /// call PQ command over API
  8395. void HandleCommandCallPq ( ISphOutputBuffer &tOut, WORD uVer, InputBuffer_c &tReq ) REQUIRES ( HandlerThread )
  8396. {
  8397. if ( !CheckCommandVersion ( uVer, VER_COMMAND_CALLPQ, tOut ) )
  8398. return;
  8399. // options
  8400. PercolateOptions_t tOpts;
  8401. DWORD uFlags = tReq.GetDword ();
  8402. tOpts.m_bGetDocs = !!(uFlags & 1);
  8403. tOpts.m_bGetQuery = !!(uFlags & 2);
  8404. tOpts.m_bJsonDocs = !!(uFlags & 4);
  8405. tOpts.m_bVerbose = !!(uFlags & 8);
  8406. tOpts.m_bSkipBadJson = !! ( uFlags & 16 );
  8407. tOpts.m_sIdAlias = tReq.GetString();
  8408. // index name
  8409. tOpts.m_sIndex = tReq.GetString();
  8410. // document(s)
  8411. tOpts.m_iShift = tReq.GetInt();
  8412. BlobVec_t dDocs ( tReq.GetInt() );
  8413. for ( auto & sDoc : dDocs )
  8414. if ( !tReq.GetString ( sDoc ) )
  8415. {
  8416. SendErrorReply ( tOut, "Can't retrieve doc from input buffer" );
  8417. return;
  8418. }
  8419. // working
  8420. CSphSessionAccum tAcc;
  8421. CPqResult tResult;
  8422. PercolateMatchDocuments ( dDocs, tOpts, tAcc, tResult );
  8423. if ( tResult.m_dResult.m_iQueriesFailed )
  8424. tResult.m_dResult.m_sMessages.Err ( "%d queries failed", tResult.m_dResult.m_iQueriesFailed );
  8425. if ( !tResult.m_dResult.m_sMessages.ErrEmpty () )
  8426. {
  8427. SendErrorReply ( tOut, "%s", tResult.m_dResult.m_sMessages.sError() );
  8428. return;
  8429. }
  8430. SendAPIPercolateReply ( tOut, tResult, tOpts.m_iShift );
  8431. }
  8432. static void HandleMysqlCallPQ ( RowBuffer_i & tOut, SqlStmt_t & tStmt, CSphSessionAccum & tAcc, CPqResult & tResult )
  8433. {
  8434. StatCountCommand ( SEARCHD_COMMAND_CALLPQ );
  8435. PercolateMatchResult_t &tRes = tResult.m_dResult;
  8436. tRes.Reset();
  8437. // check arguments
  8438. // index name, document | documents list, [named opts]
  8439. if ( tStmt.m_dInsertValues.GetLength()!=2 )
  8440. {
  8441. tOut.Error ( tStmt.m_sStmt, "PQ() expects exactly 2 arguments (index, document(s))" );
  8442. return;
  8443. }
  8444. auto &dStmtIndex = tStmt.m_dInsertValues[0];
  8445. auto &dStmtDocs = tStmt.m_dInsertValues[1];
  8446. if ( dStmtIndex.m_iType!=SqlInsert_t::QUOTED_STRING )
  8447. {
  8448. tOut.Error ( tStmt.m_sStmt, "PQ() argument 1 must be a string" );
  8449. return;
  8450. }
  8451. if ( dStmtDocs.m_iType!=SqlInsert_t::QUOTED_STRING && dStmtDocs.m_iType!=SqlInsert_t::CONST_STRINGS )
  8452. {
  8453. tOut.Error ( tStmt.m_sStmt, "PQ() argument 2 must be a string or a string list" );
  8454. return;
  8455. }
  8456. // document(s)
  8457. StrVec_t dDocs;
  8458. if ( dStmtDocs.m_iType==SqlInsert_t::QUOTED_STRING )
  8459. dDocs.Add ( dStmtDocs.m_sVal );
  8460. else
  8461. dDocs.SwapData ( tStmt.m_dCallStrings );
  8462. // options last
  8463. CSphString sError;
  8464. PercolateOptions_t tOpts;
  8465. tOpts.m_sIndex = dStmtIndex.m_sVal;
  8466. SqlParser_SplitClusterIndex ( tOpts.m_sIndex, nullptr );
  8467. bool bSkipEmpty = false;
  8468. ARRAY_FOREACH ( i, tStmt.m_dCallOptNames )
  8469. {
  8470. CSphString & sOpt = tStmt.m_dCallOptNames[i];
  8471. const SqlInsert_t & v = tStmt.m_dCallOptValues[i];
  8472. sOpt.ToLower();
  8473. int iExpType = SqlInsert_t::CONST_INT;
  8474. if ( sOpt=="docs_id" )
  8475. {
  8476. tOpts.m_sIdAlias = v.m_sVal;
  8477. iExpType = SqlInsert_t::QUOTED_STRING;
  8478. sphColumnToLowercase ( const_cast<char *>( tOpts.m_sIdAlias.cstr() ) );
  8479. } else if ( sOpt=="docs" ) tOpts.m_bGetDocs = ( v.m_iVal!=0 );
  8480. else if ( sOpt=="verbose" ) tOpts.m_bVerbose = ( v.m_iVal!=0 );
  8481. else if ( sOpt=="docs_json" ) tOpts.m_bJsonDocs = ( v.m_iVal!=0 );
  8482. else if ( sOpt=="query" ) tOpts.m_bGetQuery = ( v.m_iVal!=0 );
  8483. else if ( sOpt=="skip_bad_json" ) tOpts.m_bSkipBadJson = ( v.m_iVal!=0 );
  8484. else if ( sOpt=="skip_empty" ) bSkipEmpty = true;
  8485. else if ( sOpt=="shift" ) tOpts.m_iShift = v.m_iVal;
  8486. else if ( sOpt=="mode" )
  8487. {
  8488. auto sMode = v.m_sVal;
  8489. iExpType = SqlInsert_t::QUOTED_STRING;
  8490. sMode.ToLower();
  8491. if ( sMode=="sparsed" )
  8492. tOpts.m_eMode = PercolateOptions_t::sparsed;
  8493. else if ( sMode=="sharded" )
  8494. tOpts.m_eMode = PercolateOptions_t::sharded;
  8495. else
  8496. {
  8497. sError.SetSprintf ( "unknown mode %s. (Expected 'sparsed' or 'sharded')", v.m_sVal.cstr () );
  8498. break;
  8499. }
  8500. } else
  8501. {
  8502. sError.SetSprintf ( "unknown option %s", sOpt.cstr() );
  8503. break;
  8504. }
  8505. // post-conf type check
  8506. if ( iExpType!=v.m_iType )
  8507. {
  8508. sError.SetSprintf ( "unexpected option %s type", sOpt.cstr() );
  8509. break;
  8510. }
  8511. }
  8512. if ( tOpts.m_bSkipBadJson && !tOpts.m_bJsonDocs ) // fixme! do we need such warn? Uncomment, if so.
  8513. tRes.m_sMessages.Warn ( "option to skip bad json has no sense since docs are not in json form" );
  8514. if ( !sError.IsEmpty() )
  8515. {
  8516. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  8517. return;
  8518. }
  8519. BlobVec_t dBlobDocs;
  8520. dBlobDocs.Reserve ( dDocs.GetLength() ); // actually some docs may be complex
  8521. CSphVector<int> dBadDocs;
  8522. if ( !tOpts.m_bJsonDocs )
  8523. for ( auto &dDoc : dDocs )
  8524. dDoc.LeakToVec ( dBlobDocs.Add () );
  8525. else
  8526. ARRAY_FOREACH ( i, dDocs )
  8527. {
  8528. using namespace bson;
  8529. CSphVector<BYTE> dData;
  8530. if ( !sphJsonParse ( dData, (char *)dDocs[i].cstr(), g_bJsonAutoconvNumbers, g_bJsonKeynamesToLowercase, false, sError ) )
  8531. {
  8532. dBadDocs.Add ( i + 1 );
  8533. continue;
  8534. }
  8535. Bson_c dBson ( dData );
  8536. if ( dBson.IsArray () )
  8537. {
  8538. for ( BsonIterator_c dItem ( dBson ); dItem; dItem.Next() )
  8539. {
  8540. if ( dItem.IsAssoc () )
  8541. dItem.BsonToBson ( dBlobDocs.Add () );
  8542. else
  8543. {
  8544. dBadDocs.Add ( i + 1 ); // fixme! m.b. report it as 'wrong doc N in string M'?
  8545. break;
  8546. }
  8547. }
  8548. }
  8549. else if ( dBson.IsAssoc() )
  8550. {
  8551. dData.SwapData ( dBlobDocs.Add () );
  8552. }
  8553. else if ( bSkipEmpty && dBson.IsEmpty() )
  8554. continue;
  8555. else
  8556. dBadDocs.Add ( i + 1 ); // let it be just 'an error' for now
  8557. if ( !dBadDocs.IsEmpty() && !tOpts.m_bSkipBadJson )
  8558. break;
  8559. }
  8560. if ( !dBadDocs.IsEmpty() )
  8561. {
  8562. StringBuilder_c sBad ( ",", "Bad JSON objects in strings: " );
  8563. for ( int iBadDoc:dBadDocs )
  8564. sBad.Sprintf ( "%d", iBadDoc );
  8565. if ( !tOpts.m_bSkipBadJson )
  8566. {
  8567. tOut.Error ( tStmt.m_sStmt, sBad.cstr ());
  8568. return;
  8569. }
  8570. tRes.m_sMessages.Warn ( sBad.cstr () );
  8571. }
  8572. tResult.m_dDocids.Reset ( tOpts.m_sIdAlias.IsEmpty () ? 0 : dBlobDocs.GetLength () + 1 );
  8573. if ( tOpts.m_iShift && !tOpts.m_sIdAlias.IsEmpty () )
  8574. tRes.m_sMessages.Warn ( "'shift' option works only for automatic ids, when 'docs_id' is not defined" );
  8575. PercolateMatchDocuments ( dBlobDocs, tOpts, tAcc, tResult );
  8576. if ( !tRes.m_sMessages.ErrEmpty () )
  8577. {
  8578. tRes.m_sMessages.MoveAllTo ( sError );
  8579. tOut.Error ( tStmt.m_sStmt, sError.cstr () );
  8580. return;
  8581. }
  8582. SendMysqlPercolateReply ( tOut, tResult, tOpts.m_iShift );
  8583. }
  8584. void HandleMysqlPercolateMeta ( const CPqResult &tResult, const CSphString & sWarning, RowBuffer_i & tOut )
  8585. {
  8586. // shortcuts
  8587. const PercolateMatchResult_t &tMeta = tResult.m_dResult;
  8588. tOut.HeadTuplet ( "Name", "Value" );
  8589. tOut.DataTupletf ( "Total", "%.3D sec", tMeta.m_tmTotal / 1000 );
  8590. if ( tMeta.m_tmSetup && tMeta.m_tmSetup>0 )
  8591. tOut.DataTupletf ( "Setup", "%.3D sec", tMeta.m_tmSetup / 1000 );
  8592. tOut.DataTuplet ( "Queries matched", tMeta.m_iQueriesMatched );
  8593. tOut.DataTuplet ( "Queries failed", tMeta.m_iQueriesFailed );
  8594. tOut.DataTuplet ( "Document matched", tMeta.m_iDocsMatched );
  8595. tOut.DataTuplet ( "Total queries stored", tMeta.m_iTotalQueries );
  8596. tOut.DataTuplet ( "Term only queries", tMeta.m_iOnlyTerms );
  8597. tOut.DataTuplet ( "Fast rejected queries", tMeta.m_iEarlyOutQueries );
  8598. if ( !tMeta.m_dQueryDT.IsEmpty() )
  8599. {
  8600. uint64_t tmMatched = 0;
  8601. StringBuilder_c sList (", ");
  8602. assert ( tMeta.m_iQueriesMatched==tMeta.m_dQueryDT.GetLength() );
  8603. for ( int tmQuery : tMeta.m_dQueryDT )
  8604. {
  8605. sList.Sprintf ( "%d", tmQuery );
  8606. tmMatched += tmQuery;
  8607. }
  8608. tOut.DataTuplet ( "Time per query", sList.cstr() );
  8609. tOut.DataTuplet ( "Time of matched queries", tmMatched );
  8610. }
  8611. if ( !sWarning.IsEmpty() )
  8612. tOut.DataTuplet ( "Warning", sWarning.cstr() );
  8613. tOut.Eof();
  8614. }
  8615. static bool IsHttpStmt ( const SqlStmt_t & tStmt )
  8616. {
  8617. return !tStmt.m_sEndpoint.IsEmpty();
  8618. }
  8619. static void PopulateMapsFromIndexSchema ( CSphVector<int> & dAttrSchema, CSphVector<int> & dFieldSchema, const CSphSchema & tSchema )
  8620. {
  8621. assert ( tSchema.GetAttr(0).m_sName==sphGetDocidName() );
  8622. ARRAY_FOREACH ( i, dFieldSchema )
  8623. dFieldSchema[i] = i+1;
  8624. dAttrSchema[0]=0;
  8625. int iAttrId = dFieldSchema.GetLength()+1;
  8626. for ( int i = 1; i < dAttrSchema.GetLength(); i++ )
  8627. {
  8628. if ( sphIsInternalAttr( tSchema.GetAttr(i) ) )
  8629. dAttrSchema[i]=-1;
  8630. else
  8631. dAttrSchema[i] = iAttrId++;
  8632. }
  8633. }
  8634. static bool CreateAttrMaps ( CSphVector<int> & dAttrSchema, CSphVector<int> & dFieldSchema, CSphVector<bool> & dFieldAttrs, const CSphSchema & tSchema, const StrVec_t & dStmtInsertSchema, StmtErrorReporter_i & tOut )
  8635. {
  8636. ARRAY_FOREACH ( i, dFieldAttrs )
  8637. dFieldAttrs[i] = false;
  8638. if ( !dStmtInsertSchema.GetLength() )
  8639. {
  8640. PopulateMapsFromIndexSchema ( dAttrSchema, dFieldSchema, tSchema );
  8641. return true;
  8642. }
  8643. // got a list of columns, check for 1) existance, 2) dupes
  8644. StrVec_t dCheck = dStmtInsertSchema;
  8645. ARRAY_FOREACH ( i, dCheck )
  8646. // OPTIMIZE! GetFieldIndex use linear searching. M.b. hash instead?
  8647. if ( tSchema.GetAttrIndex ( dCheck[i].cstr() )==-1 && tSchema.GetFieldIndex ( dCheck[i].cstr() )==-1 )
  8648. {
  8649. tOut.Error ( "unknown column: '%s'", dCheck[i].cstr() );
  8650. return false;
  8651. }
  8652. dCheck.Sort();
  8653. for ( int i=1; i<dCheck.GetLength(); i++ )
  8654. if ( dCheck[i-1]==dCheck[i] )
  8655. {
  8656. CSphString sError;
  8657. sError.SetSprintf ( "column '%s' specified twice", dCheck[i].cstr() );
  8658. tOut.ErrorEx ( MYSQL_ERR_FIELD_SPECIFIED_TWICE, sError.cstr() );
  8659. return false;
  8660. }
  8661. // hash column list
  8662. // OPTIMIZE! hash index columns once (!) instead
  8663. SmallStringHash_T<int> dInsertSchema;
  8664. ARRAY_FOREACH ( i, dStmtInsertSchema )
  8665. dInsertSchema.Add ( i, dStmtInsertSchema[i] );
  8666. // map fields
  8667. ARRAY_FOREACH ( i, dFieldSchema )
  8668. {
  8669. const char * szFieldName = tSchema.GetFieldName(i);
  8670. if ( dInsertSchema.Exists(szFieldName) )
  8671. {
  8672. dFieldSchema[i] = dInsertSchema[szFieldName];
  8673. // does an attribute with the same name exist?
  8674. if ( tSchema.GetAttr(szFieldName) )
  8675. dFieldAttrs[i] = true;
  8676. } else
  8677. dFieldSchema[i] = -1;
  8678. }
  8679. // map attrs
  8680. ARRAY_FOREACH ( j, dAttrSchema )
  8681. {
  8682. const char * szAttrName = tSchema.GetAttr(j).m_sName.cstr();
  8683. if ( dInsertSchema.Exists(szAttrName) )
  8684. dAttrSchema[j] = dInsertSchema[szAttrName];
  8685. else
  8686. dAttrSchema[j] = -1;
  8687. }
  8688. return true;
  8689. }
  8690. /////////////////////////////////////////////////////////////////////
  8691. class AttributeConverter_c : public InsertDocData_t
  8692. {
  8693. public:
  8694. AttributeConverter_c ( const CSphSchema & tSchema, const CSphVector<bool> & dFieldAttrs, CSphString & sError, CSphString & sWarning );
  8695. bool SetAttrValue ( int iCol, const SqlInsert_t & tVal, int iRow, int iQuerySchemaIdx );
  8696. void SetDefaultAttrValue ( int iCol );
  8697. bool SetFieldValue ( int iField, const SqlInsert_t & tVal, int iRow, int iQuerySchemaIdx );
  8698. void SetDefaultFieldValue ( int iField );
  8699. void NewRow();
  8700. void Finalize();
  8701. private:
  8702. const CSphSchema & m_tSchema;
  8703. const CSphColumnInfo * m_pDocId = nullptr;
  8704. const CSphVector<bool> & m_dFieldAttrs;
  8705. StringPtrTraits_t m_tStrings;
  8706. StrVec_t m_dTmpFieldStorage;
  8707. CSphVector<int> m_dColumnarRemap;
  8708. CSphString & m_sError;
  8709. CSphString & m_sWarning;
  8710. bool String2JsonPack ( char * pStr, CSphVector<BYTE> & dBuf );
  8711. bool CheckDocId ( const CSphColumnInfo & tCol, SphAttr_t tAttr );
  8712. bool CheckStrings ( const CSphColumnInfo & tCol, const SqlInsert_t & tVal, int iCol, int iRow );
  8713. bool CheckJson ( const CSphColumnInfo & tCol, const SqlInsert_t & tVal );
  8714. bool CheckInsertTypes ( const CSphColumnInfo & tCol, const SqlInsert_t & tVal, int iRow, int iQuerySchemaIdx );
  8715. };
  8716. AttributeConverter_c::AttributeConverter_c ( const CSphSchema & tSchema, const CSphVector<bool> & dFieldAttrs, CSphString & sError, CSphString & sWarning )
  8717. : InsertDocData_t ( tSchema )
  8718. , m_tSchema ( tSchema )
  8719. , m_pDocId ( tSchema.GetAttr ( sphGetDocidName() ) )
  8720. , m_dFieldAttrs ( dFieldAttrs )
  8721. , m_sError ( sError )
  8722. , m_sWarning ( sWarning )
  8723. {
  8724. int iAttrs = tSchema.GetAttrsCount();
  8725. m_dTmpFieldStorage.Resize ( tSchema.GetFieldsCount() );
  8726. m_dColumnarRemap.Resize(iAttrs);
  8727. int iColumnarAttr = 0;
  8728. for ( int i = 0; i < iAttrs; i++ )
  8729. if ( m_tSchema.GetAttr(i).IsColumnar() )
  8730. m_dColumnarRemap[i] = iColumnarAttr++;
  8731. else
  8732. m_dColumnarRemap[i] = -1;
  8733. m_dColumnarAttrs.Resize(iColumnarAttr);
  8734. m_tStrings.m_dOff.Reset(iAttrs);
  8735. }
  8736. bool AttributeConverter_c::String2JsonPack ( char * pStr, CSphVector<BYTE> & dBuf )
  8737. {
  8738. dBuf.Resize ( 0 ); // buffer for JSON parser must be empty to properly set JSON_ROOT data
  8739. if ( !pStr )
  8740. return true;
  8741. if ( !sphJsonParse ( dBuf, pStr, g_bJsonAutoconvNumbers, g_bJsonKeynamesToLowercase, true, m_sError ) )
  8742. {
  8743. if ( g_bJsonStrict )
  8744. return false;
  8745. if ( m_sWarning.IsEmpty() )
  8746. m_sWarning = m_sError;
  8747. else
  8748. m_sWarning.SetSprintf ( "%s; %s", m_sWarning.cstr(), m_sError.cstr() );
  8749. m_sError = "";
  8750. }
  8751. return true;
  8752. }
  8753. bool AttributeConverter_c::CheckDocId ( const CSphColumnInfo & tCol, SphAttr_t tAttr )
  8754. {
  8755. if ( &tCol==m_pDocId && tAttr<0 )
  8756. {
  8757. m_sError.SetSprintf ( "'id' column is " INT64_FMT ". Must be positive.", tAttr );
  8758. return false;
  8759. }
  8760. return true;
  8761. }
  8762. bool AttributeConverter_c::CheckStrings ( const CSphColumnInfo & tCol, const SqlInsert_t & tVal, int iCol, int iRow )
  8763. {
  8764. if ( tCol.m_eAttrType!=SPH_ATTR_STRING && tCol.m_eAttrType!=SPH_ATTR_STRINGPTR )
  8765. return true;
  8766. if ( tVal.m_sVal.Length() > 0x3FFFFF )
  8767. {
  8768. *( char * ) ( tVal.m_sVal.cstr () + 0x3FFFFF ) = '\0';
  8769. m_sWarning.SetSprintf ( "String column %d at row %d too long, truncated to 4MB", iCol, iRow );
  8770. }
  8771. m_dStrings.Add ( tVal.m_sVal.cstr() );
  8772. return true;
  8773. }
  8774. bool AttributeConverter_c::CheckJson ( const CSphColumnInfo & tCol, const SqlInsert_t & tVal )
  8775. {
  8776. if ( tCol.m_eAttrType!=SPH_ATTR_JSON )
  8777. return true;
  8778. int iStrCount = m_dStrings.GetLength();
  8779. m_dStrings.Add ( nullptr );
  8780. // empty source string means NULL attribute
  8781. if ( tVal.m_sVal.IsEmpty() )
  8782. return true;
  8783. // sphJsonParse must be terminated with a double zero however usual CSphString have SAFETY_GAP of 4 zeros
  8784. if ( !String2JsonPack ( (char *)tVal.m_sVal.cstr(), m_tStrings.m_dParserBuf ) )
  8785. return false;
  8786. int iParsedLength = m_tStrings.m_dParserBuf.GetLength();
  8787. if ( iParsedLength )
  8788. {
  8789. m_tStrings.m_dOff[iStrCount] = m_tStrings.m_dPackedData.GetLength();
  8790. BYTE * pPacked = m_tStrings.m_dPackedData.AddN ( sphCalcPackedLength ( iParsedLength ) );
  8791. sphPackPtrAttr ( pPacked, m_tStrings.m_dParserBuf );
  8792. }
  8793. return true;
  8794. }
  8795. bool AttributeConverter_c::CheckInsertTypes ( const CSphColumnInfo & tCol, const SqlInsert_t & tVal, int iRow, int iQuerySchemaIdx )
  8796. {
  8797. if ( tVal.m_iType!=SqlInsert_t::QUOTED_STRING
  8798. && tVal.m_iType!=SqlInsert_t::CONST_INT
  8799. && tVal.m_iType!=SqlInsert_t::CONST_FLOAT
  8800. && tVal.m_iType!=SqlInsert_t::CONST_MVA )
  8801. {
  8802. m_sError.SetSprintf ( "row %d, column %d: internal error: unknown insval type %d", 1+iRow, 1+iQuerySchemaIdx, tVal.m_iType ); // 1 for human base
  8803. return false;
  8804. }
  8805. if ( tVal.m_iType==SqlInsert_t::CONST_MVA && !( tCol.m_eAttrType==SPH_ATTR_UINT32SET || tCol.m_eAttrType==SPH_ATTR_INT64SET ) )
  8806. {
  8807. m_sError.SetSprintf ( "row %d, column %d: MVA value specified for a non-MVA column", 1+iRow, 1+iQuerySchemaIdx ); // 1 for human base
  8808. return false;
  8809. }
  8810. if ( ( tCol.m_eAttrType==SPH_ATTR_UINT32SET || tCol.m_eAttrType==SPH_ATTR_INT64SET ) && tVal.m_iType!=SqlInsert_t::CONST_MVA )
  8811. {
  8812. m_sError.SetSprintf ( "row %d, column %d: non-MVA value specified for a MVA column", 1+iRow, 1+iQuerySchemaIdx ); // 1 for human base
  8813. return false;
  8814. }
  8815. return true;
  8816. }
  8817. void AttributeConverter_c::SetDefaultAttrValue ( int iCol )
  8818. {
  8819. const CSphColumnInfo & tCol = m_tSchema.GetAttr(iCol);
  8820. CSphAttrLocator tLoc = tCol.m_tLocator;
  8821. tLoc.m_bDynamic = true;
  8822. if ( tCol.m_eAttrType==SPH_ATTR_STRING || tCol.m_eAttrType==SPH_ATTR_STRINGPTR || tCol.m_eAttrType==SPH_ATTR_JSON )
  8823. m_dStrings.Add(nullptr);
  8824. if ( tCol.m_eAttrType==SPH_ATTR_UINT32SET || tCol.m_eAttrType==SPH_ATTR_INT64SET )
  8825. m_dMvas.Add(0);
  8826. SqlInsert_t tDefaultVal;
  8827. tDefaultVal.m_iType = SqlInsert_t::CONST_INT;
  8828. tDefaultVal.m_iVal = 0;
  8829. SphAttr_t tAttr;
  8830. if ( CSphMatchVariant::ConvertPlainAttr ( tDefaultVal, tCol.m_eAttrType, tAttr ) )
  8831. {
  8832. if ( tCol.IsColumnar() )
  8833. m_dColumnarAttrs [ m_dColumnarRemap[iCol] ] = tAttr;
  8834. else
  8835. m_tDoc.SetAttr ( tLoc, tAttr );
  8836. }
  8837. }
  8838. bool AttributeConverter_c::SetAttrValue ( int iCol, const SqlInsert_t & tVal, int iRow, int iQuerySchemaIdx )
  8839. {
  8840. const CSphColumnInfo & tCol = m_tSchema.GetAttr(iCol);
  8841. CSphAttrLocator tLoc = tCol.m_tLocator;
  8842. tLoc.m_bDynamic = true;
  8843. if ( !CheckInsertTypes ( tCol, tVal, iRow, iQuerySchemaIdx ) )
  8844. return false;
  8845. // MVA column? grab the values
  8846. if ( tCol.m_eAttrType==SPH_ATTR_UINT32SET || tCol.m_eAttrType==SPH_ATTR_INT64SET )
  8847. {
  8848. // collect data from scattered insvals
  8849. // FIXME! maybe remove this mess, and just have a single m_dMvas pool in parser instead?
  8850. int iLen = 0;
  8851. if ( tVal.m_pVals )
  8852. {
  8853. tVal.m_pVals->Uniq();
  8854. iLen = tVal.m_pVals->GetLength();
  8855. }
  8856. m_dMvas.Add ( iLen );
  8857. for ( int j=0; j<iLen; j++ )
  8858. m_dMvas.Add ( (*tVal.m_pVals)[j] );
  8859. }
  8860. SphAttr_t tAttr;
  8861. if ( CSphMatchVariant::ConvertPlainAttr ( tVal, tCol.m_eAttrType, tAttr ) )
  8862. {
  8863. if ( tCol.IsColumnar() )
  8864. m_dColumnarAttrs [ m_dColumnarRemap[iCol] ] = tAttr;
  8865. else
  8866. m_tDoc.SetAttr ( tLoc, tAttr );
  8867. }
  8868. if ( !CheckDocId ( tCol, tAttr ) ) return false;
  8869. if ( !CheckStrings ( tCol, tVal, iCol, iRow ) ) return false;
  8870. if ( !CheckJson ( tCol, tVal ) ) return false;
  8871. return true;
  8872. }
  8873. bool AttributeConverter_c::SetFieldValue ( int iField, const SqlInsert_t & tVal, int iRow, int iQuerySchemaIdx )
  8874. {
  8875. if ( tVal.m_iType!=SqlInsert_t::QUOTED_STRING )
  8876. {
  8877. m_sError.SetSprintf ( "row %d, column %d: string expected", 1+iRow, 1+iQuerySchemaIdx ); // 1 for human base
  8878. return false;
  8879. }
  8880. const char * szFieldValue = tVal.m_sVal.cstr();
  8881. if ( m_dFieldAttrs[iField] )
  8882. {
  8883. m_dTmpFieldStorage[iField] = szFieldValue;
  8884. m_dFields[iField] = { m_dTmpFieldStorage[iField].cstr(), m_dTmpFieldStorage[iField].Length() };
  8885. } else
  8886. m_dFields[iField] = { szFieldValue, ( int64_t) strlen(szFieldValue) };
  8887. return true;
  8888. }
  8889. void AttributeConverter_c::SetDefaultFieldValue ( int iField )
  8890. {
  8891. m_dFields[iField] = { nullptr, 0 };
  8892. }
  8893. void AttributeConverter_c::NewRow()
  8894. {
  8895. m_dStrings.Resize(0);
  8896. m_tStrings.Reset();
  8897. m_dMvas.Resize(0);
  8898. }
  8899. void AttributeConverter_c::Finalize()
  8900. {
  8901. // remap JSON to string pointers
  8902. m_tStrings.SavePointersTo ( m_dStrings );
  8903. }
  8904. /////////////////////////////////////////////////////////////////////
  8905. static bool InsertToPQ ( SqlStmt_t & tStmt, RtIndex_i * pIndex, RtAccum_t * pAccum, CSphVector<int64_t> & dIds, const CSphMatch & tDoc, const CSphAttrLocator & tIdLoc, const CSphVector<const char *> & dStrings,
  8906. const CSphSchema & tSchemaInt, bool bReplace, CSphString & sError )
  8907. {
  8908. CSphVector<CSphFilterSettings> dFilters;
  8909. CSphVector<FilterTreeItem_t> dFilterTree;
  8910. if ( !PercolateParseFilters ( dStrings[2], session::GetCollation(), tSchemaInt, dFilters, dFilterTree, sError ) )
  8911. return false;
  8912. PercolateQueryArgs_t tArgs ( dFilters, dFilterTree );
  8913. tArgs.m_sQuery = dStrings[0];
  8914. tArgs.m_sTags = dStrings[1];
  8915. tArgs.m_iQUID = tDoc.GetAttr(tIdLoc);
  8916. tArgs.m_bReplace = bReplace;
  8917. tArgs.m_bQL = true;
  8918. // add query
  8919. auto * pQIndex = (PercolateIndex_i *)pIndex;
  8920. auto pStored = pQIndex->CreateQuery ( tArgs, sError );
  8921. if ( pStored )
  8922. {
  8923. auto * pCmd = pAccum->AddCommand ( ReplicationCommand_e::PQUERY_ADD, tStmt.m_sIndex, tStmt.m_sCluster );
  8924. dIds.Add ( pStored->m_iQUID );
  8925. pCmd->m_pStored = std::move ( pStored );
  8926. }
  8927. return true;
  8928. }
  8929. void sphHandleMysqlBegin ( StmtErrorReporter_i& tOut, Str_t sQuery )
  8930. {
  8931. auto* pSession = session::GetClientSession();
  8932. auto& tAcc = pSession->m_tAcc;
  8933. auto& sError = pSession->m_sError;
  8934. MEMORY ( MEM_SQL_BEGIN );
  8935. if ( tAcc.GetIndex() && !HandleCmdReplicate ( *tAcc.GetAcc(), sError ) )
  8936. return tOut.Error ( "%s", sError.cstr() );
  8937. pSession->m_bInTransaction = true;
  8938. tOut.Ok ( 0 );
  8939. }
  8940. void sphHandleMysqlCommitRollback ( StmtErrorReporter_i& tOut, Str_t sQuery, bool bCommit )
  8941. {
  8942. auto* pSession = session::GetClientSession();
  8943. auto& tAcc = pSession->m_tAcc;
  8944. auto& sError = pSession->m_sError;
  8945. auto& tCrashQuery = GlobalCrashQueryGetRef();
  8946. TRACE_CONN ( "conn", "sphHandleMysqlCommitRollback" );
  8947. MEMORY ( MEM_SQL_COMMIT );
  8948. pSession->m_bInTransaction = false;
  8949. RtIndex_i* pIndex = tAcc.GetIndex();
  8950. int iDeleted = 0;
  8951. if ( pIndex )
  8952. {
  8953. tCrashQuery.m_dIndex = FromStr ( pIndex->GetName() );
  8954. RtAccum_t* pAccum = tAcc.GetAcc ();
  8955. if ( bCommit )
  8956. {
  8957. StatCountCommand ( SEARCHD_COMMAND_COMMIT );
  8958. if ( !HandleCmdReplicate ( *pAccum, sError, iDeleted ) )
  8959. {
  8960. tOut.Error ( "%s", sError.cstr() );
  8961. return;
  8962. }
  8963. } else
  8964. {
  8965. pIndex->RollBack ( pAccum );
  8966. }
  8967. }
  8968. tOut.Ok ( iDeleted );
  8969. }
  8970. void sphHandleMysqlInsert ( StmtErrorReporter_i & tOut, SqlStmt_t & tStmt )
  8971. {
  8972. if ( !sphCheckWeCanModify ( tOut ) )
  8973. return;
  8974. auto* pSession = session::GetClientSession();
  8975. pSession->FreezeLastMeta();
  8976. bool bReplace = ( tStmt.m_eStmt == STMT_REPLACE );
  8977. bool bCommit = pSession->m_bAutoCommit && !pSession->m_bInTransaction;
  8978. auto& sWarning = pSession->m_tLastMeta.m_sWarning;
  8979. auto& tAcc = pSession->m_tAcc;
  8980. auto& dLastIds = pSession->m_dLastIds;
  8981. StatCountCommand ( bReplace ? SEARCHD_COMMAND_REPLACE : SEARCHD_COMMAND_INSERT );
  8982. MEMORY ( MEM_SQL_INSERT );
  8983. CSphString sError;
  8984. auto pServed = GetServed ( tStmt.m_sIndex );
  8985. if ( !ServedDesc_t::IsMutable ( pServed ) )
  8986. {
  8987. tOut.Error ( "index '%s' absent, or does not support INSERT", tStmt.m_sIndex.cstr ());
  8988. return;
  8989. }
  8990. GlobalCrashQueryGetRef().m_dIndex = FromStr ( tStmt.m_sIndex );
  8991. bool bPq = ( pServed->m_eType==IndexType_e::PERCOLATE );
  8992. RIdx_T<RtIndex_i*> pIndex { pServed };
  8993. // get schema, check values count
  8994. const CSphSchema & tSchema = pIndex->GetMatchSchema ();
  8995. int iSchemaSz = tSchema.GetAttrsCount() + tSchema.GetFieldsCount();
  8996. if ( pIndex->GetSettings().m_bIndexFieldLens )
  8997. iSchemaSz -= tSchema.GetFieldsCount();
  8998. if ( tSchema.GetAttr ( sphGetBlobLocatorName() ) )
  8999. iSchemaSz--;
  9000. int iExp = tStmt.m_iSchemaSz;
  9001. int iGot = tStmt.m_dInsertValues.GetLength();
  9002. if ( !tStmt.m_dInsertSchema.GetLength() && iSchemaSz!=tStmt.m_iSchemaSz )
  9003. {
  9004. tOut.Error ( "column count does not match schema (expected %d, got %d)", iSchemaSz, iGot );
  9005. return;
  9006. }
  9007. if ( ( iGot % iExp )!=0 )
  9008. {
  9009. tOut.Error ( "column count does not match value count (expected %d, got %d)", iExp, iGot );
  9010. return;
  9011. }
  9012. if ( !CheckIndexCluster ( tStmt.m_sIndex, *pServed, tStmt.m_sCluster, IsHttpStmt ( tStmt ), sError ) )
  9013. {
  9014. tOut.Error ( "%s", sError.cstr() );
  9015. return;
  9016. }
  9017. if ( !sError.IsEmpty() )
  9018. {
  9019. tOut.Error ( "%s", sError.cstr() );
  9020. return;
  9021. }
  9022. CSphVector<int> dAttrSchema ( tSchema.GetAttrsCount() );
  9023. CSphVector<int> dFieldSchema ( tSchema.GetFieldsCount() );
  9024. CSphVector<bool> dFieldAttrs ( tSchema.GetFieldsCount() );
  9025. if ( !CreateAttrMaps ( dAttrSchema, dFieldSchema, dFieldAttrs, tSchema, tStmt.m_dInsertSchema, tOut ) )
  9026. return;
  9027. RtAccum_t * pAccum = tAcc.GetAcc ( pIndex, sError );
  9028. if ( !sError.IsEmpty() )
  9029. return tOut.Error ( "%s", sError.cstr() );
  9030. CSphVector<int64_t> dIds;
  9031. dIds.Reserve ( tStmt.m_iRowsAffected );
  9032. const CSphColumnInfo * pDocid = tSchema.GetAttr(sphGetDocidName());
  9033. assert ( pDocid );
  9034. CSphAttrLocator tIdLoc = pDocid->m_tLocator;
  9035. tIdLoc.m_bDynamic = true;
  9036. AttributeConverter_c tConverter ( tSchema, dFieldAttrs, sError, sWarning );
  9037. // convert attrs
  9038. for ( int iRow=0; iRow<tStmt.m_iRowsAffected; iRow++ )
  9039. {
  9040. assert ( sError.IsEmpty() );
  9041. tConverter.NewRow();
  9042. int iSchemaAttrCount = tSchema.GetAttrsCount();
  9043. if ( pIndex->GetSettings().m_bIndexFieldLens )
  9044. iSchemaAttrCount -= tSchema.GetFieldsCount();
  9045. bool bOk = true;
  9046. for ( int i=0; i<iSchemaAttrCount && bOk; i++ )
  9047. {
  9048. int iQuerySchemaIdx = dAttrSchema[i];
  9049. if ( iQuerySchemaIdx < 0 )
  9050. tConverter.SetDefaultAttrValue(i);
  9051. else
  9052. bOk = tConverter.SetAttrValue ( i, tStmt.m_dInsertValues[iQuerySchemaIdx + iRow * iExp], iRow, iQuerySchemaIdx );
  9053. }
  9054. if ( !bOk )
  9055. break;
  9056. // if strings and fields share one value, it might be modified by html stripper etc
  9057. // we need to use separate storage for such string attributes and fields
  9058. for ( int i = 0; i < tSchema.GetFieldsCount() && bOk; i++ )
  9059. {
  9060. int iQuerySchemaIdx = dFieldSchema[i];
  9061. if ( iQuerySchemaIdx < 0 )
  9062. tConverter.SetDefaultFieldValue(i);
  9063. else
  9064. bOk = tConverter.SetFieldValue( i, tStmt.m_dInsertValues [ iQuerySchemaIdx + iRow * iExp ], iRow, iQuerySchemaIdx );
  9065. }
  9066. if ( !bOk )
  9067. break;
  9068. tConverter.Finalize();
  9069. // do add
  9070. if ( bPq )
  9071. {
  9072. if ( !InsertToPQ ( tStmt, pIndex, pAccum, dIds, tConverter.m_tDoc, tIdLoc, tConverter.m_dStrings, pIndex->GetInternalSchema(), bReplace, sError ) )
  9073. break;
  9074. }
  9075. else
  9076. {
  9077. pIndex->AddDocument ( tConverter, bReplace, tStmt.m_sStringParam, sError, sWarning, pAccum );
  9078. dIds.Add ( tConverter.GetID() );
  9079. pAccum->AddCommand ( ReplicationCommand_e::RT_TRX, tStmt.m_sIndex, tStmt.m_sCluster );
  9080. }
  9081. if ( !sError.IsEmpty() )
  9082. break;
  9083. }
  9084. // fire exit
  9085. if ( !sError.IsEmpty() )
  9086. {
  9087. pIndex->RollBack ( pAccum ); // clean up collected data
  9088. tOut.Error ( "%s", sError.cstr() );
  9089. return;
  9090. }
  9091. dLastIds.SwapData ( dIds );
  9092. // no errors so far
  9093. if ( bCommit )
  9094. {
  9095. if ( !HandleCmdReplicate ( *pAccum, sError ) )
  9096. {
  9097. pIndex->RollBack ( pAccum ); // clean up collected data
  9098. tOut.Error ( "%s", sError.cstr() );
  9099. return;
  9100. }
  9101. }
  9102. int64_t iLastInsertId = 0;
  9103. if ( dLastIds.GetLength() )
  9104. iLastInsertId = dLastIds.Last();
  9105. // my OK packet
  9106. tOut.Ok ( tStmt.m_iRowsAffected, sWarning, iLastInsertId );
  9107. }
  9108. void HandleMysqlCallSnippets ( RowBuffer_i & tOut, SqlStmt_t & tStmt )
  9109. {
  9110. StatCountCommand ( SEARCHD_COMMAND_EXCERPT );
  9111. CSphString sError;
  9112. // check arguments
  9113. // string data, string index, string query, [named opts]
  9114. if ( tStmt.m_dInsertValues.GetLength()!=3 )
  9115. {
  9116. tOut.Error ( tStmt.m_sStmt, "SNIPPETS() expects exactly 3 arguments (data, index, query)" );
  9117. return;
  9118. }
  9119. if ( tStmt.m_dInsertValues[0].m_iType!=SqlInsert_t::QUOTED_STRING && tStmt.m_dInsertValues[0].m_iType!=SqlInsert_t::CONST_STRINGS )
  9120. {
  9121. tOut.Error ( tStmt.m_sStmt, "SNIPPETS() argument 1 must be a string or a string list" );
  9122. return;
  9123. }
  9124. if ( tStmt.m_dInsertValues[1].m_iType!=SqlInsert_t::QUOTED_STRING )
  9125. {
  9126. tOut.Error ( tStmt.m_sStmt, "SNIPPETS() argument 2 must be a string" );
  9127. return;
  9128. }
  9129. if ( tStmt.m_dInsertValues[2].m_iType!=SqlInsert_t::QUOTED_STRING )
  9130. {
  9131. tOut.Error ( tStmt.m_sStmt, "SNIPPETS() argument 3 must be a string" );
  9132. return;
  9133. }
  9134. // do magics
  9135. CSphString sIndex = tStmt.m_dInsertValues[1].m_sVal;
  9136. SnippetQuerySettings_t q;
  9137. q.m_sQuery = tStmt.m_dInsertValues[2].m_sVal;
  9138. ARRAY_FOREACH ( i, tStmt.m_dCallOptNames )
  9139. {
  9140. CSphString & sOpt = tStmt.m_dCallOptNames[i];
  9141. const SqlInsert_t & v = tStmt.m_dCallOptValues[i];
  9142. sOpt.ToLower();
  9143. int iExpType = -1;
  9144. if ( sOpt=="before_match" ) { q.m_sBeforeMatch = v.m_sVal; iExpType = SqlInsert_t::QUOTED_STRING; }
  9145. else if ( sOpt=="after_match" ) { q.m_sAfterMatch = v.m_sVal; iExpType = SqlInsert_t::QUOTED_STRING; }
  9146. else if ( sOpt=="chunk_separator" || sOpt=="snippet_separator" ) { q.m_sChunkSeparator = v.m_sVal; iExpType = SqlInsert_t::QUOTED_STRING; }
  9147. else if ( sOpt=="html_strip_mode" ) { q.m_sStripMode = v.m_sVal; iExpType = SqlInsert_t::QUOTED_STRING; }
  9148. else if ( sOpt=="passage_boundary" || sOpt=="snippet_boundary" ) { q.m_ePassageSPZ = GetPassageBoundary(v.m_sVal); iExpType = SqlInsert_t::QUOTED_STRING; }
  9149. else if ( sOpt=="limit" ) { q.m_iLimit = (int)v.m_iVal; iExpType = SqlInsert_t::CONST_INT; }
  9150. else if ( sOpt=="limit_words" ) { q.m_iLimitWords = (int)v.m_iVal; iExpType = SqlInsert_t::CONST_INT; }
  9151. else if ( sOpt=="limit_passages" || sOpt=="limit_snippets" ) { q.m_iLimitPassages = (int)v.m_iVal; iExpType = SqlInsert_t::CONST_INT; }
  9152. else if ( sOpt=="around" ) { q.m_iAround = (int)v.m_iVal; iExpType = SqlInsert_t::CONST_INT; }
  9153. else if ( sOpt=="start_passage_id" || sOpt=="start_snippet_id" ) { q.m_iPassageId = (int)v.m_iVal; iExpType = SqlInsert_t::CONST_INT; }
  9154. else if ( sOpt=="exact_phrase" )
  9155. {
  9156. sError.SetSprintf ( "exact_phrase is deprecated" );
  9157. break;
  9158. }
  9159. else if ( sOpt=="use_boundaries" ) { q.m_bUseBoundaries = ( v.m_iVal!=0 ); iExpType = SqlInsert_t::CONST_INT; }
  9160. else if ( sOpt=="weight_order" ) { q.m_bWeightOrder = ( v.m_iVal!=0 ); iExpType = SqlInsert_t::CONST_INT; }
  9161. else if ( sOpt=="query_mode" )
  9162. {
  9163. bool bQueryMode = ( v.m_iVal!=0 );
  9164. iExpType = SqlInsert_t::CONST_INT;
  9165. if ( !bQueryMode )
  9166. {
  9167. sError.SetSprintf ( "query_mode=0 is deprecated" );
  9168. break;
  9169. }
  9170. }
  9171. else if ( sOpt=="force_all_words" ) { q.m_bForceAllWords = ( v.m_iVal!=0 ); iExpType = SqlInsert_t::CONST_INT; }
  9172. else if ( sOpt=="load_files" ) { q.m_uFilesMode = ( v.m_iVal!=0 )?1:0; iExpType = SqlInsert_t::CONST_INT; }
  9173. else if ( sOpt=="load_files_scattered" ) { q.m_uFilesMode |= ( v.m_iVal!=0 )?2:0; iExpType = SqlInsert_t::CONST_INT; }
  9174. else if ( sOpt=="allow_empty" ) { q.m_bAllowEmpty = ( v.m_iVal!=0 ); iExpType = SqlInsert_t::CONST_INT; }
  9175. else if ( sOpt=="emit_zones" ) { q.m_bEmitZones = ( v.m_iVal!=0 ); iExpType = SqlInsert_t::CONST_INT; }
  9176. else if ( sOpt=="force_passages" || sOpt=="force_snippets" ) { q.m_bForcePassages = ( v.m_iVal!=0 ); iExpType = SqlInsert_t::CONST_INT; }
  9177. else
  9178. {
  9179. sError.SetSprintf ( "unknown option %s", sOpt.cstr() );
  9180. break;
  9181. }
  9182. // post-conf type check
  9183. if ( iExpType!=v.m_iType )
  9184. {
  9185. sError.SetSprintf ( "unexpected option %s type", sOpt.cstr() );
  9186. break;
  9187. }
  9188. }
  9189. if ( !sError.IsEmpty() )
  9190. {
  9191. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9192. return;
  9193. }
  9194. if ( !sphCheckOptionsSPZ ( q, q.m_ePassageSPZ, sError ) )
  9195. {
  9196. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9197. return;
  9198. }
  9199. q.Setup();
  9200. CSphVector<ExcerptQuery_t> dQueries;
  9201. if ( tStmt.m_dInsertValues[0].m_iType==SqlInsert_t::QUOTED_STRING )
  9202. {
  9203. auto& dQuery = dQueries.Add ();
  9204. dQuery.m_sSource = tStmt.m_dInsertValues[0].m_sVal; // OPTIMIZE?
  9205. } else
  9206. {
  9207. dQueries.Resize ( tStmt.m_dCallStrings.GetLength() );
  9208. ARRAY_FOREACH ( i, tStmt.m_dCallStrings )
  9209. {
  9210. dQueries[i].m_sSource = tStmt.m_dCallStrings[i]; // OPTIMIZE?
  9211. }
  9212. }
  9213. myinfo::SetTaskInfo ( R"(sphinxql-snippet datasize=%.1Dk query="%s")", GetSnippetDataSize ( dQueries ), q.m_sQuery.scstr ());
  9214. if ( !MakeSnippets ( sIndex, dQueries, q, sError ) )
  9215. {
  9216. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9217. return;
  9218. }
  9219. if ( !dQueries.any_of ( [] ( const ExcerptQuery_t & tQuery ) { return tQuery.m_sError.IsEmpty(); } ) )
  9220. {
  9221. // just one last error instead of all errors is hopefully ok
  9222. sError.SetSprintf ( "highlighting failed: %s", sError.cstr() );
  9223. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9224. return;
  9225. }
  9226. // result set header packet
  9227. tOut.HeadBegin(1);
  9228. tOut.HeadColumn("snippet");
  9229. tOut.HeadEnd();
  9230. // data
  9231. for ( auto & i : dQueries )
  9232. {
  9233. FixupResultTail ( i.m_dResult );
  9234. tOut.PutArray ( i.m_dResult );
  9235. tOut.Commit();
  9236. }
  9237. tOut.Eof();
  9238. }
  9239. class KeywordsRequestBuilder_c : public RequestBuilder_i
  9240. {
  9241. public:
  9242. KeywordsRequestBuilder_c ( const GetKeywordsSettings_t & tSettings, const CSphString & sTerm );
  9243. void BuildRequest ( const AgentConn_t & tAgent, ISphOutputBuffer & tOut ) const final;
  9244. protected:
  9245. const GetKeywordsSettings_t & m_tSettings;
  9246. const CSphString & m_sTerm;
  9247. };
  9248. class KeywordsReplyParser_c : public ReplyParser_i
  9249. {
  9250. public:
  9251. KeywordsReplyParser_c ( bool bGetStats, CSphVector<CSphKeywordInfo> & dKeywords );
  9252. bool ParseReply ( MemInputBuffer_c & tReq, AgentConn_t & tAgent ) const final;
  9253. bool m_bStats;
  9254. CSphVector<CSphKeywordInfo> & m_dKeywords;
  9255. };
  9256. static void MergeKeywords ( CSphVector<CSphKeywordInfo> & dKeywords );
  9257. static void SortKeywords ( const GetKeywordsSettings_t & tSettings, CSphVector<CSphKeywordInfo> & dKeywords );
  9258. bool DoGetKeywords ( const CSphString & sIndex, const CSphString & sQuery, const GetKeywordsSettings_t & tSettings, CSphVector <CSphKeywordInfo> & dKeywords, CSphString & sError, SearchFailuresLog_c & tFailureLog )
  9259. {
  9260. auto pLocal = GetServed ( sIndex );
  9261. auto pDistributed = GetDistr ( sIndex );
  9262. if ( !pLocal && !pDistributed )
  9263. {
  9264. sError.SetSprintf ( "no such index %s", sIndex.cstr() );
  9265. return false;
  9266. }
  9267. bool bOk = false;
  9268. // just local plain or template index
  9269. if ( pLocal )
  9270. bOk = RIdx_c(pLocal)->GetKeywords ( dKeywords, sQuery.cstr(), tSettings, &sError );
  9271. else
  9272. {
  9273. // FIXME!!! g_iDistThreads thread pool for locals.
  9274. // locals
  9275. const StrVec_t & dLocals = pDistributed->m_dLocal;
  9276. CSphVector<CSphKeywordInfo> dKeywordsLocal;
  9277. for ( const CSphString &sLocal : dLocals )
  9278. {
  9279. auto pServed = GetServed ( sLocal );
  9280. if ( !pServed )
  9281. {
  9282. tFailureLog.Submit ( sLocal.cstr(), sIndex.cstr(), "missed index" );
  9283. continue;
  9284. }
  9285. dKeywordsLocal.Resize(0);
  9286. if ( RIdx_c ( pServed )->GetKeywords ( dKeywordsLocal, sQuery.cstr(), tSettings, &sError ) )
  9287. dKeywords.Append ( dKeywordsLocal );
  9288. else
  9289. tFailureLog.SubmitEx ( sLocal, sIndex.cstr (), "keyword extraction failed: %s", sError.cstr () );
  9290. }
  9291. // remote agents requests send off thread
  9292. VecRefPtrsAgentConn_t dAgents;
  9293. // fixme! We don't need all hosts here, only usual selected mirrors
  9294. pDistributed->GetAllHosts ( dAgents );
  9295. int iAgentsReply = 0;
  9296. if ( !dAgents.IsEmpty() )
  9297. {
  9298. // connect to remote agents and query them
  9299. KeywordsRequestBuilder_c tReqBuilder ( tSettings, sQuery );
  9300. KeywordsReplyParser_c tParser ( tSettings.m_bStats, dKeywords );
  9301. iAgentsReply = PerformRemoteTasks ( dAgents, &tReqBuilder, &tParser );
  9302. for ( const AgentConn_t * pAgent : dAgents )
  9303. if ( !pAgent->m_bSuccess && !pAgent->m_sFailure.IsEmpty() )
  9304. tFailureLog.SubmitEx ( pAgent->m_tDesc.m_sIndexes, sIndex.cstr(),
  9305. "agent %s: %s", pAgent->m_tDesc.GetMyUrl().cstr(), pAgent->m_sFailure.cstr() );
  9306. }
  9307. // process result sets
  9308. if ( dLocals.GetLength() + iAgentsReply>1 )
  9309. MergeKeywords ( dKeywords );
  9310. bOk = true;
  9311. }
  9312. SortKeywords ( tSettings, dKeywords );
  9313. return bOk;
  9314. }
  9315. void HandleMysqlCallKeywords ( RowBuffer_i & tOut, SqlStmt_t & tStmt, CSphString & sWarning )
  9316. {
  9317. StatCountCommand ( SEARCHD_COMMAND_KEYWORDS );
  9318. CSphString sError;
  9319. // string query, string index, [bool hits] || [value as option_name, ...]
  9320. int iArgs = tStmt.m_dInsertValues.GetLength();
  9321. if ( iArgs<2
  9322. || iArgs>3
  9323. || tStmt.m_dInsertValues[0].m_iType!=SqlInsert_t::QUOTED_STRING
  9324. || tStmt.m_dInsertValues[1].m_iType!=SqlInsert_t::QUOTED_STRING
  9325. || ( iArgs==3 && tStmt.m_dInsertValues[2].m_iType!=SqlInsert_t::CONST_INT ) )
  9326. {
  9327. tOut.Error ( tStmt.m_sStmt, "bad argument count or types in KEYWORDS() call" );
  9328. return;
  9329. }
  9330. GetKeywordsSettings_t tSettings;
  9331. tSettings.m_bStats = ( iArgs==3 && tStmt.m_dInsertValues[2].m_iVal!=0 );
  9332. ARRAY_FOREACH ( i, tStmt.m_dCallOptNames )
  9333. {
  9334. CSphString & sOpt = tStmt.m_dCallOptNames[i];
  9335. sOpt.ToLower ();
  9336. bool bEnabled = ( tStmt.m_dCallOptValues[i].m_iVal!=0 );
  9337. bool bOptInt = true;
  9338. if ( sOpt=="stats" )
  9339. tSettings.m_bStats = bEnabled;
  9340. else if ( sOpt=="fold_lemmas" )
  9341. tSettings.m_bFoldLemmas = bEnabled;
  9342. else if ( sOpt=="fold_blended" )
  9343. tSettings.m_bFoldBlended = bEnabled;
  9344. else if ( sOpt=="fold_wildcards" )
  9345. tSettings.m_bFoldWildcards = bEnabled;
  9346. else if ( sOpt=="expansion_limit" )
  9347. tSettings.m_iExpansionLimit = int ( tStmt.m_dCallOptValues[i].m_iVal );
  9348. else if ( sOpt=="sort_mode" )
  9349. {
  9350. // FIXME!!! add more sorting modes
  9351. if ( tStmt.m_dCallOptValues[i].m_sVal!="docs" && tStmt.m_dCallOptValues[i].m_sVal!="hits" )
  9352. {
  9353. sError.SetSprintf ( "unknown option %s mode '%s'", sOpt.cstr(), tStmt.m_dCallOptValues[i].m_sVal.cstr() );
  9354. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9355. return;
  9356. }
  9357. tSettings.m_bSortByDocs = ( tStmt.m_dCallOptValues[i].m_sVal=="docs" );
  9358. tSettings.m_bSortByHits = ( tStmt.m_dCallOptValues[i].m_sVal=="hits" );
  9359. bOptInt = false;
  9360. } else
  9361. {
  9362. sError.SetSprintf ( "unknown option %s", sOpt.cstr () );
  9363. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9364. return;
  9365. }
  9366. // post-conf type check
  9367. if ( bOptInt && tStmt.m_dCallOptValues[i].m_iType!=SqlInsert_t::CONST_INT )
  9368. {
  9369. sError.SetSprintf ( "unexpected option %s type", sOpt.cstr () );
  9370. tOut.Error ( tStmt.m_sStmt, sError.cstr () );
  9371. return;
  9372. }
  9373. }
  9374. const CSphString & sTerm = tStmt.m_dInsertValues[0].m_sVal;
  9375. const CSphString & sIndex = tStmt.m_dInsertValues[1].m_sVal;
  9376. CSphVector<CSphKeywordInfo> dKeywords;
  9377. SearchFailuresLog_c tFailureLog;
  9378. if ( !DoGetKeywords ( sIndex, sTerm, tSettings, dKeywords, sError, tFailureLog ) )
  9379. {
  9380. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9381. return;
  9382. }
  9383. // result set header packet
  9384. tOut.HeadBegin ( tSettings.m_bStats ? 5 : 3 );
  9385. tOut.HeadColumn("qpos");
  9386. tOut.HeadColumn("tokenized");
  9387. tOut.HeadColumn("normalized");
  9388. if ( tSettings.m_bStats )
  9389. {
  9390. tOut.HeadColumn("docs");
  9391. tOut.HeadColumn("hits");
  9392. }
  9393. tOut.HeadEnd();
  9394. // data
  9395. char sBuf[16];
  9396. ARRAY_FOREACH ( i, dKeywords )
  9397. {
  9398. snprintf ( sBuf, sizeof(sBuf), "%d", dKeywords[i].m_iQpos );
  9399. tOut.PutString ( sBuf );
  9400. tOut.PutString ( dKeywords[i].m_sTokenized );
  9401. tOut.PutString ( dKeywords[i].m_sNormalized );
  9402. if ( tSettings.m_bStats )
  9403. {
  9404. snprintf ( sBuf, sizeof(sBuf), "%d", dKeywords[i].m_iDocs );
  9405. tOut.PutString ( sBuf );
  9406. snprintf ( sBuf, sizeof(sBuf), "%d", dKeywords[i].m_iHits );
  9407. tOut.PutString ( sBuf );
  9408. }
  9409. tOut.Commit();
  9410. }
  9411. // put network errors and warnings to meta as warning
  9412. int iWarnings = 0;
  9413. if ( !tFailureLog.IsEmpty() )
  9414. {
  9415. iWarnings = tFailureLog.GetReportsCount();
  9416. StringBuilder_c sErrorBuf;
  9417. tFailureLog.BuildReport ( sErrorBuf );
  9418. sErrorBuf.MoveTo ( sWarning );
  9419. sphWarning ( "%s", sWarning.cstr() );
  9420. }
  9421. tOut.Eof ( false, iWarnings );
  9422. }
  9423. KeywordsRequestBuilder_c::KeywordsRequestBuilder_c ( const GetKeywordsSettings_t & tSettings, const CSphString & sTerm )
  9424. : m_tSettings ( tSettings )
  9425. , m_sTerm ( sTerm )
  9426. {
  9427. }
  9428. void KeywordsRequestBuilder_c::BuildRequest ( const AgentConn_t & tAgent, ISphOutputBuffer & tOut ) const
  9429. {
  9430. const CSphString & sIndexes = tAgent.m_tDesc.m_sIndexes;
  9431. auto tHdr = APIHeader ( tOut, SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS );
  9432. tOut.SendString ( m_sTerm.cstr() );
  9433. tOut.SendString ( sIndexes.cstr() );
  9434. tOut.SendInt ( m_tSettings.m_bStats );
  9435. tOut.SendInt ( m_tSettings.m_bFoldLemmas );
  9436. tOut.SendInt ( m_tSettings.m_bFoldBlended );
  9437. tOut.SendInt ( m_tSettings.m_bFoldWildcards );
  9438. tOut.SendInt ( m_tSettings.m_iExpansionLimit );
  9439. }
  9440. KeywordsReplyParser_c::KeywordsReplyParser_c ( bool bGetStats, CSphVector<CSphKeywordInfo> & dKeywords )
  9441. : m_bStats ( bGetStats )
  9442. , m_dKeywords ( dKeywords )
  9443. {
  9444. }
  9445. bool KeywordsReplyParser_c::ParseReply ( MemInputBuffer_c & tReq, AgentConn_t & ) const
  9446. {
  9447. int iWords = tReq.GetInt();
  9448. int iLen = m_dKeywords.GetLength();
  9449. m_dKeywords.Resize ( iWords + iLen );
  9450. for ( int i=0; i<iWords; i++ )
  9451. {
  9452. CSphKeywordInfo & tWord = m_dKeywords[i + iLen];
  9453. tWord.m_sTokenized = tReq.GetString();
  9454. tWord.m_sNormalized = tReq.GetString();
  9455. tWord.m_iQpos = tReq.GetInt();
  9456. if ( m_bStats )
  9457. {
  9458. tWord.m_iDocs = tReq.GetInt();
  9459. tWord.m_iHits = tReq.GetInt();
  9460. }
  9461. }
  9462. return true;
  9463. }
  9464. struct KeywordSorter_fn
  9465. {
  9466. bool IsLess ( const CSphKeywordInfo & a, const CSphKeywordInfo & b ) const
  9467. {
  9468. return ( ( a.m_iQpos<b.m_iQpos )
  9469. || ( a.m_iQpos==b.m_iQpos && a.m_iHits>b.m_iHits )
  9470. || ( a.m_iQpos==b.m_iQpos && a.m_iHits==b.m_iHits && a.m_sNormalized<b.m_sNormalized ) );
  9471. }
  9472. };
  9473. void MergeKeywords ( CSphVector<CSphKeywordInfo> & dSrc )
  9474. {
  9475. CSphOrderedHash < CSphKeywordInfo, uint64_t, IdentityHash_fn, 256 > hWords;
  9476. ARRAY_FOREACH ( i, dSrc )
  9477. {
  9478. const CSphKeywordInfo & tInfo = dSrc[i];
  9479. uint64_t uKey = sphFNV64 ( &tInfo.m_iQpos, sizeof(tInfo.m_iQpos) );
  9480. uKey = sphFNV64 ( tInfo.m_sNormalized.cstr(), tInfo.m_sNormalized.Length(), uKey );
  9481. CSphKeywordInfo & tVal = hWords.AddUnique ( uKey );
  9482. if ( !tVal.m_iQpos )
  9483. {
  9484. tVal = tInfo;
  9485. } else
  9486. {
  9487. tVal.m_iDocs += tInfo.m_iDocs;
  9488. tVal.m_iHits += tInfo.m_iHits;
  9489. }
  9490. }
  9491. dSrc.Resize ( 0 );
  9492. for ( const auto& tWord : hWords )
  9493. dSrc.Add ( tWord.second );
  9494. sphSort ( dSrc.Begin(), dSrc.GetLength(), KeywordSorter_fn() );
  9495. }
  9496. struct KeywordSorterDocs_fn
  9497. {
  9498. bool IsLess ( const CSphKeywordInfo & a, const CSphKeywordInfo & b ) const
  9499. {
  9500. return ( ( a.m_iQpos<b.m_iQpos )
  9501. || ( a.m_iQpos==b.m_iQpos && a.m_iDocs>b.m_iDocs )
  9502. || ( a.m_iQpos==b.m_iQpos && a.m_iDocs==b.m_iDocs && a.m_sNormalized<b.m_sNormalized ) );
  9503. }
  9504. };
  9505. void SortKeywords ( const GetKeywordsSettings_t & tSettings, CSphVector<CSphKeywordInfo> & dKeywords )
  9506. {
  9507. if ( !tSettings.m_bSortByDocs && !tSettings.m_bSortByHits )
  9508. return;
  9509. if ( tSettings.m_bSortByHits )
  9510. dKeywords.Sort ( KeywordSorter_fn() );
  9511. else
  9512. dKeywords.Sort ( KeywordSorterDocs_fn() );
  9513. }
  9514. // sort by distance asc, document count desc, ABC asc
  9515. struct CmpDistDocABC_fn
  9516. {
  9517. const char * m_pBuf;
  9518. explicit CmpDistDocABC_fn ( const char * pBuf ) : m_pBuf ( pBuf ) {}
  9519. inline bool IsLess ( const SuggestWord_t & a, const SuggestWord_t & b ) const
  9520. {
  9521. if ( a.m_iDistance==b.m_iDistance && a.m_iDocs==b.m_iDocs )
  9522. {
  9523. return ( sphDictCmpStrictly ( m_pBuf + a.m_iNameOff, a.m_iLen, m_pBuf + b.m_iNameOff, b.m_iLen )<0 );
  9524. }
  9525. if ( a.m_iDistance==b.m_iDistance )
  9526. return a.m_iDocs>=b.m_iDocs;
  9527. return a.m_iDistance<b.m_iDistance;
  9528. }
  9529. };
  9530. void HandleMysqlCallSuggest ( RowBuffer_i & tOut, SqlStmt_t & tStmt, bool bQueryMode )
  9531. {
  9532. StatCountCommand ( SEARCHD_COMMAND_SUGGEST );
  9533. CSphString sError;
  9534. // string query, string index, [value as option_name, ...]
  9535. int iArgs = tStmt.m_dInsertValues.GetLength ();
  9536. if ( iArgs<2
  9537. || iArgs>3
  9538. || tStmt.m_dInsertValues[0].m_iType!=SqlInsert_t::QUOTED_STRING
  9539. || tStmt.m_dInsertValues[1].m_iType!=SqlInsert_t::QUOTED_STRING
  9540. || ( iArgs==3 && tStmt.m_dInsertValues[2].m_iType!=SqlInsert_t::CONST_INT ) )
  9541. {
  9542. tOut.Error ( tStmt.m_sStmt, "bad argument count or types in KEYWORDS() call" );
  9543. return;
  9544. }
  9545. SuggestArgs_t tArgs;
  9546. SuggestResult_t tRes;
  9547. const char * sWord = tStmt.m_dInsertValues[0].m_sVal.cstr();
  9548. tArgs.m_bQueryMode = bQueryMode;
  9549. ARRAY_FOREACH ( i, tStmt.m_dCallOptNames )
  9550. {
  9551. CSphString & sOpt = tStmt.m_dCallOptNames[i];
  9552. sOpt.ToLower ();
  9553. int iTokType = SqlInsert_t::CONST_INT;
  9554. if ( sOpt=="limit" )
  9555. {
  9556. tArgs.m_iLimit = int ( tStmt.m_dCallOptValues[i].m_iVal );
  9557. } else if ( sOpt=="delta_len" )
  9558. {
  9559. tArgs.m_iDeltaLen = int ( tStmt.m_dCallOptValues[i].m_iVal );
  9560. } else if ( sOpt=="max_matches" )
  9561. {
  9562. tArgs.m_iQueueLen = int ( tStmt.m_dCallOptValues[i].m_iVal );
  9563. } else if ( sOpt=="reject" )
  9564. {
  9565. tArgs.m_iRejectThr = int ( tStmt.m_dCallOptValues[i].m_iVal );
  9566. } else if ( sOpt=="max_edits" )
  9567. {
  9568. tArgs.m_iMaxEdits = int ( tStmt.m_dCallOptValues[i].m_iVal );
  9569. } else if ( sOpt=="result_line" )
  9570. {
  9571. tArgs.m_bResultOneline = ( tStmt.m_dCallOptValues[i].m_iVal!=0 );
  9572. } else if ( sOpt=="result_stats" )
  9573. {
  9574. tArgs.m_bResultStats = ( tStmt.m_dCallOptValues[i].m_iVal!=0 );
  9575. } else if ( sOpt=="non_char" )
  9576. {
  9577. tArgs.m_bNonCharAllowed = ( tStmt.m_dCallOptValues[i].m_iVal!=0 );
  9578. } else
  9579. {
  9580. sError.SetSprintf ( "unknown option %s", sOpt.cstr () );
  9581. tOut.Error ( tStmt.m_sStmt, sError.cstr () );
  9582. return;
  9583. }
  9584. // post-conf type check
  9585. if ( tStmt.m_dCallOptValues[i].m_iType!=iTokType )
  9586. {
  9587. sError.SetSprintf ( "unexpected option %s type", sOpt.cstr () );
  9588. tOut.Error ( tStmt.m_sStmt, sError.cstr () );
  9589. return;
  9590. }
  9591. }
  9592. { // scope for ServedINdexPtr_c
  9593. auto pServed = GetServed ( tStmt.m_dInsertValues[1].m_sVal );
  9594. if ( !pServed )
  9595. {
  9596. sError.SetSprintf ( "no such index %s", tStmt.m_dInsertValues[1].m_sVal.cstr () );
  9597. tOut.Error ( tStmt.m_sStmt, sError.cstr () );
  9598. return;
  9599. }
  9600. RIdx_c pIdx { pServed };
  9601. if ( !pIdx->GetSettings().m_iMinInfixLen || !pIdx->GetDictionary()->GetSettings().m_bWordDict )
  9602. {
  9603. sError.SetSprintf ( "suggests work only for keywords dictionary with infix enabled" );
  9604. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9605. return;
  9606. }
  9607. if ( tRes.SetWord ( sWord, pIdx->GetQueryTokenizer(), tArgs.m_bQueryMode ) )
  9608. {
  9609. pIdx->GetSuggest ( tArgs, tRes );
  9610. }
  9611. }
  9612. // data
  9613. if ( tArgs.m_bResultOneline )
  9614. {
  9615. // let's resort by alphabet to better compare result sets
  9616. CmpDistDocABC_fn tCmp ( (const char *)( tRes.m_dBuf.Begin() ) );
  9617. tRes.m_dMatched.Sort ( tCmp );
  9618. // result set header packet
  9619. tOut.HeadBegin ( 2 );
  9620. tOut.HeadColumn ( "name" );
  9621. tOut.HeadColumn ( "value" );
  9622. tOut.HeadEnd ();
  9623. StringBuilder_c sBuf ( "," );
  9624. for ( auto& dMatched : tRes.m_dMatched )
  9625. sBuf << (const char*) tRes.m_dBuf.Begin() + dMatched.m_iNameOff;
  9626. tOut.PutString ( "suggests" );
  9627. tOut.PutString ( sBuf.cstr() );
  9628. tOut.Commit();
  9629. if ( tArgs.m_bResultStats )
  9630. {
  9631. sBuf.Clear ();
  9632. sBuf.StartBlock ( "," );
  9633. for ( auto &dMatched : tRes.m_dMatched )
  9634. sBuf.Appendf ( "%d", dMatched.m_iDistance );
  9635. tOut.PutString ( "distance" );
  9636. tOut.PutString ( sBuf.cstr () );
  9637. tOut.Commit ();
  9638. sBuf.Clear ();
  9639. sBuf.StartBlock ( "," );
  9640. for ( auto &dMatched : tRes.m_dMatched )
  9641. sBuf.Appendf ( "%d", dMatched.m_iDocs );
  9642. tOut.PutString ( "docs" );
  9643. tOut.PutString ( sBuf );
  9644. tOut.Commit ();
  9645. }
  9646. } else
  9647. {
  9648. // result set header packet
  9649. tOut.HeadBegin ( tArgs.m_bResultStats ? 3 : 1 );
  9650. tOut.HeadColumn ( "suggest" );
  9651. if ( tArgs.m_bResultStats )
  9652. {
  9653. tOut.HeadColumn ( "distance" );
  9654. tOut.HeadColumn ( "docs" );
  9655. }
  9656. tOut.HeadEnd ();
  9657. auto * szResult = (const char *)( tRes.m_dBuf.Begin() );
  9658. ARRAY_FOREACH ( i, tRes.m_dMatched )
  9659. {
  9660. const SuggestWord_t & tWord = tRes.m_dMatched[i];
  9661. tOut.PutString ( szResult + tWord.m_iNameOff );
  9662. if ( tArgs.m_bResultStats )
  9663. {
  9664. tOut.PutNumAsString ( tWord.m_iDistance );
  9665. tOut.PutNumAsString ( tWord.m_iDocs );
  9666. }
  9667. tOut.Commit();
  9668. }
  9669. }
  9670. tOut.Eof();
  9671. }
  9672. static void AddFieldDesc ( VectorLike & dOut, const CSphColumnInfo & tField )
  9673. {
  9674. if ( !dOut.MatchAdd ( tField.m_sName.cstr() ) )
  9675. return;
  9676. dOut.Add ( "text" );
  9677. StringBuilder_c sProperties ( " " );
  9678. DWORD uFlags = tField.m_uFieldFlags;
  9679. if ( uFlags & CSphColumnInfo::FIELD_INDEXED )
  9680. sProperties << "indexed";
  9681. if ( uFlags & CSphColumnInfo::FIELD_STORED )
  9682. sProperties << "stored";
  9683. dOut.Add ( sProperties.cstr () );
  9684. }
  9685. static void AddAttributeDesc ( VectorLike & dOut, const CSphColumnInfo & tAttr )
  9686. {
  9687. if ( sphIsInternalAttr ( tAttr ) )
  9688. return;
  9689. if ( dOut.MatchAdd ( tAttr.m_sName.cstr() ) )
  9690. {
  9691. if ( tAttr.m_eAttrType==SPH_ATTR_INTEGER && tAttr.m_tLocator.m_iBitCount!=ROWITEM_BITS && tAttr.m_tLocator.m_iBitCount>0 )
  9692. {
  9693. StringBuilder_c sName;
  9694. sName.Sprintf ( "%s:%d", sphTypeName ( tAttr.m_eAttrType ), tAttr.m_tLocator.m_iBitCount );
  9695. dOut.Add ( sName.cstr() );
  9696. } else
  9697. dOut.Add ( sphTypeName ( tAttr.m_eAttrType ) );
  9698. StringBuilder_c sProps(" ");
  9699. if ( tAttr.IsColumnar() )
  9700. sProps << "columnar";
  9701. if ( tAttr.m_uAttrFlags & CSphColumnInfo::ATTR_STORED )
  9702. sProps << "fast_fetch";
  9703. if ( tAttr.IsColumnar() && tAttr.m_eAttrType==SPH_ATTR_STRING && !(tAttr.m_uAttrFlags & CSphColumnInfo::ATTR_COLUMNAR_HASHES) )
  9704. sProps << "no_hash";
  9705. dOut.Add ( sProps.cstr() );
  9706. }
  9707. }
  9708. void DescribeLocalSchema ( VectorLike & dOut, const CSphSchema & tSchema, bool bIsTemplate )
  9709. {
  9710. // result set header packet
  9711. dOut.SetColNames ( { "Field", "Type", "Properties" } );
  9712. // id comes before fields
  9713. if ( !bIsTemplate )
  9714. {
  9715. assert ( tSchema.GetAttr(0).m_sName==sphGetDocidName() );
  9716. AddAttributeDesc ( dOut, tSchema.GetAttr(0) );
  9717. }
  9718. for ( int i = 0; i<tSchema.GetFieldsCount (); ++i )
  9719. AddFieldDesc ( dOut, tSchema.GetField(i) );
  9720. for ( int i = 1; i<tSchema.GetAttrsCount (); ++i )
  9721. AddAttributeDesc ( dOut, tSchema.GetAttr(i) );
  9722. }
  9723. void DescribeDistributedSchema ( VectorLike& dOut, const cDistributedIndexRefPtr_t& pDistr )
  9724. {
  9725. // result set header packet
  9726. dOut.SetColNames ( { "Agent", "Type" } );
  9727. for ( const auto & sIdx : pDistr->m_dLocal )
  9728. dOut.MatchTuplet( sIdx.cstr (), "local" );
  9729. ARRAY_CONSTFOREACH ( i, pDistr->m_dAgents )
  9730. {
  9731. MultiAgentDescRefPtr_c pMultiAgent = pDistr->m_dAgents[i];
  9732. const MultiAgentDesc_c & tMultiAgent = *pMultiAgent;
  9733. if ( tMultiAgent.IsHA () )
  9734. {
  9735. int iNumMultiAgents = tMultiAgent.GetLength();
  9736. for ( int j = 0; j < iNumMultiAgents; j++ )
  9737. {
  9738. const AgentDesc_t & tDesc = tMultiAgent[j];
  9739. StringBuilder_c sValue;
  9740. sValue << tDesc.GetMyUrl().cstr() << ":" << tDesc.m_sIndexes.cstr();
  9741. dOut.MatchTupletf ( sValue.cstr (), "%s_%d_mirror_%d",
  9742. tDesc.m_bBlackhole ? "blackhole" : "remote", i+1, j+1 );
  9743. }
  9744. } else
  9745. {
  9746. const AgentDesc_t & tDesc = tMultiAgent[0];
  9747. StringBuilder_c sValue;
  9748. sValue << tDesc.GetMyUrl ().cstr () << ":" << tDesc.m_sIndexes.cstr ();
  9749. dOut.MatchTupletf ( sValue.cstr (), "%s_%d", tDesc.m_bBlackhole ? "blackhole" : "remote", i+1 );
  9750. }
  9751. }
  9752. }
  9753. void HandleMysqlDescribe ( RowBuffer_i & tOut, const SqlStmt_t * pStmt )
  9754. {
  9755. auto & tStmt = *pStmt;
  9756. VectorLike dOut ( tStmt.m_sStringParam, 0 );
  9757. auto pServed = GetServed ( tStmt.m_sIndex );
  9758. if ( pServed )
  9759. {
  9760. // data
  9761. const CSphSchema *pSchema = &RIdx_c(pServed)->GetMatchSchema ();
  9762. bool bNeedInternal = false;
  9763. if ( tStmt.m_iIntParam==SqlInsert_t::TABLE ) // user wants internal schema instead
  9764. bNeedInternal = true;
  9765. if ( tStmt.m_dStringSubkeys.GetLength()==1 && tStmt.m_dStringSubkeys[0].EqN(".table") )
  9766. bNeedInternal = true;
  9767. if ( bNeedInternal && ServedDesc_t::IsMutable ( pServed ) )
  9768. {
  9769. RIdx_T<const RtIndex_i*> pRtIndex { pServed };
  9770. pSchema = &pRtIndex->GetInternalSchema();
  9771. }
  9772. const CSphSchema &tSchema = *pSchema;
  9773. assert ( pServed->m_eType==IndexType_e::TEMPLATE || tSchema.GetAttr(0).m_sName==sphGetDocidName() );
  9774. DescribeLocalSchema ( dOut, tSchema, pServed->m_eType==IndexType_e::TEMPLATE );
  9775. } else
  9776. {
  9777. auto pDistr = GetDistr ( tStmt.m_sIndex );
  9778. if ( !pDistr )
  9779. {
  9780. tOut.ErrorAbsent ( tStmt.m_sStmt, "no such index '%s'", tStmt.m_sIndex.cstr () );
  9781. return;
  9782. }
  9783. DescribeDistributedSchema ( dOut, pDistr );
  9784. }
  9785. tOut.DataTable ( dOut );
  9786. }
  9787. void HandleMysqlShowTables ( RowBuffer_i & tOut, const SqlStmt_t * pStmt )
  9788. {
  9789. // 0 local, 1 distributed, 2 rt, 3 template, 4 percolate, 5 unknown
  9790. static const char* sTypes[] = {"local", "distributed", "rt", "template", "percolate", "unknown"};
  9791. CSphVector<CSphNamedInt> dIndexes;
  9792. // collect local, rt, percolate
  9793. ServedSnap_t hLocal = g_pLocalIndexes->GetHash();
  9794. for ( const auto& tIt : *hLocal )
  9795. {
  9796. if ( !tIt.second )
  9797. continue;
  9798. switch ( tIt.second->m_eType )
  9799. {
  9800. case IndexType_e::PLAIN:
  9801. dIndexes.Add ( CSphNamedInt ( tIt.first, 0 ) );
  9802. break;
  9803. case IndexType_e::RT:
  9804. dIndexes.Add ( CSphNamedInt ( tIt.first, 2 ) );
  9805. break;
  9806. case IndexType_e::PERCOLATE:
  9807. dIndexes.Add ( CSphNamedInt ( tIt.first, 4 ) );
  9808. break;
  9809. case IndexType_e::TEMPLATE:
  9810. dIndexes.Add ( CSphNamedInt ( tIt.first, 3 ) );
  9811. break;
  9812. default:
  9813. dIndexes.Add ( CSphNamedInt ( tIt.first, 5 ) );
  9814. }
  9815. }
  9816. // collect distributed
  9817. assert ( g_pDistIndexes );
  9818. auto pDistSnapshot = g_pDistIndexes->GetHash();
  9819. for ( auto& tIt : *pDistSnapshot )
  9820. // no need to check distr's it, iterating guarantees index existance.
  9821. dIndexes.Add ( CSphNamedInt ( tIt.first, 1 ) );
  9822. dIndexes.Sort ( Lesser ([] ( const CSphNamedInt & a, const CSphNamedInt & b)
  9823. { return strcasecmp ( a.first.cstr (), b.first.cstr () )<0; }));
  9824. // output the results
  9825. VectorLike dTable ( pStmt->m_sStringParam, { "Index", "Type" } );
  9826. for ( auto& dPair : dIndexes )
  9827. dTable.MatchTuplet( dPair.first.cstr (), sTypes[dPair.second] );
  9828. tOut.DataTable ( dTable );
  9829. }
  9830. template <typename T, typename GETNAME>
  9831. static bool CheckAttrs ( const VecTraits_T<T> & dAttrs, GETNAME && fnGetName, CSphString & sError )
  9832. {
  9833. ARRAY_FOREACH ( i, dAttrs )
  9834. {
  9835. const CSphString & sName = fnGetName(dAttrs[i]);
  9836. if ( CSphSchema::IsReserved ( sName.cstr() ) || sphIsInternalAttr ( sName ) )
  9837. {
  9838. sError.SetSprintf ( "attribute name '%s' is a reserved keyword", sName.cstr() );
  9839. return false;
  9840. }
  9841. for ( int j = i+1; j < dAttrs.GetLength(); j++ )
  9842. if ( fnGetName(dAttrs[j])==sName )
  9843. {
  9844. sError.SetSprintf ( "duplicate attribute name '%s'", sName.cstr() );
  9845. return false;
  9846. }
  9847. }
  9848. return true;
  9849. }
  9850. static bool CheckExistingTables ( const SqlStmt_t & tStmt, CSphString & sError )
  9851. {
  9852. if ( g_pLocalIndexes->Contains ( tStmt.m_sIndex ) || g_pDistIndexes->Contains ( tStmt.m_sIndex ) )
  9853. {
  9854. if ( tStmt.m_tCreateTable.m_bIfNotExists )
  9855. return true;
  9856. else
  9857. {
  9858. sError.SetSprintf ( "index '%s' already exists", tStmt.m_sIndex.cstr() );
  9859. return false;
  9860. }
  9861. }
  9862. if ( CSphSchema::IsReserved ( tStmt.m_sIndex.cstr() ) )
  9863. {
  9864. sError.SetSprintf ( "'%s' is a reserved keyword", tStmt.m_sIndex.cstr() );
  9865. return false;
  9866. }
  9867. return true;
  9868. }
  9869. static bool CheckCreateTable ( const SqlStmt_t & tStmt, CSphString & sError )
  9870. {
  9871. if ( !CheckExistingTables ( tStmt, sError ) )
  9872. return false;
  9873. if ( !CheckAttrs ( tStmt.m_tCreateTable.m_dAttrs, []( const CreateTableAttr_t & tAttr ) { return tAttr.m_tAttr.m_sName; }, sError ) )
  9874. return false;
  9875. if ( !CheckAttrs ( tStmt.m_tCreateTable.m_dFields, []( const CSphColumnInfo & tAttr ) { return tAttr.m_sName; }, sError ) )
  9876. return false;
  9877. // cross-checks attrs and fields
  9878. for ( const auto & i : tStmt.m_tCreateTable.m_dAttrs )
  9879. for ( const auto & j : tStmt.m_tCreateTable.m_dFields )
  9880. if ( i.m_tAttr.m_sName==j.m_sName && i.m_tAttr.m_eAttrType!=SPH_ATTR_STRING )
  9881. {
  9882. sError.SetSprintf ( "duplicate attribute name '%s'", i.m_tAttr.m_sName.cstr() );
  9883. return false;
  9884. }
  9885. return true;
  9886. }
  9887. static CSphString ConcatWarnings ( const StrVec_t & dWarnings )
  9888. {
  9889. StringBuilder_c sRes ( "; " );
  9890. for ( const auto & i : dWarnings )
  9891. sRes << i;
  9892. return sRes.cstr();
  9893. }
  9894. static void HandleMysqlCreateTable ( RowBuffer_i & tOut, const SqlStmt_t & tStmt, CSphString & sWarning )
  9895. {
  9896. SearchFailuresLog_c dErrors;
  9897. CSphString sError;
  9898. if ( !sphCheckWeCanModify ( tStmt.m_sStmt, tOut ) )
  9899. return;
  9900. if ( !IsConfigless() )
  9901. {
  9902. sError = "CREATE TABLE requires data_dir to be set in the config file";
  9903. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9904. return;
  9905. }
  9906. if ( !CheckCreateTable ( tStmt, sError ) )
  9907. {
  9908. sError.SetSprintf ( "index '%s': CREATE TABLE failed: %s", tStmt.m_sIndex.cstr(), sError.cstr() );
  9909. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9910. return;
  9911. }
  9912. StrVec_t dWarnings;
  9913. bool bCreatedOk = CreateNewIndexConfigless ( tStmt.m_sIndex, tStmt.m_tCreateTable, dWarnings, sError );
  9914. sWarning = ConcatWarnings(dWarnings);
  9915. if ( !bCreatedOk )
  9916. {
  9917. sError.SetSprintf ( "error adding index '%s': %s", tStmt.m_sIndex.cstr(), sError.cstr() );
  9918. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9919. return;
  9920. }
  9921. tOut.Ok ( 0, dWarnings.GetLength() );
  9922. }
  9923. static const CSphSchema & GetSchemaForCreateTable ( const CSphIndex * pIndex )
  9924. {
  9925. assert ( pIndex );
  9926. assert ( pIndex->IsRT() || pIndex->IsPQ() );
  9927. return ((const RtIndex_i*)pIndex)->GetInternalSchema();
  9928. }
  9929. static CSphString BuildCreateTableRt ( const CSphString & sName, const CSphIndex * pIndex, const CSphSchema & tSchema )
  9930. {
  9931. assert(pIndex);
  9932. CSphString sCreateTable = BuildCreateTable ( sName, pIndex, tSchema );
  9933. return sCreateTable;
  9934. }
  9935. static void HandleMysqlCreateTableLike ( RowBuffer_i & tOut, const SqlStmt_t & tStmt, CSphString & sWarning )
  9936. {
  9937. SearchFailuresLog_c dErrors;
  9938. CSphString sError;
  9939. if ( !IsConfigless() )
  9940. {
  9941. sError = "CREATE TABLE requires data_dir to be set in the config file";
  9942. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9943. return;
  9944. }
  9945. if ( !CheckExistingTables ( tStmt, sError ) )
  9946. {
  9947. sError.SetSprintf ( "index '%s': CREATE TABLE failed: %s", tStmt.m_sIndex.cstr(), sError.cstr() );
  9948. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9949. return;
  9950. }
  9951. const CSphString & sLike = tStmt.m_tCreateTable.m_sLike;
  9952. CSphString sCreateTable;
  9953. switch ( IndexIsServed ( sLike ) )
  9954. {
  9955. case RunIdx_e::NOTSERVED:
  9956. sError.SetSprintf ( "index '%s': CREATE TABLE LIKE failed: no index '%s' found", tStmt.m_sIndex.cstr(), sLike.cstr() );
  9957. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9958. return;
  9959. case RunIdx_e::LOCAL:
  9960. {
  9961. auto pServed = GetServed ( sLike );
  9962. assert ( pServed );
  9963. if ( !ServedDesc_t::IsMutable ( pServed ) )
  9964. {
  9965. tOut.ErrorAbsent ( tStmt.m_sStmt, "index '%s' is not real-time or percolate", sError.cstr() );
  9966. return;
  9967. }
  9968. RIdx_c pIdx { pServed };
  9969. sCreateTable = BuildCreateTableRt ( tStmt.m_sIndex, pIdx, GetSchemaForCreateTable ( pIdx ) );
  9970. break;
  9971. }
  9972. case RunIdx_e::DISTR:
  9973. {
  9974. auto pDist = GetDistr ( sLike );
  9975. sCreateTable = BuildCreateTableDistr ( tStmt.m_sIndex, *pDist );
  9976. }
  9977. default: break;
  9978. };
  9979. CSphVector<SqlStmt_t> dCreateTableStmts;
  9980. if ( !ParseDdl ( sCreateTable.cstr(), sCreateTable.Length(), dCreateTableStmts, sError ) )
  9981. {
  9982. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  9983. return;
  9984. }
  9985. if ( dCreateTableStmts.GetLength()!=1 )
  9986. {
  9987. tOut.Error ( tStmt.m_sStmt, "CREATE TABLE LIKE failed" );
  9988. return;
  9989. }
  9990. SqlStmt_t & tNewCreateTable = dCreateTableStmts[0];
  9991. tNewCreateTable.m_tCreateTable.m_bIfNotExists = tStmt.m_tCreateTable.m_bIfNotExists;
  9992. HandleMysqlCreateTable ( tOut, tNewCreateTable, sWarning );
  9993. }
  9994. static void HandleMysqlDropTable ( RowBuffer_i & tOut, const SqlStmt_t & tStmt )
  9995. {
  9996. if ( !sphCheckWeCanModify ( tStmt.m_sStmt, tOut ) )
  9997. return;
  9998. CSphString sError;
  9999. if ( !IsConfigless() )
  10000. {
  10001. sError = "DROP TABLE requires data_dir to be set in the config file";
  10002. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  10003. return;
  10004. }
  10005. bool bDropped = DropIndexInt ( tStmt.m_sIndex.cstr(), tStmt.m_bIfExists, sError );
  10006. sphLogDebug ( "dropped table %s, ok %d, error %s", tStmt.m_sIndex.cstr(), (int)bDropped, sError.scstr() ); // FIXME!!! remove
  10007. if ( !bDropped )
  10008. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  10009. else
  10010. tOut.Ok();
  10011. }
  10012. void HandleMysqlShowCreateTable ( RowBuffer_i & tOut, const SqlStmt_t & tStmt )
  10013. {
  10014. auto pServed = GetServed ( tStmt.m_sIndex );
  10015. auto pDist = GetDistr ( tStmt.m_sIndex );
  10016. if ( !pServed && !pDist )
  10017. {
  10018. tOut.ErrorAbsent ( tStmt.m_sStmt, "no such index '%s'", tStmt.m_sIndex.cstr () );
  10019. return;
  10020. }
  10021. if ( pServed && !ServedDesc_t::IsMutable ( pServed ) )
  10022. {
  10023. tOut.ErrorAbsent ( tStmt.m_sStmt, "index '%s' is not real-time or percolate", tStmt.m_sIndex.cstr () );
  10024. return;
  10025. }
  10026. // result set header packet
  10027. tOut.HeadTuplet ( "Table", "Create Table" );
  10028. CSphString sCreateTable;
  10029. if ( pServed )
  10030. {
  10031. RIdx_c pIdx { pServed };
  10032. sCreateTable = BuildCreateTableRt ( pIdx->GetName(), pIdx, GetSchemaForCreateTable ( pIdx ) );
  10033. } else
  10034. sCreateTable = BuildCreateTableDistr ( tStmt.m_sIndex, *pDist );
  10035. tOut.DataTuplet ( tStmt.m_sIndex.cstr(), sCreateTable.cstr() );
  10036. tOut.Eof();
  10037. }
  10038. // MySQL Workbench (and maybe other clients) crashes without it
  10039. void HandleMysqlShowDatabases ( RowBuffer_i & tOut, SqlStmt_t & )
  10040. {
  10041. tOut.HeadBegin ( 1 );
  10042. tOut.HeadColumn ( "Databases" );
  10043. tOut.HeadEnd();
  10044. tOut.PutString ( g_sDbName );
  10045. tOut.Commit ();
  10046. tOut.Eof();
  10047. }
  10048. void HandleMysqlShowPlugins ( RowBuffer_i & tOut, SqlStmt_t & )
  10049. {
  10050. CSphVector<PluginInfo_t> dPlugins;
  10051. sphPluginList ( dPlugins );
  10052. tOut.HeadBegin ( 5 );
  10053. tOut.HeadColumn ( "Type" );
  10054. tOut.HeadColumn ( "Name" );
  10055. tOut.HeadColumn ( "Library" );
  10056. tOut.HeadColumn ( "Users" );
  10057. tOut.HeadColumn ( "Extra" );
  10058. tOut.HeadEnd();
  10059. ARRAY_FOREACH ( i, dPlugins )
  10060. {
  10061. const PluginInfo_t & p = dPlugins[i];
  10062. tOut.PutString ( g_dPluginTypes[p.m_eType] );
  10063. tOut.PutString ( p.m_sName );
  10064. tOut.PutString ( p.m_sLib );
  10065. tOut.PutNumAsString ( p.m_iUsers );
  10066. tOut.PutString ( p.m_sExtra );
  10067. tOut.Commit();
  10068. }
  10069. tOut.Eof();
  10070. }
  10071. enum ThreadInfoFormat_e
  10072. {
  10073. THD_FORMAT_NATIVE,
  10074. THD_FORMAT_SPHINXQL
  10075. };
  10076. static Str_t FormatInfo ( const PublicThreadDesc_t & tThd, ThreadInfoFormat_e eFmt, QuotationEscapedBuilder & tBuf )
  10077. {
  10078. if ( tThd.m_pQuery && eFmt==THD_FORMAT_SPHINXQL && tThd.m_eProto!=Proto_e::MYSQL41 )
  10079. {
  10080. bool bGotQuery = false;
  10081. if ( tThd.m_pQuery )
  10082. {
  10083. tBuf.Clear();
  10084. FormatSphinxql ( *tThd.m_pQuery, 0, tBuf );
  10085. bGotQuery = true;
  10086. }
  10087. // query might be removed prior to lock then go to common path
  10088. if ( bGotQuery )
  10089. return (Str_t)tBuf;
  10090. }
  10091. if ( tThd.m_sDescription.IsEmpty () && tThd.m_szCommand )
  10092. return FromSz ( tThd.m_szCommand );
  10093. else
  10094. return (Str_t)tThd.m_sDescription;
  10095. }
  10096. void HandleMysqlShowThreads ( RowBuffer_i & tOut, const SqlStmt_t * pStmt )
  10097. {
  10098. ThreadInfoFormat_e eFmt = THD_FORMAT_NATIVE;
  10099. bool bAll = false;
  10100. int iCols = -1;
  10101. if ( pStmt )
  10102. {
  10103. if ( pStmt->m_sThreadFormat == "sphinxql" )
  10104. eFmt = THD_FORMAT_SPHINXQL;
  10105. else if ( pStmt->m_sThreadFormat == "all" )
  10106. bAll = true;
  10107. iCols = pStmt->m_iThreadsCols;
  10108. }
  10109. tOut.HeadBegin ( bAll ? 15 : 14 ); // 15 with chain
  10110. tOut.HeadColumn ( "Tid", MYSQL_COL_LONG );
  10111. tOut.HeadColumn ( "Name" );
  10112. tOut.HeadColumn ( "Proto" );
  10113. tOut.HeadColumn ( "State" );
  10114. tOut.HeadColumn ( "Host" );
  10115. tOut.HeadColumn ( "ConnID", MYSQL_COL_LONGLONG );
  10116. tOut.HeadColumn ( "Time", MYSQL_COL_FLOAT );
  10117. tOut.HeadColumn ( "Work time" );
  10118. tOut.HeadColumn ( "Work time CPU" );
  10119. tOut.HeadColumn ( "Thd efficiency", MYSQL_COL_FLOAT);
  10120. tOut.HeadColumn ( "Jobs done", MYSQL_COL_LONG );
  10121. tOut.HeadColumn ( "Last job took" );
  10122. tOut.HeadColumn ( "In idle" );
  10123. if ( bAll )
  10124. tOut.HeadColumn ( "Chain" );
  10125. tOut.HeadColumn ( "Info" );
  10126. if (!tOut.HeadEnd())
  10127. return;
  10128. QuotationEscapedBuilder tBuf;
  10129. // sphLogDebug ( "^^ Show threads. Current info is %p", GetTaskInfo () );
  10130. CSphSwapVector<PublicThreadDesc_t> dFinal;
  10131. Threads::IterateActive([&dFinal, iCols] ( Threads::LowThreadDesc_t * pThread ){
  10132. if ( pThread )
  10133. dFinal.Add ( GatherPublicThreadInfo ( pThread, iCols ) );
  10134. });
  10135. for ( const auto & dThd : dFinal )
  10136. {
  10137. if ( !bAll && dThd.m_eTaskState==TaskState_e::UNKNOWN )
  10138. continue;
  10139. tOut.PutNumAsString ( dThd.m_iThreadID );
  10140. tOut.PutString ( dThd.m_sThreadName );
  10141. tOut.PutString ( dThd.m_sProto );
  10142. tOut.PutString ( TaskStateName ( dThd.m_eTaskState ) );
  10143. tOut.PutString ( dThd.m_sClientName ); // Host
  10144. tOut.PutNumAsString ( dThd.m_iConnID ); // ConnID
  10145. int64_t tmNow = sphMicroTimer (); // short-term cache
  10146. tOut.PutMicrosec ( tmNow-dThd.m_tmStart.get_value_or(tmNow) ); // time
  10147. tOut.PutTimeAsString ( dThd.m_tmTotalWorkedTimeUS ); // work time
  10148. tOut.PutTimeAsString ( dThd.m_tmTotalWorkedCPUTimeUS ); // work CPU time
  10149. tOut.PutPercentAsString ( dThd.m_tmTotalWorkedCPUTimeUS, dThd.m_tmTotalWorkedTimeUS ); // work CPU time %
  10150. tOut.PutNumAsString ( dThd.m_iTotalJobsDone ); // jobs done
  10151. if ( dThd.m_tmLastJobStartTimeUS<0 )
  10152. {
  10153. tOut.PutString ( "-" ); // last job take
  10154. tOut.PutString ( "-" ); // idle for
  10155. } else if ( dThd.m_tmLastJobDoneTimeUS<0 )
  10156. {
  10157. tOut.PutTimeAsString ( tmNow-dThd.m_tmLastJobStartTimeUS ); // last job take
  10158. tOut.PutString ( "No (working)" ); // idle for
  10159. } else
  10160. {
  10161. tOut.PutTimeAsString ( dThd.m_tmLastJobDoneTimeUS-dThd.m_tmLastJobStartTimeUS ); // last job take
  10162. tOut.PutTimestampAsString ( dThd.m_tmLastJobDoneTimeUS ); // idle for
  10163. }
  10164. if ( bAll )
  10165. tOut.PutString ( dThd.m_sChain ); // Chain
  10166. auto sInfo = FormatInfo ( dThd, eFmt, tBuf );
  10167. if ( iCols >= 0 && iCols < sInfo.second )
  10168. sInfo.second = iCols;
  10169. tOut.PutString ( sInfo ); // Info m_pTaskInfo
  10170. if ( !tOut.Commit () )
  10171. break;
  10172. }
  10173. tOut.Eof();
  10174. }
  10175. // helper; available only via 'select ... from @@system.sessions...'
  10176. void HandleShowSessions ( RowBuffer_i& tOut, const SqlStmt_t* pStmt )
  10177. {
  10178. ThreadInfoFormat_e eFmt = THD_FORMAT_NATIVE;
  10179. bool bAll = false;
  10180. int iCols = -1;
  10181. if ( pStmt )
  10182. {
  10183. if ( pStmt->m_sThreadFormat == "sphinxql" )
  10184. eFmt = THD_FORMAT_SPHINXQL;
  10185. else if ( pStmt->m_sThreadFormat == "all" )
  10186. bAll = true;
  10187. iCols = pStmt->m_iThreadsCols;
  10188. }
  10189. tOut.HeadBegin ( bAll ? 6 : 5 ); // 6 with chain
  10190. tOut.HeadColumn ( "Proto" );
  10191. tOut.HeadColumn ( "State" );
  10192. tOut.HeadColumn ( "Host" );
  10193. tOut.HeadColumn ( "ConnID", MYSQL_COL_LONGLONG );
  10194. tOut.HeadColumn ( "Killed" );
  10195. if ( bAll )
  10196. tOut.HeadColumn ( "Chain" );
  10197. tOut.HeadColumn ( "Last cmd" );
  10198. if ( !tOut.HeadEnd() )
  10199. return;
  10200. QuotationEscapedBuilder tBuf;
  10201. // sphLogDebug ( "^^ Show threads. Current info is %p", GetTaskInfo () );
  10202. CSphSwapVector<PublicThreadDesc_t> dFinal;
  10203. IterateTasks ( [&dFinal, iCols] ( ClientTaskInfo_t* pTask ) {
  10204. if ( pTask )
  10205. {
  10206. PublicThreadDesc_t& tDesc = dFinal.Add();
  10207. tDesc.m_iDescriptionLimit = iCols;
  10208. GatherPublicTaskInfo ( tDesc, pTask );
  10209. }
  10210. } );
  10211. for ( const auto& dThd : dFinal )
  10212. {
  10213. if ( !bAll && dThd.m_eTaskState == TaskState_e::UNKNOWN )
  10214. continue;
  10215. tOut.PutString ( dThd.m_sProto );
  10216. tOut.PutString ( TaskStateName ( dThd.m_eTaskState ) );
  10217. tOut.PutString ( dThd.m_sClientName ); // Host
  10218. tOut.PutNumAsString ( dThd.m_iConnID ); // ConnID
  10219. tOut.PutNumAsString ( dThd.m_bKilled ? 1 : 0);
  10220. if ( bAll )
  10221. tOut.PutString ( dThd.m_sChain ); // Chain
  10222. auto sInfo = FormatInfo ( dThd, eFmt, tBuf );
  10223. if ( iCols >= 0 && iCols < sInfo.second )
  10224. sInfo.second = iCols;
  10225. tOut.PutString ( sInfo ); // Info m_pTaskInfo
  10226. if ( !tOut.Commit() )
  10227. break;
  10228. }
  10229. tOut.Eof();
  10230. }
  10231. void HandleMysqlFlushHostnames ( RowBuffer_i & tOut )
  10232. {
  10233. SmallStringHash_T<DWORD> hHosts;
  10234. // Collect all urls from all distr indexes
  10235. assert ( g_pDistIndexes );
  10236. auto pDistSnapshot = g_pDistIndexes->GetHash();
  10237. for ( auto& tIt : *pDistSnapshot )
  10238. tIt.second->ForEveryHost ( [&] ( AgentDesc_t& tDesc ) {
  10239. if ( tDesc.m_bNeedResolve )
  10240. hHosts.Add ( tDesc.m_uAddr, tDesc.m_sAddr );
  10241. });
  10242. for ( auto tHost : hHosts )
  10243. {
  10244. DWORD uRenew = sphGetAddress ( tHost.first.cstr() );
  10245. if ( uRenew )
  10246. tHost.second = uRenew;
  10247. }
  10248. // copy back renew hosts to distributed agents.
  10249. // case when distr index list changed between collecting urls and applying them
  10250. // is safe, since we are iterating over the list again, and also apply
  10251. // only existing hosts.
  10252. for ( auto& tIt : *pDistSnapshot )
  10253. tIt.second->ForEveryHost ( [&] ( AgentDesc_t& tDesc ) {
  10254. if ( tDesc.m_bNeedResolve )
  10255. {
  10256. DWORD * pRenew = hHosts ( tDesc.m_sAddr );
  10257. if ( pRenew && *pRenew )
  10258. tDesc.m_uAddr = *pRenew;
  10259. }
  10260. });
  10261. tOut.Ok ( hHosts.GetLength() );
  10262. }
  10263. void HandleMysqlFlushLogs ( RowBuffer_i & tOut )
  10264. {
  10265. sigusr1(1);
  10266. tOut.Ok ();
  10267. }
  10268. void HandleMysqlReloadIndexes ( RowBuffer_i & tOut )
  10269. {
  10270. g_bReloadForced = true;
  10271. sighup(1);
  10272. tOut.Ok ();
  10273. }
  10274. /////////////////////////////////////////////////////////////////////////////
  10275. // user variables these send from master to agents
  10276. /////////////////////////////////////////////////////////////////////////////
  10277. class UVarRequestBuilder_c : public RequestBuilder_i
  10278. {
  10279. public:
  10280. UVarRequestBuilder_c ( const char * sName, const CSphVector<SphAttr_t> &dSetValues )
  10281. : m_sName ( sName )
  10282. {
  10283. m_iUserVars = dSetValues.GetLength ();
  10284. m_dBuf.Reset ( m_iUserVars * sizeof ( dSetValues[0] ) + 129 );
  10285. // 129 above is the safe gap for VLB delta encoding 64-bits ints.
  10286. // If we have 1st value 0x8000`0000`0000`0000 - it will occupy 10 bytes VLB,
  10287. // then up to 127 values 0x0100.. - each will occupy 9 bytes VLB,
  10288. // deltas 0x00XX.. takes 8 bytes or less. So, 2+127 bytes gap is enough to cover worst possible case
  10289. // (since 0x80.. + 127 * 0x01.. produce 0xFF.. num, any other delta >0x01.. impossible, since
  10290. // it will cause overflow, and deltals <0x01.. occupy <=8 bytes each).
  10291. SphAttr_t iLast = 0;
  10292. BYTE * pCur = m_dBuf.Begin ();
  10293. for ( const auto &dValue : dSetValues )
  10294. {
  10295. pCur += ZipToPtrLE ( pCur, dValue - iLast );
  10296. iLast = dValue;
  10297. }
  10298. m_iLength = pCur-m_dBuf.Begin();
  10299. }
  10300. void BuildRequest ( const AgentConn_t &, ISphOutputBuffer & tOut ) const final
  10301. {
  10302. // API header
  10303. auto tHdr = APIHeader ( tOut, SEARCHD_COMMAND_UVAR, VER_COMMAND_UVAR );
  10304. tOut.SendString ( m_sName.cstr() );
  10305. tOut.SendInt ( m_iUserVars );
  10306. tOut.SendArray ( m_dBuf, m_iLength );
  10307. }
  10308. CSphString m_sName;
  10309. CSphFixedVector<BYTE> m_dBuf { 0 };
  10310. int m_iUserVars = 0;
  10311. int m_iLength = 0;
  10312. };
  10313. class UVarReplyParser_c : public ReplyParser_i
  10314. {
  10315. public:
  10316. bool ParseReply ( MemInputBuffer_c & tReq, AgentConn_t & ) const final
  10317. {
  10318. // error got handled at call site
  10319. bool bOk = ( tReq.GetByte()==1 );
  10320. return bOk;
  10321. }
  10322. };
  10323. static bool SendUserVar ( const char * sIndex, const char * sUserVarName, CSphVector<SphAttr_t> & dSetValues, CSphString & sError )
  10324. {
  10325. auto pIndex = GetDistr ( sIndex );
  10326. if ( !pIndex )
  10327. {
  10328. sError.SetSprintf ( "unknown index '%s' in Set statement", sIndex );
  10329. return false;
  10330. }
  10331. VecRefPtrsAgentConn_t dAgents;
  10332. pIndex->GetAllHosts ( dAgents );
  10333. bool bGotLocal = !pIndex->m_dLocal.IsEmpty();
  10334. // FIXME!!! warn on missed agents
  10335. if ( dAgents.IsEmpty() && !bGotLocal )
  10336. return true;
  10337. dSetValues.Uniq();
  10338. // FIXME!!! warn on empty agents
  10339. // connect to remote agents and query them
  10340. if ( !dAgents.IsEmpty() )
  10341. {
  10342. UVarRequestBuilder_c tReqBuilder ( sUserVarName, dSetValues );
  10343. UVarReplyParser_c tParser;
  10344. PerformRemoteTasks ( dAgents, &tReqBuilder, &tParser );
  10345. }
  10346. // should be at the end due to swap of dSetValues values
  10347. if ( bGotLocal )
  10348. SetLocalUserVar ( sUserVarName, dSetValues );
  10349. return true;
  10350. }
  10351. void HandleCommandUserVar ( ISphOutputBuffer & tOut, WORD uVer, InputBuffer_c & tReq )
  10352. {
  10353. if ( !CheckCommandVersion ( uVer, VER_COMMAND_UVAR, tOut ) )
  10354. return;
  10355. CSphString sUserVar = tReq.GetString();
  10356. int iCount = tReq.GetInt();
  10357. CSphVector<SphAttr_t> dUserVar ( iCount );
  10358. int iLength = tReq.GetInt();
  10359. CSphFixedVector<BYTE> dBuf ( iLength );
  10360. tReq.GetBytes ( dBuf.Begin(), iLength );
  10361. if ( tReq.GetError() )
  10362. {
  10363. SendErrorReply ( tOut, "invalid or truncated request" );
  10364. return;
  10365. }
  10366. SphAttr_t iLast = 0;
  10367. const BYTE * pCur = dBuf.Begin();
  10368. ARRAY_FOREACH ( i, dUserVar )
  10369. {
  10370. auto iDelta = UnzipValueLE<int64_t> ( [&pCur]() mutable { return *pCur++; } );
  10371. assert ( iDelta>0 );
  10372. iLast += iDelta;
  10373. dUserVar[i] = iLast;
  10374. }
  10375. SetLocalUserVar ( sUserVar, dUserVar );
  10376. auto tReply = APIAnswer ( tOut, VER_COMMAND_UVAR );
  10377. tOut.SendInt ( 1 );
  10378. }
  10379. /////////////////////////////////////////////////////////////////////////////
  10380. // SMART UPDATES HANDLER
  10381. /////////////////////////////////////////////////////////////////////////////
  10382. SphinxqlReplyParser_c::SphinxqlReplyParser_c ( int * pUpd, int * pWarns )
  10383. : m_pUpdated ( pUpd )
  10384. , m_pWarns ( pWarns )
  10385. {}
  10386. // fixme! reuse code from sphinxql, leave only refs here
  10387. bool SphinxqlReplyParser_c::ParseReply ( MemInputBuffer_c & tReq, AgentConn_t & ) const
  10388. {
  10389. DWORD uSize = ( tReq.GetLSBDword() & 0x00ffffff ) - 1;
  10390. BYTE uCommand = tReq.GetByte();
  10391. if ( uCommand==0 ) // ok packet
  10392. {
  10393. *m_pUpdated += MysqlUnpack ( tReq, &uSize );
  10394. MysqlUnpack ( tReq, &uSize ); ///< int Insert_id (don't used).
  10395. auto uWarnStatus = tReq.GetLSBDword ();
  10396. *m_pWarns += ( uWarnStatus >> 16 ) & 0xFFFF; ///< num of warnings
  10397. uSize -= 4;
  10398. if ( uSize )
  10399. tReq.GetRawString ( uSize );
  10400. return true;
  10401. }
  10402. if ( uCommand==0xff ) // error packet
  10403. {
  10404. tReq.GetByte();
  10405. tReq.GetByte(); ///< num of errors (2 bytes), we don't use it for now.
  10406. uSize -= 2;
  10407. if ( uSize )
  10408. tReq.GetRawString ( uSize );
  10409. }
  10410. return false;
  10411. }
  10412. SphinxqlRequestBuilder_c::SphinxqlRequestBuilder_c ( Str_t sQuery, const SqlStmt_t & tStmt )
  10413. : m_sBegin { sQuery.first, tStmt.m_iListStart }
  10414. , m_sEnd ( sQuery.first + tStmt.m_iListEnd, sQuery.second - tStmt.m_iListEnd )
  10415. {
  10416. }
  10417. void SphinxqlRequestBuilder_c::BuildRequest ( const AgentConn_t & tAgent, ISphOutputBuffer & tOut ) const
  10418. {
  10419. const char* sIndexes = tAgent.m_tDesc.m_sIndexes.cstr();
  10420. // API header
  10421. auto tHdr = APIHeader ( tOut, SEARCHD_COMMAND_SPHINXQL, VER_COMMAND_SPHINXQL );
  10422. APIBlob_c dWrapper ( tOut ); // sphinxql wrapped twice, so one more length need to be written.
  10423. tOut.SendBytes ( m_sBegin );
  10424. tOut.SendBytes ( sIndexes );
  10425. tOut.SendBytes ( m_sEnd );
  10426. }
  10427. //////////////////////////////////////////////////////////////////////////
  10428. static void DoExtendedUpdate ( const SqlStmt_t & tStmt, const CSphString & sIndex, const char * sDistributed,
  10429. bool bBlobUpdate, int & iSuccesses, int & iUpdated, SearchFailuresLog_c & dFails, CSphString & sWarning,
  10430. const cServedIndexRefPtr_c & pServed )
  10431. {
  10432. CSphString sError;
  10433. // checks
  10434. if ( !pServed )
  10435. {
  10436. dFails.Submit ( sIndex, sDistributed, "index not available" );
  10437. return;
  10438. }
  10439. if ( !CheckIndexCluster ( sIndex, *pServed, tStmt.m_sCluster, IsHttpStmt ( tStmt ), sError ) )
  10440. {
  10441. dFails.Submit ( sIndex, sDistributed, sError.cstr() );
  10442. return;
  10443. }
  10444. RtAccum_t tAcc;
  10445. ReplicationCommand_t * pCmd = tAcc.AddCommand ( tStmt.m_bJson ? ReplicationCommand_e::UPDATE_JSON : ReplicationCommand_e::UPDATE_QL, sIndex, tStmt.m_sCluster );
  10446. assert ( pCmd );
  10447. pCmd->m_pUpdateAPI = tStmt.AttrUpdatePtr();
  10448. pCmd->m_bBlobUpdate = bBlobUpdate;
  10449. pCmd->m_pUpdateCond = &tStmt.m_tQuery;
  10450. HandleCmdReplicate ( tAcc, sError, sWarning, iUpdated );
  10451. if ( sError.Length() )
  10452. {
  10453. dFails.Submit ( sIndex, sDistributed, sError.cstr() );
  10454. return;
  10455. }
  10456. iSuccesses++;
  10457. }
  10458. bool HandleUpdateAPI ( AttrUpdateArgs& tArgs, CSphIndex* pIndex, int& iUpdate )
  10459. {
  10460. bool bCritical = false;
  10461. iUpdate = pIndex->UpdateAttributes ( tArgs.m_pUpdate, bCritical, *tArgs.m_pError, *tArgs.m_pWarning );
  10462. return !bCritical;
  10463. }
  10464. namespace {
  10465. DocsCollector_c InitUpdate( AttrUpdateArgs& tArgs, const cServedIndexRefPtr_c& pDesc )
  10466. {
  10467. DocsCollector_c tCollector ( *tArgs.m_pQuery, tArgs.m_bJson, *tArgs.m_pIndexName, pDesc, tArgs.m_pError );
  10468. AttrUpdateSharedPtr_t& pUpdate = tArgs.m_pUpdate;
  10469. pUpdate->m_bReusable = false;
  10470. pUpdate->m_bIgnoreNonexistent = tArgs.m_pQuery->m_bIgnoreNonexistent;
  10471. pUpdate->m_bStrict = tArgs.m_pQuery->m_bStrict;
  10472. return tCollector;
  10473. }
  10474. template<typename RWLOCKED>
  10475. void DoUpdate( DocsCollector_c& tCollector, AttrUpdateArgs& tArgs, RWLOCKED& rwLocked )
  10476. {
  10477. AttrUpdateSharedPtr_t& pUpdate = tArgs.m_pUpdate;
  10478. while ( tCollector.GetValuesChunk ( pUpdate->m_dDocids, tArgs.m_pQuery->m_iMaxMatches ) )
  10479. {
  10480. int iChanged = 0;
  10481. Verify ( HandleUpdateAPI ( tArgs, rwLocked, iChanged ) ); // fixme! handle this
  10482. tArgs.m_iAffected += iChanged;
  10483. }
  10484. }
  10485. void UpdateWlocked ( AttrUpdateArgs& tArgs, const cServedIndexRefPtr_c& pDesc, int& iUpdated )
  10486. {
  10487. // short-living r-lock m.b. acquired and released by collector when running query
  10488. auto tCollector = InitUpdate ( tArgs, pDesc );
  10489. WIdx_c wLocked { pDesc }; // exclusive lock for process of update. Note, between collecting and updating m.b. race! To eliminate it, need to trace index generation and recollect if it changed.
  10490. DoUpdate ( tCollector, tArgs, wLocked );
  10491. }
  10492. void UpdateRlocked ( AttrUpdateArgs& tArgs, const cServedIndexRefPtr_c& pDesc, int& iUpdated)
  10493. {
  10494. // wide r-lock over whole update. r-locks acquired by collector should be re-enterable.
  10495. RWIdx_c rLocked { pDesc };
  10496. auto tCollector = InitUpdate ( tArgs, pDesc );
  10497. DoUpdate ( tCollector, tArgs, rLocked );
  10498. }
  10499. } // unnamed namespace
  10500. void HandleMySqlExtendedUpdate ( AttrUpdateArgs& tArgs, const cServedIndexRefPtr_c& pDesc, int& iUpdated, bool bNeedWlock )
  10501. {
  10502. return bNeedWlock ? UpdateWlocked ( tArgs, pDesc, iUpdated ) : UpdateRlocked ( tArgs, pDesc, iUpdated );
  10503. }
  10504. void sphHandleMysqlUpdate ( StmtErrorReporter_i & tOut, const SqlStmt_t & tStmt, Str_t sQuery )
  10505. {
  10506. if ( !sphCheckWeCanModify ( tOut ) )
  10507. return;
  10508. auto* pSession = session::GetClientSession();
  10509. pSession->FreezeLastMeta();
  10510. auto& sWarning = pSession->m_tLastMeta.m_sWarning;
  10511. StatCountCommand ( SEARCHD_COMMAND_UPDATE );
  10512. int64_t tmStart = sphMicroTimer();
  10513. // extract index names
  10514. StrVec_t dIndexNames;
  10515. ParseIndexList ( tStmt.m_sIndex, dIndexNames );
  10516. if ( dIndexNames.IsEmpty() )
  10517. {
  10518. tOut.Error ( "no such index '%s'", tStmt.m_sIndex.cstr() );
  10519. return;
  10520. }
  10521. DistrPtrs_t dDistributed;
  10522. // copy distributed indexes description
  10523. CSphString sMissed;
  10524. if ( !ExtractDistributedIndexes ( dIndexNames, dDistributed, sMissed ) )
  10525. {
  10526. tOut.Error ( "unknown index '%s' in update request", sMissed.cstr() );
  10527. return;
  10528. }
  10529. // do update
  10530. SearchFailuresLog_c dFails;
  10531. int iSuccesses = 0;
  10532. int iUpdated = 0;
  10533. int iWarns = 0;
  10534. bool bBlobUpdate = false;
  10535. for ( const auto & i : tStmt.AttrUpdate().m_dAttributes )
  10536. {
  10537. if ( i.m_sName==sphGetDocidName() )
  10538. {
  10539. tOut.Error ( "'id' attribute cannot be updated" );
  10540. return;
  10541. }
  10542. bBlobUpdate |= sphIsBlobAttr ( i.m_eType );
  10543. }
  10544. ARRAY_FOREACH ( iIdx, dIndexNames )
  10545. {
  10546. const char * sReqIndex = dIndexNames[iIdx].cstr();
  10547. auto pLocked = GetServed ( sReqIndex );
  10548. if ( pLocked )
  10549. {
  10550. DoExtendedUpdate ( tStmt, sReqIndex, nullptr, bBlobUpdate, iSuccesses, iUpdated, dFails, sWarning, pLocked );
  10551. } else if ( dDistributed[iIdx] )
  10552. {
  10553. assert ( !dDistributed[iIdx]->IsEmpty() );
  10554. const StrVec_t & dLocal = dDistributed[iIdx]->m_dLocal;
  10555. ARRAY_FOREACH ( i, dLocal )
  10556. {
  10557. const char * sLocal = dLocal[i].cstr();
  10558. auto pServed = GetServed ( sLocal );
  10559. DoExtendedUpdate ( tStmt, sLocal, sReqIndex, bBlobUpdate, iSuccesses, iUpdated, dFails, sWarning, pServed );
  10560. }
  10561. }
  10562. // update remote agents
  10563. if ( dDistributed[iIdx] && !dDistributed[iIdx]->m_dAgents.IsEmpty() )
  10564. {
  10565. const DistributedIndex_t * pDist = dDistributed[iIdx];
  10566. VecRefPtrs_t<AgentConn_t *> dAgents;
  10567. pDist->GetAllHosts ( dAgents );
  10568. // connect to remote agents and query them
  10569. std::unique_ptr<RequestBuilder_i> pRequestBuilder = CreateRequestBuilder ( sQuery, tStmt );
  10570. std::unique_ptr<ReplyParser_i> pReplyParser = CreateReplyParser ( tStmt.m_bJson, iUpdated, iWarns );
  10571. iSuccesses += PerformRemoteTasks ( dAgents, pRequestBuilder.get (), pReplyParser.get () );
  10572. }
  10573. }
  10574. StringBuilder_c sReport;
  10575. dFails.BuildReport ( sReport );
  10576. if ( !iSuccesses )
  10577. {
  10578. tOut.Error ( "%s", sReport.cstr() );
  10579. return;
  10580. } else
  10581. {
  10582. int64_t tmRealTime = sphMicroTimer() - tmStart;
  10583. LogStatementSphinxql ( sQuery, (int)( tmRealTime / 1000 ) );
  10584. }
  10585. tOut.Ok ( iUpdated, iWarns );
  10586. }
  10587. bool HandleMysqlSelect ( RowBuffer_i & dRows, SearchHandler_c & tHandler )
  10588. {
  10589. // lets check all query for errors
  10590. CSphString sError;
  10591. CSphVector<int64_t> dAgentTimes; // dummy for error reporting
  10592. ARRAY_FOREACH ( i, tHandler.m_dQueries )
  10593. {
  10594. CheckQuery ( tHandler.m_dQueries[i], tHandler.m_dAggrResults[i].m_sError );
  10595. if ( !tHandler.m_dAggrResults[i].m_sError.IsEmpty() )
  10596. {
  10597. LogQuery ( tHandler.m_dQueries[i], tHandler.m_dAggrResults[i], dAgentTimes );
  10598. if ( sError.IsEmpty() )
  10599. {
  10600. if ( tHandler.m_dQueries.GetLength()==1 )
  10601. sError = tHandler.m_dAggrResults[0].m_sError;
  10602. else
  10603. sError.SetSprintf ( "query %d error: %s", i, tHandler.m_dAggrResults[i].m_sError.cstr() );
  10604. } else
  10605. sError.SetSprintf ( "%s; query %d error: %s", sError.cstr(), i, tHandler.m_dAggrResults[i].m_sError.cstr() );
  10606. }
  10607. }
  10608. if ( sError.Length() )
  10609. {
  10610. // stmt is intentionally NULL, as we did all the reporting just above
  10611. dRows.Error ( NULL, sError.cstr() );
  10612. return false;
  10613. }
  10614. // actual searching
  10615. tHandler.RunQueries();
  10616. if ( sphInterrupted() )
  10617. {
  10618. sphLogDebug ( "HandleClientMySQL: got SIGTERM, sending the packet MYSQL_ERR_SERVER_SHUTDOWN" );
  10619. dRows.Error ( NULL, "Server shutdown in progress", MYSQL_ERR_SERVER_SHUTDOWN );
  10620. return false;
  10621. }
  10622. return true;
  10623. }
  10624. inline static int Bit ( int iBit, const unsigned int * pData )
  10625. {
  10626. return ( ( pData[iBit / 32] & ( 1 << ( iBit % 32 ) ) ) ? 1 : 0 );
  10627. }
  10628. void sphFormatFactors ( StringBuilder_c & sOut, const unsigned int * pFactors, bool bJson )
  10629. {
  10630. sOut.GrowEnough ( 512 );
  10631. // format lines for header, fields, words
  10632. const char * sBmFmt = nullptr;
  10633. const char * sFieldFmt = nullptr;
  10634. const char * sWordFmt = nullptr;
  10635. ScopedComma_c sDelim;
  10636. if ( bJson )
  10637. {
  10638. sBmFmt = R"("bm25":%d, "bm25a":%f, "field_mask":%u, "doc_word_count":%d)";
  10639. sFieldFmt = R"({"field":%d, "lcs":%u, "hit_count":%u, "word_count":%u, "tf_idf":%f, "min_idf":%f, )"
  10640. R"("max_idf":%f, "sum_idf":%f, "min_hit_pos":%d, "min_best_span_pos":%d, "exact_hit":%u, )"
  10641. R"("max_window_hits":%d, "min_gaps":%d, "exact_order":%u, "lccs":%d, "wlccs":%f, "atc":%f})";
  10642. sWordFmt = R"(%i{"tf":%d, "idf":%f})";
  10643. sDelim.Init ( sOut, ", ", "{", "}" );
  10644. } else
  10645. {
  10646. sBmFmt = "bm25=%d, bm25a=%f, field_mask=%u, doc_word_count=%d";
  10647. sFieldFmt = "field%d=(lcs=%u, hit_count=%u, word_count=%u, tf_idf=%f, min_idf=%f, max_idf=%f, sum_idf=%f, "
  10648. "min_hit_pos=%d, min_best_span_pos=%d, exact_hit=%u, max_window_hits=%d, "
  10649. "min_gaps=%d, exact_order=%u, lccs=%d, wlccs=%f, atc=%f)";
  10650. sWordFmt = "word%d=(tf=%d, idf=%f)";
  10651. sDelim.Init ( sOut, ", " );
  10652. }
  10653. #define DI( _factor ) sphinx_get_doc_factor_int ( pFactors, SPH_DOCF_##_factor )
  10654. #define DF( _factor ) sphinx_get_doc_factor_float ( pFactors, SPH_DOCF_##_factor )
  10655. sOut.Sprintf ( sBmFmt, DI( BM25 ), DF( BM25A ), DI( MATCHED_FIELDS ), DI( DOC_WORD_COUNT ) );
  10656. { ScopedComma_c sFields;
  10657. if ( bJson )
  10658. sFields.Init ( sOut, ", ", R"("fields":[)", "]");
  10659. auto pExactHit = sphinx_get_doc_factor_ptr ( pFactors, SPH_DOCF_EXACT_HIT_MASK );
  10660. auto pExactOrder = sphinx_get_doc_factor_ptr ( pFactors, SPH_DOCF_EXACT_ORDER_MASK );
  10661. int iFields = DI ( NUM_FIELDS );
  10662. for ( int i = 0; i<iFields; ++i )
  10663. {
  10664. #define FI( _factor ) sphinx_get_field_factor_int ( pField, SPH_FIELDF_##_factor )
  10665. #define FF( _factor ) sphinx_get_field_factor_float ( pField, SPH_FIELDF_##_factor )
  10666. auto pField = sphinx_get_field_factors ( pFactors, i );
  10667. if ( !FI (HIT_COUNT) )
  10668. continue;
  10669. sOut.Sprintf ( sFieldFmt, i, FI (LCS), FI (HIT_COUNT), FI (WORD_COUNT), FF (TF_IDF), FF (MIN_IDF),
  10670. FF (MAX_IDF), FF (SUM_IDF), FI (MIN_HIT_POS), FI (MIN_BEST_SPAN_POS), Bit (i, pExactHit),
  10671. FI (MAX_WINDOW_HITS), FI (MIN_GAPS), Bit (i, pExactOrder), FI (LCCS), FF (WLCCS), FF (ATC) );
  10672. #undef FF
  10673. #undef FI
  10674. }
  10675. } // fields block
  10676. { ScopedComma_c sWords;
  10677. if ( bJson )
  10678. sWords.Init ( sOut, ", ", R"("words":[)", "]" );
  10679. auto iUniqQpos = DI ( MAX_UNIQ_QPOS );
  10680. for ( int i = 0; i<iUniqQpos; ++i )
  10681. {
  10682. auto pTerm = sphinx_get_term_factors ( pFactors, i + 1 );
  10683. if ( !sphinx_get_term_factor_int ( pTerm, SPH_TERMF_KEYWORD_MASK ) )
  10684. continue;
  10685. sOut.Sprintf ( sWordFmt, i, sphinx_get_term_factor_int ( pTerm, SPH_TERMF_TF ),
  10686. sphinx_get_term_factor_float ( pTerm, SPH_TERMF_IDF ) );
  10687. }
  10688. } // words block
  10689. #undef DF
  10690. #undef DI
  10691. }
  10692. static void ReturnZeroCount ( const CSphSchema & tSchema, const CSphBitvec & tAttrsToSend, const StrVec_t & dCounts, RowBuffer_i & dRows )
  10693. {
  10694. for ( int i=0; i<tSchema.GetAttrsCount(); ++i )
  10695. {
  10696. if ( !tAttrsToSend.BitGet(i) )
  10697. continue;
  10698. const CSphColumnInfo & tCol = tSchema.GetAttr ( i );
  10699. // @count or its alias or count(distinct attr_name)
  10700. if ( dCounts.Contains ( tCol.m_sName ) )
  10701. {
  10702. dRows.PutNumAsString ( 0 );
  10703. } else
  10704. {
  10705. // essentially the same as SELECT_DUAL, parse and print constant expressions
  10706. ESphAttr eAttrType;
  10707. CSphString sError;
  10708. ExprParseArgs_t tExprArgs;
  10709. tExprArgs.m_pAttrType = &eAttrType;
  10710. ISphExprRefPtr_c pExpr { sphExprParse ( tCol.m_sName.cstr(), tSchema, sError, tExprArgs )};
  10711. if ( !pExpr || !pExpr->IsConst() )
  10712. eAttrType = SPH_ATTR_NONE;
  10713. CSphMatch tMatch;
  10714. const BYTE * pStr = nullptr;
  10715. switch ( eAttrType )
  10716. {
  10717. case SPH_ATTR_STRINGPTR:
  10718. pExpr->StringEval ( tMatch, &pStr );
  10719. dRows.PutString ( (const char *)pStr );
  10720. SafeDelete ( pStr );
  10721. break;
  10722. case SPH_ATTR_INTEGER: dRows.PutNumAsString ( pExpr->IntEval ( tMatch ) ); break;
  10723. case SPH_ATTR_BIGINT: dRows.PutNumAsString ( pExpr->Int64Eval ( tMatch ) ); break;
  10724. case SPH_ATTR_FLOAT: dRows.PutFloatAsString ( pExpr->Eval ( tMatch ) ); break;
  10725. default:
  10726. dRows.PutNULL();
  10727. break;
  10728. }
  10729. }
  10730. }
  10731. dRows.Commit();
  10732. }
  10733. void SendMysqlSelectResult ( RowBuffer_i & dRows, const AggrResult_t & tRes, bool bMoreResultsFollow, bool bAddQueryColumn, const CSphString * pQueryColumn, QueryProfile_c * pProfile )
  10734. {
  10735. CSphScopedProfile tProf ( pProfile, SPH_QSTATE_NET_WRITE );
  10736. if ( !tRes.m_iSuccesses )
  10737. {
  10738. // at this point, SELECT error logging should have been handled, so pass a NULL stmt to logger
  10739. dRows.Error ( nullptr, tRes.m_sError.cstr() );
  10740. return;
  10741. }
  10742. // empty result sets just might carry the full uberschema
  10743. // bummer! lets protect ourselves against that
  10744. CSphBitvec tAttrsToSend;
  10745. bool bReturnZeroCount = !tRes.m_dZeroCount.IsEmpty();
  10746. if ( tRes.GetLength() || bReturnZeroCount )
  10747. sphGetAttrsToSend ( tRes.m_tSchema, false, true, tAttrsToSend );
  10748. // field packets
  10749. if ( tRes.GetLength()==0 && !bReturnZeroCount )
  10750. {
  10751. // in case there are no matches, send a dummy schema
  10752. // result set header packet. We will attach EOF manually at the end.
  10753. dRows.HeadBegin ( bAddQueryColumn ? 2 : 1 );
  10754. dRows.HeadColumn ( "id", MYSQL_COL_LONGLONG );
  10755. } else
  10756. {
  10757. int iAttrsToSend = tAttrsToSend.BitCount();
  10758. if ( bAddQueryColumn )
  10759. ++iAttrsToSend;
  10760. dRows.HeadBegin ( iAttrsToSend );
  10761. for ( int i=0; i<tRes.m_tSchema.GetAttrsCount(); ++i )
  10762. {
  10763. if ( !tAttrsToSend.BitGet(i) )
  10764. continue;
  10765. const CSphColumnInfo & tCol = tRes.m_tSchema.GetAttr(i);
  10766. MysqlColumnType_e eType = MYSQL_COL_STRING;
  10767. switch ( tCol.m_eAttrType )
  10768. {
  10769. case SPH_ATTR_INTEGER:
  10770. case SPH_ATTR_TIMESTAMP:
  10771. case SPH_ATTR_BOOL: eType = MYSQL_COL_LONG; break;
  10772. case SPH_ATTR_FLOAT: eType = MYSQL_COL_FLOAT; break;
  10773. case SPH_ATTR_DOUBLE: eType = MYSQL_COL_DOUBLE; break;
  10774. case SPH_ATTR_BIGINT: eType = MYSQL_COL_LONGLONG; break;
  10775. default: break;
  10776. }
  10777. dRows.HeadColumn ( tCol.m_sName.cstr(), eType );
  10778. }
  10779. }
  10780. if ( bAddQueryColumn )
  10781. dRows.HeadColumn ( "query" );
  10782. // EOF packet is sent explicitly due to non-default params.
  10783. auto iWarns = tRes.m_sWarning.IsEmpty() ? 0 : 1;
  10784. dRows.HeadEnd ( bMoreResultsFollow, iWarns );
  10785. // FIXME!!! replace that vector relocations by SqlRowBuffer
  10786. // rows
  10787. const CSphSchema &tSchema = tRes.m_tSchema;
  10788. assert ( tRes.m_bSingle );
  10789. auto dMatches = tRes.m_dResults.First ().m_dMatches.Slice ( tRes.m_iOffset, tRes.m_iCount );
  10790. for ( const auto& tMatch : dMatches )
  10791. {
  10792. for ( int i=0; i<tRes.m_tSchema.GetAttrsCount(); ++i )
  10793. {
  10794. if ( !tAttrsToSend.BitGet(i) )
  10795. continue;
  10796. const CSphColumnInfo & dAttr = tSchema.GetAttr(i);
  10797. CSphAttrLocator tLoc = dAttr.m_tLocator;
  10798. ESphAttr eAttrType = dAttr.m_eAttrType;
  10799. assert ( sphPlainAttrToPtrAttr(eAttrType)==eAttrType );
  10800. switch ( eAttrType )
  10801. {
  10802. case SPH_ATTR_INTEGER:
  10803. case SPH_ATTR_TIMESTAMP:
  10804. case SPH_ATTR_BOOL:
  10805. case SPH_ATTR_TOKENCOUNT:
  10806. dRows.PutNumAsString ( ( DWORD ) tMatch.GetAttr ( tLoc ) );
  10807. break;
  10808. case SPH_ATTR_BIGINT:
  10809. dRows.PutNumAsString( tMatch.GetAttr(tLoc) );
  10810. break;
  10811. case SPH_ATTR_FLOAT:
  10812. dRows.PutFloatAsString ( tMatch.GetAttrFloat(tLoc) );
  10813. break;
  10814. case SPH_ATTR_DOUBLE:
  10815. dRows.PutDoubleAsString ( tMatch.GetAttrDouble(tLoc) );
  10816. break;
  10817. case SPH_ATTR_INT64SET_PTR:
  10818. case SPH_ATTR_UINT32SET_PTR:
  10819. {
  10820. StringBuilder_c dStr;
  10821. sphPackedMVA2Str ( (const BYTE *)tMatch.GetAttr(tLoc), eAttrType==SPH_ATTR_INT64SET_PTR, dStr );
  10822. dRows.PutArray ( dStr, false );
  10823. break;
  10824. }
  10825. case SPH_ATTR_STRINGPTR:
  10826. {
  10827. auto * pString = ( const BYTE * ) tMatch.GetAttr ( tLoc );
  10828. auto dString = sphUnpackPtrAttr ( pString );
  10829. if ( dString.second>1 && dString.first[dString.second-2]=='\0' )
  10830. dString.second -= 2;
  10831. dRows.PutArray ( dString );
  10832. }
  10833. break;
  10834. case SPH_ATTR_JSON_PTR:
  10835. {
  10836. auto * pString = (const BYTE*) tMatch.GetAttr ( tLoc );
  10837. JsonEscapedBuilder sTmp;
  10838. if ( pString )
  10839. {
  10840. auto dJson = sphUnpackPtrAttr ( pString );
  10841. sphJsonFormat ( sTmp, dJson.first );
  10842. }
  10843. dRows.PutArray ( sTmp );
  10844. }
  10845. break;
  10846. case SPH_ATTR_FACTORS:
  10847. case SPH_ATTR_FACTORS_JSON:
  10848. {
  10849. auto dFactors = sphUnpackPtrAttr ((const BYTE *) tMatch.GetAttr ( tLoc ));
  10850. StringBuilder_c sTmp;
  10851. if ( !IsEmpty ( dFactors ))
  10852. sphFormatFactors ( sTmp, (const unsigned int *)dFactors.first, eAttrType==SPH_ATTR_FACTORS_JSON );
  10853. dRows.PutArray ( sTmp, false );
  10854. break;
  10855. }
  10856. case SPH_ATTR_JSON_FIELD_PTR:
  10857. {
  10858. const BYTE * pField = (const BYTE *)tMatch.GetAttr ( tLoc );
  10859. if ( !pField )
  10860. {
  10861. dRows.PutNULL();
  10862. break;
  10863. }
  10864. auto dField = sphUnpackPtrAttr ( pField );
  10865. auto eJson = ESphJsonType ( *dField.first++ );
  10866. if ( eJson==JSON_NULL )
  10867. {
  10868. dRows.PutNULL();
  10869. break;
  10870. }
  10871. // send string to client
  10872. JsonEscapedBuilder sTmp;
  10873. sphJsonFieldFormat ( sTmp, dField.first, eJson, false );
  10874. dRows.PutArray ( sTmp, false );
  10875. break;
  10876. }
  10877. default:
  10878. dRows.Add(1);
  10879. dRows.Add('-');
  10880. break;
  10881. }
  10882. }
  10883. if ( bAddQueryColumn )
  10884. {
  10885. assert ( pQueryColumn );
  10886. dRows.PutString ( *pQueryColumn );
  10887. }
  10888. dRows.Commit();
  10889. }
  10890. if ( bReturnZeroCount )
  10891. ReturnZeroCount ( tRes.m_tSchema, tAttrsToSend, tRes.m_dZeroCount, dRows );
  10892. // eof packet
  10893. dRows.Eof ( bMoreResultsFollow, iWarns );
  10894. }
  10895. void HandleMysqlWarning ( const CSphQueryResultMeta & tLastMeta, RowBuffer_i & dRows, bool bMoreResultsFollow )
  10896. {
  10897. // can't send simple ok if there are more results to send
  10898. // as it breaks order of multi-result output
  10899. if ( tLastMeta.m_sWarning.IsEmpty() && !bMoreResultsFollow )
  10900. {
  10901. dRows.Ok();
  10902. return;
  10903. }
  10904. // result set header packet
  10905. dRows.HeadBegin(3);
  10906. dRows.HeadColumn ( "Level" );
  10907. dRows.HeadColumn ( "Code", MYSQL_COL_DECIMAL );
  10908. dRows.HeadColumn ( "Message" );
  10909. dRows.HeadEnd ( bMoreResultsFollow );
  10910. // row
  10911. dRows.PutString ( "warning" );
  10912. dRows.PutString ( "1000" );
  10913. dRows.PutString ( tLastMeta.m_sWarning );
  10914. dRows.Commit();
  10915. // cleanup
  10916. dRows.Eof ( bMoreResultsFollow );
  10917. }
  10918. void HandleMysqlStatus ( RowBuffer_i & dRows, const SqlStmt_t & tStmt, bool bMoreResultsFollow )
  10919. {
  10920. VectorLike dStatus ( tStmt.m_sStringParam );
  10921. switch ( tStmt.m_eStmt )
  10922. {
  10923. case STMT_SHOW_STATUS:
  10924. BuildStatus ( dStatus );
  10925. break;
  10926. case STMT_SHOW_AGENT_STATUS:
  10927. BuildAgentStatus ( dStatus, tStmt.m_sIndex );
  10928. break;
  10929. default:
  10930. assert(0); // only 'show' statements allowed here.
  10931. break;
  10932. }
  10933. // result set header packet
  10934. if (!dRows.HeadOfStrings ( dStatus.Header() ))
  10935. return;
  10936. // send rows
  10937. for ( int iRow=0; iRow<dStatus.GetLength(); iRow+=2 )
  10938. if ( !dRows.DataTuplet ( dStatus[iRow+0].cstr (), dStatus[iRow+1].cstr () ) )
  10939. return;
  10940. // cleanup
  10941. dRows.Eof ( bMoreResultsFollow );
  10942. }
  10943. void HandleMysqlMeta ( RowBuffer_i & dRows, const SqlStmt_t & tStmt, const CSphQueryResultMeta & tLastMeta, bool bMoreResultsFollow )
  10944. {
  10945. VectorLike dMeta ( tStmt.m_sStringParam );
  10946. assert ( tStmt.m_eStmt==STMT_SHOW_META );
  10947. BuildMeta ( dMeta, tLastMeta );
  10948. // result set header packet
  10949. if ( !dRows.HeadOfStrings ( dMeta.Header () ) )
  10950. return;
  10951. // send rows
  10952. for ( int iRow=0; iRow<dMeta.GetLength(); iRow+=2 )
  10953. if ( !dRows.DataTuplet ( dMeta[iRow+0].cstr (), dMeta[iRow+1].cstr () ) )
  10954. return;
  10955. // cleanup
  10956. dRows.Eof ( bMoreResultsFollow );
  10957. }
  10958. static std::unique_ptr<ReplicationCommand_t> MakePercolateDeleteDocumentsCommand ( CSphString sIndex, CSphString sCluster, const SqlStmt_t & tStmt, CSphString & sError )
  10959. {
  10960. // prohibit double copy of filters
  10961. const CSphQuery& tQuery = tStmt.m_tQuery;
  10962. if ( tQuery.m_dFilters.IsEmpty() || tQuery.m_dFilters.GetLength() > 1 )
  10963. {
  10964. sError.SetSprintf ( "only single filter supported, got %d", tQuery.m_dFilters.GetLength() );
  10965. return nullptr;
  10966. }
  10967. const CSphFilterSettings* pFilter = tQuery.m_dFilters.Begin();
  10968. auto pCmd = MakeReplicationCommand ( ReplicationCommand_e::PQUERY_DELETE, std::move ( sIndex ), std::move ( sCluster ) );
  10969. if ( ( pFilter->m_bHasEqualMin || pFilter->m_bHasEqualMax ) && !pFilter->m_bExclude && pFilter->m_eType==SPH_FILTER_VALUES && ( pFilter->m_sAttrName=="@id" || pFilter->m_sAttrName=="id" || pFilter->m_sAttrName=="uid" ) )
  10970. {
  10971. pCmd->m_dDeleteQueries.Append ( pFilter->GetValues() );
  10972. return pCmd;
  10973. }
  10974. if ( pFilter->m_eType==SPH_FILTER_STRING && pFilter->m_sAttrName=="tags" && !pFilter->m_dStrings.IsEmpty() )
  10975. {
  10976. pCmd->m_sDeleteTags = pFilter->m_dStrings[0];
  10977. return pCmd;
  10978. }
  10979. if ( pFilter->m_eType==SPH_FILTER_STRING_LIST && pFilter->m_sAttrName=="tags" && !pFilter->m_dStrings.IsEmpty() )
  10980. {
  10981. StringBuilder_c tBuf ( "," );
  10982. pFilter->m_dStrings.for_each ( [&tBuf] ( const auto& sVal ) { tBuf << sVal; } );
  10983. tBuf.FinishBlocks ();
  10984. tBuf.MoveTo ( pCmd->m_sDeleteTags );
  10985. return pCmd;
  10986. }
  10987. sError.SetSprintf ( "unsupported filter type %d, attribute '%s'", pFilter->m_eType, pFilter->m_sAttrName.cstr() );
  10988. return nullptr;
  10989. }
  10990. static int LocalIndexDoDeleteDocuments ( const CSphString & sName, const char * sDistributed, const SqlStmt_t & tStmt,
  10991. SearchFailuresLog_c & dErrors, bool bCommit, CSphSessionAccum & tAcc )
  10992. {
  10993. const CSphString & sCluster = tStmt.m_sCluster;
  10994. const CSphString & sStore = tStmt.m_tQuery.m_sStore;
  10995. bool bOnlyStoreDocIDs = !sStore.IsEmpty();
  10996. CSphString sError;
  10997. auto err = [&sName, &sDistributed, &sError, &dErrors] (const char* szErr = nullptr)
  10998. {
  10999. dErrors.Submit ( sName, sDistributed, szErr ? szErr : sError.cstr() );
  11000. return 0;
  11001. };
  11002. cServedIndexRefPtr_c pServed { GetServed ( sName ) };
  11003. if ( !ServedDesc_t::IsMutable ( pServed ) )
  11004. return err ( "index not available, or does not support DELETE" );
  11005. GlobalCrashQueryGetRef().m_dIndex = FromStr ( sName );
  11006. if ( !CheckIndexCluster ( sName, *pServed, sCluster, IsHttpStmt ( tStmt ), sError ) )
  11007. return err();
  11008. // process store to local variable instead of deletion (here we don't need any stuff like accum, txn, replication)
  11009. if ( bOnlyStoreDocIDs )
  11010. {
  11011. if ( pServed->m_eType == IndexType_e::PERCOLATE )
  11012. return err ( "Storing del subset not implemented for PQ indexes" );
  11013. assert ( sStore.Begins ( "@" ) );
  11014. DocsCollector_c dData { tStmt.m_tQuery, tStmt.m_bJson, sName, pServed, &sError };
  11015. auto dDocs = dData.GetValuesSlice();
  11016. if ( !sError.IsEmpty() )
  11017. return err();
  11018. SetLocalTemporaryUserVar ( sStore, dDocs );
  11019. return 0;
  11020. }
  11021. RtAccum_t* pAccum = nullptr;
  11022. // goto to percolate path with unlocked index
  11023. if ( pServed->m_eType==IndexType_e::PERCOLATE )
  11024. {
  11025. auto pCmd = MakePercolateDeleteDocumentsCommand ( sName, sCluster, tStmt, sError );
  11026. if ( !sError.IsEmpty() )
  11027. return err();
  11028. if ( !pCmd )
  11029. return 0;
  11030. RIdx_T<RtIndex_i*> pRtIndex { pServed };
  11031. pAccum = tAcc.GetAcc ( pRtIndex, sError );
  11032. if ( !sError.IsEmpty() )
  11033. return err();
  11034. assert ( pAccum );
  11035. pAccum->m_dCmd.Add ( std::move ( pCmd ) );
  11036. } else
  11037. {
  11038. DocsCollector_c dData { tStmt.m_tQuery, tStmt.m_bJson, sName, pServed, &sError};
  11039. auto dDocs = dData.GetValuesSlice();
  11040. if ( !sError.IsEmpty() )
  11041. return err();
  11042. RIdx_T<RtIndex_i*> pRtIndex { pServed };
  11043. pAccum = tAcc.GetAcc ( pRtIndex, sError );
  11044. if ( !sError.IsEmpty() )
  11045. return err();
  11046. if ( !pRtIndex->DeleteDocument ( dDocs, sError, pAccum ) ) // assume dData is alive, as we use slice from internal vec
  11047. return err();
  11048. assert ( pAccum );
  11049. pAccum->AddCommand ( ReplicationCommand_e::RT_TRX, sName, sCluster );
  11050. }
  11051. int iAffected = 0;
  11052. if ( bCommit )
  11053. {
  11054. if ( !HandleCmdReplicate ( *pAccum, sError, iAffected ) )
  11055. {
  11056. dErrors.Submit ( sName, sDistributed, sError.cstr() );
  11057. return 0;
  11058. }
  11059. }
  11060. return iAffected;
  11061. }
  11062. void sphHandleMysqlDelete ( StmtErrorReporter_i & tOut, const SqlStmt_t & tStmt, Str_t sQuery )
  11063. {
  11064. if ( !sphCheckWeCanModify ( tOut ) )
  11065. return;
  11066. auto* pSession = session::GetClientSession();
  11067. pSession->FreezeLastMeta();
  11068. bool bCommit = pSession->m_bAutoCommit && !pSession->m_bInTransaction;
  11069. auto& tAcc = pSession->m_tAcc;
  11070. StatCountCommand ( SEARCHD_COMMAND_DELETE );
  11071. MEMORY ( MEM_SQL_DELETE );
  11072. // shortcut
  11073. const CSphQuery & tQuery = tStmt.m_tQuery;
  11074. const CSphString & sStorevar = tQuery.m_sStore;
  11075. bool bStoreVar = !sStorevar.IsEmpty();
  11076. if ( bStoreVar && !sStorevar.Begins("@") )
  11077. {
  11078. tOut.Error ( "store var name must start with @, '%s' given", sStorevar.cstr() );
  11079. return;
  11080. }
  11081. StrVec_t dNames;
  11082. ParseIndexList ( tStmt.m_sIndex, dNames );
  11083. if ( dNames.IsEmpty() )
  11084. {
  11085. tOut.Error ( "no such index '%s'", tStmt.m_sIndex.cstr () );
  11086. return;
  11087. }
  11088. DistrPtrs_t dDistributed;
  11089. CSphString sMissed;
  11090. if ( !ExtractDistributedIndexes ( dNames, dDistributed, sMissed ) )
  11091. {
  11092. tOut.Error ( "unknown index '%s' in delete request", sMissed.cstr () );
  11093. return;
  11094. }
  11095. // delete to agents works only with commit=1
  11096. if ( !bCommit )
  11097. {
  11098. for ( auto &pDist : dDistributed )
  11099. {
  11100. if ( !pDist || pDist->m_dAgents.IsEmpty() )
  11101. continue;
  11102. tOut.Error ( "index '%s': DELETE is not supported on agents when autocommit=0", tStmt.m_sIndex.cstr() );
  11103. return;
  11104. }
  11105. }
  11106. // do delete
  11107. SearchFailuresLog_c dErrors;
  11108. int iAffected = 0;
  11109. // delete for local indexes
  11110. ARRAY_FOREACH ( iIdx, dNames )
  11111. {
  11112. const CSphString & sName = dNames[iIdx];
  11113. bool bLocal = g_pLocalIndexes->Contains ( sName );
  11114. if ( bLocal )
  11115. {
  11116. iAffected += LocalIndexDoDeleteDocuments ( sName, nullptr, tStmt, dErrors, bCommit, tAcc );
  11117. }
  11118. else if ( dDistributed[iIdx] )
  11119. {
  11120. assert ( !dDistributed[iIdx]->IsEmpty() );
  11121. for ( const CSphString& sLocal : dDistributed[iIdx]->m_dLocal )
  11122. {
  11123. bool bDistLocal = g_pLocalIndexes->Contains ( sLocal );
  11124. if ( bDistLocal )
  11125. {
  11126. iAffected += LocalIndexDoDeleteDocuments ( sLocal, sName.cstr(), tStmt, dErrors, bCommit, tAcc );
  11127. }
  11128. }
  11129. }
  11130. // delete for remote agents
  11131. if ( !bStoreVar && dDistributed[iIdx] && !dDistributed[iIdx]->m_dAgents.IsEmpty() )
  11132. {
  11133. const DistributedIndex_t * pDist = dDistributed[iIdx];
  11134. VecRefPtrsAgentConn_t dAgents;
  11135. pDist->GetAllHosts ( dAgents );
  11136. int iGot = 0;
  11137. int iWarns = 0;
  11138. // connect to remote agents and query them
  11139. std::unique_ptr<RequestBuilder_i> pRequestBuilder = CreateRequestBuilder ( sQuery, tStmt );
  11140. std::unique_ptr<ReplyParser_i> pReplyParser = CreateReplyParser ( tStmt.m_bJson, iGot, iWarns );
  11141. PerformRemoteTasks ( dAgents, pRequestBuilder.get (), pReplyParser.get () );
  11142. // FIXME!!! report error & warnings from agents
  11143. // FIXME? profile update time too?
  11144. iAffected += iGot;
  11145. }
  11146. }
  11147. if ( !dErrors.IsEmpty() )
  11148. {
  11149. StringBuilder_c sReport;
  11150. dErrors.BuildReport ( sReport );
  11151. tOut.Error ( "%s", sReport.cstr () );
  11152. return;
  11153. }
  11154. tOut.Ok ( iAffected );
  11155. }
  11156. struct SessionVars_t
  11157. {
  11158. bool m_bAutoCommit = true;
  11159. bool m_bInTransaction = false;
  11160. CSphVector<int64_t> m_dLastIds;
  11161. };
  11162. // fwd
  11163. void HandleMysqlShowProfile ( RowBuffer_i & tOut, const QueryProfile_c & p, bool bMoreResultsFollow );
  11164. static void HandleMysqlShowPlan ( RowBuffer_i & tOut, const QueryProfile_c & p, bool bMoreResultsFollow, bool bDot );
  11165. bool IsDot ( const SqlStmt_t & tStmt )
  11166. {
  11167. if ( tStmt.m_sThreadFormat=="dot" )
  11168. return true;
  11169. else if ( tStmt.m_sThreadFormat=="plain" )
  11170. return false;
  11171. return session::IsDot();
  11172. }
  11173. Profile_e ParseProfileFormat ( const SqlStmt_t & tStmt )
  11174. {
  11175. if ( tStmt.m_sSetValue=="dot" )
  11176. return Profile_e::DOT;
  11177. else if ( tStmt.m_sSetValue=="expr" )
  11178. return Profile_e::DOTEXPR;
  11179. else if ( tStmt.m_sSetValue=="exprurl" )
  11180. return Profile_e::DOTEXPRURL;
  11181. else if ( tStmt.m_iSetValue!=0 )
  11182. return Profile_e::PLAIN;
  11183. return Profile_e::NONE;
  11184. }
  11185. void HandleMysqlMultiStmt ( const CSphVector<SqlStmt_t> & dStmt, CSphQueryResultMeta & tLastMeta, RowBuffer_i & dRows,
  11186. const CSphString & sWarning )
  11187. {
  11188. auto& tSess = session::Info();
  11189. // select count
  11190. int iSelect = dStmt.count_of ( [] ( const auto& tStmt ) { return tStmt.m_eStmt == STMT_SELECT; } );
  11191. CSphQueryResultMeta tPrevMeta = tLastMeta;
  11192. myinfo::SetCommand ( g_dSqlStmts[STMT_SELECT] );
  11193. for ( int i=0; i<iSelect; i++ )
  11194. StatCountCommand ( SEARCHD_COMMAND_SEARCH );
  11195. // setup query for searching
  11196. SearchHandler_c tHandler ( iSelect, sphCreatePlainQueryParser(), QUERY_SQL, true );
  11197. QueryProfile_c tProfile;
  11198. iSelect = 0;
  11199. for ( auto& tStmt: dStmt )
  11200. switch ( tStmt.m_eStmt )
  11201. {
  11202. case STMT_SELECT:
  11203. {
  11204. tHandler.SetQuery ( iSelect, tStmt.m_tQuery, std::move ( tStmt.m_pTableFunc ) );
  11205. ++iSelect;
  11206. break;
  11207. }
  11208. case STMT_SET:
  11209. if ( tStmt.m_eSet == SET_LOCAL )
  11210. {
  11211. CSphString sSetName ( tStmt.m_sSetName );
  11212. sSetName.ToLower();
  11213. if ( sSetName == "profiling" )
  11214. tSess.SetProfile ( ParseProfileFormat ( tStmt ) );
  11215. }
  11216. default: break;
  11217. }
  11218. // use first meta for faceted search
  11219. bool bUseFirstMeta = ( tHandler.m_dQueries.GetLength()>1 && !tHandler.m_dQueries[0].m_bFacet && tHandler.m_dQueries[1].m_bFacet );
  11220. if ( tSess.IsProfile() )
  11221. tHandler.SetProfile ( &tProfile );
  11222. // do search
  11223. bool bSearchOK = true;
  11224. if ( iSelect )
  11225. {
  11226. bSearchOK = HandleMysqlSelect ( dRows, tHandler );
  11227. // save meta for SHOW *
  11228. if ( bUseFirstMeta )
  11229. {
  11230. tLastMeta = tHandler.m_dAggrResults.First();
  11231. // fix up overall query time
  11232. for ( auto& tResult : tHandler.m_dAggrResults )
  11233. {
  11234. tLastMeta.m_iQueryTime += tResult.m_iQueryTime;
  11235. tLastMeta.m_iCpuTime += tResult.m_iCpuTime;
  11236. tLastMeta.m_iAgentCpuTime += tResult.m_iAgentCpuTime;
  11237. }
  11238. } else
  11239. tLastMeta = tHandler.m_dAggrResults.Last();
  11240. }
  11241. if ( !bSearchOK )
  11242. return;
  11243. // send multi-result set
  11244. iSelect = 0;
  11245. ARRAY_FOREACH ( i, dStmt )
  11246. {
  11247. SqlStmt_e eStmt = dStmt[i].m_eStmt;
  11248. myinfo::SetCommand ( g_dSqlStmts[eStmt] );
  11249. const CSphQueryResultMeta & tMeta = bUseFirstMeta ? tHandler.m_dAggrResults[0] : ( iSelect-1>=0 ? tHandler.m_dAggrResults[iSelect-1] : tPrevMeta );
  11250. bool bMoreResultsFollow = (i+1)<dStmt.GetLength();
  11251. bool bBreak = false;
  11252. switch ( eStmt )
  11253. {
  11254. case STMT_SELECT:
  11255. {
  11256. AggrResult_t & tRes = tHandler.m_dAggrResults[iSelect++];
  11257. if ( !sWarning.IsEmpty() )
  11258. tRes.m_sWarning = sWarning;
  11259. SendMysqlSelectResult ( dRows, tRes, bMoreResultsFollow, false, nullptr, ( tSess.IsProfile() ? &tProfile : nullptr ) );
  11260. // mysql server breaks send on error
  11261. bBreak = !tRes.m_iSuccesses;
  11262. break;
  11263. }
  11264. case STMT_SHOW_WARNINGS:
  11265. HandleMysqlWarning ( tMeta, dRows, bMoreResultsFollow );
  11266. break;
  11267. case STMT_SHOW_STATUS:
  11268. case STMT_SHOW_AGENT_STATUS:
  11269. HandleMysqlStatus ( dRows, dStmt[i], bMoreResultsFollow ); // FIXME!!! add prediction counters
  11270. break;
  11271. case STMT_SHOW_META:
  11272. HandleMysqlMeta ( dRows, dStmt[i], tMeta, bMoreResultsFollow ); // FIXME!!! add prediction counters
  11273. break;
  11274. case STMT_SET: // TODO implement all set statements and make them handle bMoreResultsFollow flag
  11275. dRows.Ok ( 0, 0, NULL, bMoreResultsFollow );
  11276. break;
  11277. case STMT_SHOW_PROFILE:
  11278. HandleMysqlShowProfile ( dRows, tProfile, bMoreResultsFollow );
  11279. break;
  11280. case STMT_SHOW_PLAN:
  11281. HandleMysqlShowPlan ( dRows, tProfile, bMoreResultsFollow, ::IsDot ( dStmt[i] ) );
  11282. default:
  11283. break;
  11284. }
  11285. if ( bBreak )
  11286. break;
  11287. if ( sphInterrupted() )
  11288. {
  11289. sphLogDebug ( "HandleMultiStmt: got SIGTERM, sending the packet MYSQL_ERR_SERVER_SHUTDOWN" );
  11290. dRows.Error ( NULL, "Server shutdown in progress", MYSQL_ERR_SERVER_SHUTDOWN );
  11291. return;
  11292. }
  11293. }
  11294. }
  11295. void HandleMysqlSet ( RowBuffer_i & tOut, SqlStmt_t & tStmt, CSphSessionAccum & tAcc )
  11296. {
  11297. auto& tSess = session::Info();
  11298. MEMORY ( MEM_SQL_SET );
  11299. CSphString sError;
  11300. tStmt.m_sSetName.ToLower();
  11301. switch ( tStmt.m_eSet )
  11302. {
  11303. case SET_LOCAL: // SET foo = value|'svalue'|null
  11304. if ( tStmt.m_sSetName=="wait_timeout" )
  11305. {
  11306. tSess.SetTimeoutS ( tStmt.m_iSetValue );
  11307. break;
  11308. }
  11309. if ( tStmt.m_sSetName=="throttling_period" )
  11310. {
  11311. tSess.SetThrottlingPeriodMS( tStmt.m_iSetValue );
  11312. break;
  11313. }
  11314. if ( tStmt.m_sSetName == "optimize_by_id" )
  11315. {
  11316. session::SetOptimizeById ( !!tStmt.m_iSetValue );
  11317. break;
  11318. }
  11319. if ( tStmt.m_sSetName=="max_threads_per_query" )
  11320. {
  11321. tSess.SetDistThreads ( tStmt.m_iSetValue );
  11322. break;
  11323. }
  11324. if ( tStmt.m_sSetName == "ro" )
  11325. {
  11326. if ( !tSess.GetVip() )
  11327. {
  11328. if (!sphCheckWeCanModify ( tStmt.m_sStmt, tOut ) )
  11329. return;
  11330. }
  11331. tSess.SetReadOnly ( !!tStmt.m_iSetValue );
  11332. break;
  11333. }
  11334. if ( tStmt.m_sSetName == "threads_ex" )
  11335. {
  11336. auto dDispatchers = Dispatcher::ParseTemplates ( tStmt.m_sSetValue.cstr() );
  11337. tSess.SetBaseDispatcherTemplate ( dDispatchers.first );
  11338. tSess.SetPseudoShardingDispatcherTemplate ( dDispatchers.second );
  11339. break;
  11340. }
  11341. // move check here from bison parser. Only boolean allowed below.
  11342. if ( tStmt.m_iSetValue!=0 && tStmt.m_iSetValue!=1 )
  11343. {
  11344. tOut.ErrorEx ( tStmt.m_sStmt, "sphinxql: only 0 and 1 could be used as boolean values near '%d'", tStmt.m_iSetValue );
  11345. return;
  11346. }
  11347. if ( tStmt.m_sSetName=="autocommit" )
  11348. {
  11349. // per-session AUTOCOMMIT
  11350. bool bAutoCommit = ( tStmt.m_iSetValue != 0 );
  11351. auto pSession = session::Info().GetClientSession();
  11352. pSession->m_bAutoCommit = ( tStmt.m_iSetValue!=0 );
  11353. pSession->m_bInTransaction = false;
  11354. // commit all pending changes
  11355. if ( bAutoCommit && tAcc.GetIndex() && !HandleCmdReplicate ( *tAcc.GetAcc(), sError ) )
  11356. return tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  11357. } else if ( tStmt.m_sSetName=="collation_connection" )
  11358. {
  11359. // per-session COLLATION_CONNECTION
  11360. CSphString & sVal = tStmt.m_sSetValue;
  11361. sVal.ToLower();
  11362. tSess.SetCollation ( sphCollationFromName ( sVal, &sError ) );
  11363. if ( !sError.IsEmpty() )
  11364. return tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  11365. } else if ( tStmt.m_sSetName=="character_set_results"
  11366. || tStmt.m_sSetName=="sql_auto_is_null"
  11367. || tStmt.m_sSetName=="sql_safe_updates"
  11368. || tStmt.m_sSetName=="sql_mode"
  11369. || tStmt.m_sSetName=="time_zone" )
  11370. {
  11371. // per-session CHARACTER_SET_RESULTS at all; just ignore for now
  11372. } else if ( tStmt.m_sSetName=="profiling" )
  11373. {
  11374. // per-session PROFILING
  11375. tSess.SetProfile ( ParseProfileFormat ( tStmt ) );
  11376. } else
  11377. {
  11378. // unknown variable, return error
  11379. tOut.ErrorEx ( tStmt.m_sStmt, "Unknown session variable '%s' in SET statement", tStmt.m_sSetName.cstr () );
  11380. return;
  11381. }
  11382. break;
  11383. case SET_GLOBAL_UVAR: // SET GLOBAL @foo = (i1,i2,...)'
  11384. {
  11385. // global user variable
  11386. // INT_SET type must be sorted
  11387. tStmt.m_dSetValues.Sort();
  11388. SetLocalUserVar ( tStmt.m_sSetName, tStmt.m_dSetValues );
  11389. break;
  11390. }
  11391. case SET_GLOBAL_SVAR: // SET GLOBAL foo = iValue|'string'
  11392. if ( !tSess.GetVip() && !sphCheckWeCanModify ( tStmt.m_sStmt, tOut ) )
  11393. return;
  11394. // global server variable
  11395. if ( tStmt.m_sSetName=="query_log_format" )
  11396. {
  11397. if ( tStmt.m_sSetValue=="plain" )
  11398. g_eLogFormat = LOG_FORMAT_PLAIN;
  11399. else if ( tStmt.m_sSetValue=="sphinxql" )
  11400. g_eLogFormat = LOG_FORMAT_SPHINXQL;
  11401. else
  11402. {
  11403. tOut.Error ( tStmt.m_sStmt, "Unknown query_log_format value (must be plain or sphinxql)" );
  11404. return;
  11405. }
  11406. } else if ( tStmt.m_sSetName=="log_level" )
  11407. {
  11408. if ( tStmt.m_sSetValue=="info" )
  11409. g_eLogLevel = SPH_LOG_INFO;
  11410. else if ( tStmt.m_sSetValue=="debug" )
  11411. g_eLogLevel = SPH_LOG_DEBUG;
  11412. else if ( tStmt.m_sSetValue=="debugv" )
  11413. g_eLogLevel = SPH_LOG_VERBOSE_DEBUG;
  11414. else if ( tStmt.m_sSetValue=="debugvv" )
  11415. g_eLogLevel = SPH_LOG_VERY_VERBOSE_DEBUG;
  11416. else if ( tStmt.m_sSetValue=="replication" )
  11417. g_eLogLevel = SPH_LOG_RPL_DEBUG;
  11418. else
  11419. {
  11420. tOut.Error ( tStmt.m_sStmt, "Unknown log_level value (must be one of info, debug, debugv, debugvv, replication)" );
  11421. return;
  11422. }
  11423. } else if ( tStmt.m_sSetName=="query_log_min_msec" )
  11424. {
  11425. g_iQueryLogMinMs = (int)tStmt.m_iSetValue;
  11426. } else if ( tStmt.m_sSetName=="qcache_max_bytes" )
  11427. {
  11428. const QcacheStatus_t & s = QcacheGetStatus();
  11429. QcacheSetup ( tStmt.m_iSetValue, s.m_iThreshMs, s.m_iTtlS );
  11430. } else if ( tStmt.m_sSetName=="qcache_thresh_msec" )
  11431. {
  11432. const QcacheStatus_t & s = QcacheGetStatus();
  11433. QcacheSetup ( s.m_iMaxBytes, (int)tStmt.m_iSetValue, s.m_iTtlS );
  11434. } else if ( tStmt.m_sSetName=="qcache_ttl_sec" )
  11435. {
  11436. const QcacheStatus_t & s = QcacheGetStatus();
  11437. QcacheSetup ( s.m_iMaxBytes, s.m_iThreshMs, (int)tStmt.m_iSetValue );
  11438. } else if ( tStmt.m_sSetName=="log_debug_filter" )
  11439. {
  11440. int iLen = tStmt.m_sSetValue.Length();
  11441. iLen = Min ( iLen, SPH_MAX_FILENAME_LEN );
  11442. memcpy ( g_sLogFilter, tStmt.m_sSetValue.cstr(), iLen );
  11443. g_sLogFilter[iLen] = '\0';
  11444. g_iLogFilterLen = iLen;
  11445. } else if ( tStmt.m_sSetName=="net_wait" )
  11446. {
  11447. g_tmWaitUS = tStmt.m_iSetValue * 1000LL;
  11448. } else if ( tStmt.m_sSetName=="grouping_in_utc")
  11449. {
  11450. g_bGroupingInUtc = !!tStmt.m_iSetValue;
  11451. SetGroupingInUtcExpr ( g_bGroupingInUtc );
  11452. SetGroupingInUtcSort ( g_bGroupingInUtc );
  11453. } else if ( tStmt.m_sSetName=="cpustats")
  11454. {
  11455. g_bCpuStats = !!tStmt.m_iSetValue;
  11456. } else if ( tStmt.m_sSetName=="iostats")
  11457. {
  11458. g_bIOStats = !!tStmt.m_iSetValue;
  11459. } else if ( tStmt.m_sSetName=="coredump")
  11460. {
  11461. g_bCoreDump = !!tStmt.m_iSetValue;
  11462. } else if ( tStmt.m_sSetName=="maintenance")
  11463. {
  11464. if ( tSess.GetVip() )
  11465. g_bMaintenance = !!tStmt.m_iSetValue;
  11466. else
  11467. {
  11468. tOut.Error ( tStmt.m_sStmt, "Only VIP connections can set maintenance mode" );
  11469. return;
  11470. }
  11471. } else if ( tStmt.m_sSetName=="wait_timeout" )
  11472. {
  11473. if ( tSess.GetVip() )
  11474. g_iClientQlTimeoutS = tStmt.m_iSetValue;
  11475. else
  11476. {
  11477. tOut.Error ( tStmt.m_sStmt, "Only VIP connections can change global wait_timeout value" );
  11478. return;
  11479. }
  11480. } else if ( tStmt.m_sSetName=="throttling_period" )
  11481. {
  11482. if ( tSess.GetVip() )
  11483. Threads::Coro::Throttler_c::SetDefaultThrottlingPeriodMS ( tStmt.m_iSetValue );
  11484. else
  11485. {
  11486. tOut.Error ( tStmt.m_sStmt, "Only VIP connections can change global throttling_period value" );
  11487. return;
  11488. }
  11489. } else if ( tStmt.m_sSetName=="max_threads_per_query" )
  11490. {
  11491. g_iDistThreads = tStmt.m_iSetValue; // that is not dangerous to allow everybody change the value
  11492. } else if ( tStmt.m_sSetName=="auto_optimize")
  11493. {
  11494. if ( !AUTOOPTIMIZE_NEEDS_VIP || tSess.GetVip() )
  11495. g_iAutoOptimizeCutoffMultiplier = tStmt.m_iSetValue;
  11496. else
  11497. {
  11498. tOut.Error ( tStmt.m_sStmt, "Only VIP connections can change global auto_optimize value" );
  11499. return;
  11500. }
  11501. } else if ( tStmt.m_sSetName=="optimize_cutoff")
  11502. {
  11503. if ( tStmt.m_iSetValue<1 )
  11504. {
  11505. tOut.ErrorEx ( tStmt.m_sStmt, "optimize_cutoff should be greater than 0, got %d", tStmt.m_iSetValue );
  11506. return;
  11507. }
  11508. MutableIndexSettings_c::GetDefaults().m_iOptimizeCutoff = tStmt.m_iSetValue;
  11509. } else if ( tStmt.m_sSetName=="pseudo_sharding")
  11510. {
  11511. g_bSplit = !!tStmt.m_iSetValue;
  11512. } else if ( tStmt.m_sSetName=="secondary_indexes" )
  11513. {
  11514. SetSecondaryIndexDefault ( !!tStmt.m_iSetValue );
  11515. } else if ( tStmt.m_sSetName == "threads_ex" )
  11516. {
  11517. if ( !THREAD_EX_NEEDS_VIP || tSess.GetVip() )
  11518. Dispatcher::SetGlobalDispatchers ( tStmt.m_sSetValue.cstr() );
  11519. else
  11520. {
  11521. tOut.Error ( tStmt.m_sStmt, "Only VIP connections can change global threads_ex value" );
  11522. return;
  11523. }
  11524. } else {
  11525. tOut.ErrorEx ( tStmt.m_sStmt, "Unknown system variable '%s'", tStmt.m_sSetName.cstr () );
  11526. return;
  11527. }
  11528. break;
  11529. case SET_INDEX_UVAR: // SET INDEX bar GLOBAL @foo = (values)
  11530. if ( !SendUserVar ( tStmt.m_sIndex.cstr(), tStmt.m_sSetName.cstr(), tStmt.m_dSetValues, sError ) )
  11531. {
  11532. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  11533. return;
  11534. }
  11535. break;
  11536. case SET_CLUSTER_UVAR: // SET CLUSTER ident GLOBAL 'variable' = string|int
  11537. {
  11538. if ( !ReplicateSetOption ( tStmt.m_sIndex, tStmt.m_sSetName, tStmt.m_sSetValue, sError ) )
  11539. {
  11540. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  11541. return;
  11542. }
  11543. }
  11544. break;
  11545. default:
  11546. tOut.ErrorEx ( tStmt.m_sStmt, "internal error: unhandled SET mode %d", (int) tStmt.m_eSet );
  11547. return;
  11548. }
  11549. // it went ok
  11550. tOut.Ok();
  11551. }
  11552. void HandleMysqlAttach ( RowBuffer_i & tOut, const SqlStmt_t & tStmt, CSphString & sWarning )
  11553. {
  11554. if ( !sphCheckWeCanModify ( tStmt.m_sStmt, tOut ) )
  11555. return;
  11556. const CSphString & sFrom = tStmt.m_sIndex;
  11557. const CSphString & sTo = tStmt.m_sStringParam;
  11558. bool bTruncate = ( tStmt.m_iIntParam==1 );
  11559. CSphString sError;
  11560. auto pServedFrom = GetServed ( sFrom );
  11561. auto pServedTo = GetServed ( sTo );
  11562. bool bOk = false;
  11563. if ( !pServedFrom )
  11564. tOut.ErrorEx ( nullptr, "no such index '%s'", sFrom.cstr() );
  11565. else if ( pServedFrom->m_eType != IndexType_e::PLAIN )
  11566. tOut.Error ( tStmt.m_sStmt, "1st argument to ATTACH must be a plain index" );
  11567. else if ( !pServedTo )
  11568. tOut.ErrorEx ( nullptr, "no such index '%s'", sTo.cstr() );
  11569. else if ( pServedTo->m_eType!=IndexType_e::RT )
  11570. tOut.Error ( tStmt.m_sStmt, "2nd argument to ATTACH must be a RT index" );
  11571. else
  11572. bOk = true;
  11573. if (!bOk)
  11574. return;
  11575. // cluster does not implement ATTACH for now
  11576. auto tCluster = IsPartOfCluster ( pServedTo );
  11577. if ( tCluster )
  11578. {
  11579. tOut.ErrorEx ( nullptr, "index %s is part of cluster %s, can not issue ATTACH", sTo.cstr(), tCluster->cstr(), sError.cstr () );
  11580. return;
  11581. }
  11582. WIdx_T<RtIndex_i*> pRtTo { pServedTo };
  11583. WIdx_c pPlainFrom { pServedFrom };
  11584. bool bFatal = false;
  11585. StrVec_t dWarnings;
  11586. auto bAttached = pRtTo->AttachDiskIndex ( pPlainFrom, bTruncate, bFatal, dWarnings, sError );
  11587. sWarning = ConcatWarnings(dWarnings);
  11588. if ( bAttached || bFatal )
  11589. g_pLocalIndexes->Delete ( sFrom );
  11590. if ( bAttached )
  11591. {
  11592. pServedFrom->ReleaseIdx(); // since index no more belong to us
  11593. tOut.Ok();
  11594. }
  11595. else
  11596. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  11597. }
  11598. void HandleMysqlFlushRtindex ( RowBuffer_i & tOut, const SqlStmt_t & tStmt )
  11599. {
  11600. CSphString sError;
  11601. auto pIndex = GetServed ( tStmt.m_sIndex );
  11602. if ( !ServedDesc_t::IsMutable ( pIndex ) )
  11603. {
  11604. tOut.Error ( tStmt.m_sStmt, "FLUSH RTINDEX requires an existing RT index" );
  11605. return;
  11606. }
  11607. RIdx_T<RtIndex_i*> ( pIndex )->ForceRamFlush ( "forced" );
  11608. tOut.Ok();
  11609. }
  11610. void HandleMysqlFlushRamchunk ( RowBuffer_i & tOut, const SqlStmt_t & tStmt )
  11611. {
  11612. auto pIndex = GetServed ( tStmt.m_sIndex );
  11613. if ( !ServedDesc_t::IsMutable ( pIndex ) )
  11614. {
  11615. tOut.Error ( tStmt.m_sStmt, "FLUSH RAMCHUNK requires an existing RT index" );
  11616. return;
  11617. }
  11618. RIdx_T<RtIndex_i*> pRt { pIndex };
  11619. if ( !pRt->ForceDiskChunk() )
  11620. {
  11621. CSphString sError;
  11622. sError.SetSprintf ( "index '%s': FLUSH RAMCHUNK failed; INDEX UNUSABLE (%s)", tStmt.m_sIndex.cstr(), pRt->GetLastError().cstr() );
  11623. tOut.Error ( tStmt.m_sStmt, sError.cstr () );
  11624. g_pLocalIndexes->Delete ( tStmt.m_sIndex );
  11625. return;
  11626. }
  11627. tOut.Ok();
  11628. }
  11629. void HandleMysqlFlush ( RowBuffer_i & tOut, const SqlStmt_t & )
  11630. {
  11631. int iTag = CommandFlush();
  11632. tOut.HeadBegin(1);
  11633. tOut.HeadColumn ( "tag", MYSQL_COL_LONG );
  11634. tOut.HeadEnd();
  11635. // data packet, var value
  11636. tOut.PutNumAsString ( iTag );
  11637. tOut.Commit();
  11638. // done
  11639. tOut.Eof();
  11640. }
  11641. // stuff for command 'debug', isolated
  11642. inline static CSphString strSHA1 ( const CSphString& sLine )
  11643. {
  11644. return CalcSHA1 ( sLine.cstr(), sLine.Length() );
  11645. }
  11646. int GetLogFD ()
  11647. {
  11648. if ( g_bLogStdout && g_iLogFile!=STDOUT_FILENO )
  11649. return STDOUT_FILENO;
  11650. return g_iLogFile;
  11651. }
  11652. void HandleMysqlOptimizeManual ( RowBuffer_i & tOut, const DebugCmd::DebugCommand_t & tCmd )
  11653. {
  11654. if ( !sphCheckWeCanModify ( "optimize", tOut ) )
  11655. return;
  11656. auto sIndex = tCmd.m_sParam;
  11657. auto pIndex = GetServed ( sIndex );
  11658. if ( !ServedDesc_t::IsMutable ( pIndex ) )
  11659. {
  11660. tOut.Error ( tCmd.m_szStmt, "MERGE requires an existing RT index" );
  11661. return;
  11662. }
  11663. OptimizeTask_t tTask;
  11664. tTask.m_eVerb = OptimizeTask_t::eMerge;
  11665. tTask.m_iFrom = (int)tCmd.m_iPar1;
  11666. tTask.m_iTo = (int)tCmd.m_iPar2;
  11667. tTask.m_bByOrder = !tCmd.bOpt ( "byid", session::GetOptimizeById() );
  11668. tTask.m_iCutoff = (int)tCmd.iOpt("cutoff");
  11669. tTask.m_sIndex = std::move (sIndex);
  11670. if ( tCmd.bOpt ( "sync" ) )
  11671. RIdx_T<RtIndex_i*> ( pIndex )->Optimize ( std::move ( tTask ) );
  11672. else
  11673. RunOptimizeRtIndex ( std::move ( tTask ) );
  11674. tOut.Ok();
  11675. }
  11676. // command 'drop [chunk] X [from] <IDX> [option...]'
  11677. void HandleMysqlDropManual ( RowBuffer_i & tOut, const DebugCmd::DebugCommand_t & tCmd )
  11678. {
  11679. if ( !sphCheckWeCanModify ( "drop", tOut ) )
  11680. return;
  11681. auto sIndex = tCmd.m_sParam;
  11682. auto pIndex = GetServed ( sIndex );
  11683. if ( !ServedDesc_t::IsMutable ( pIndex ) )
  11684. {
  11685. tOut.Error ( tCmd.m_szStmt, "DROP requires an existing RT index" );
  11686. return;
  11687. }
  11688. OptimizeTask_t tTask;
  11689. tTask.m_eVerb = OptimizeTask_t::eDrop;
  11690. tTask.m_iFrom = (int)tCmd.m_iPar1;
  11691. tTask.m_bByOrder = !tCmd.bOpt ( "byid", session::GetOptimizeById() );
  11692. tTask.m_sIndex = std::move ( sIndex );
  11693. if ( tCmd.bOpt ( "sync" ) )
  11694. RIdx_T<RtIndex_i*> ( pIndex )->Optimize ( std::move ( tTask ) );
  11695. else
  11696. RunOptimizeRtIndex ( std::move ( tTask ) );
  11697. tOut.Ok();
  11698. }
  11699. void HandleMysqlCompress ( RowBuffer_i & tOut, const DebugCmd::DebugCommand_t & tCmd )
  11700. {
  11701. if ( !sphCheckWeCanModify ( "compress", tOut ) )
  11702. return;
  11703. auto sIndex = tCmd.m_sParam;
  11704. auto pIndex = GetServed ( sIndex );
  11705. if ( !ServedDesc_t::IsMutable ( pIndex ) )
  11706. {
  11707. tOut.Error ( tCmd.m_szStmt, "COMPRESS requires an existing RT index" );
  11708. return;
  11709. }
  11710. OptimizeTask_t tTask;
  11711. tTask.m_eVerb = OptimizeTask_t::eCompress;
  11712. tTask.m_iFrom = (int) tCmd.m_iPar1;
  11713. tTask.m_bByOrder = !tCmd.bOpt ( "byid", session::GetOptimizeById() );
  11714. tTask.m_sIndex = std::move ( sIndex );
  11715. if ( tCmd.bOpt ( "sync" ) )
  11716. RIdx_T<RtIndex_i*> ( pIndex )->Optimize ( std::move ( tTask ) );
  11717. else
  11718. RunOptimizeRtIndex ( std::move ( tTask ) );
  11719. tOut.Ok();
  11720. }
  11721. // command 'split <IDX> [chunk] N on @uservar [option...]'
  11722. // IDX is tCmd.m_sParam
  11723. // chunk is tCmd.m_iPar1
  11724. // uservar is tCmd.m_sParam2
  11725. void HandleMysqlSplit ( RowBuffer_i & tOut, const DebugCmd::DebugCommand_t & tCmd )
  11726. {
  11727. if ( !sphCheckWeCanModify ( "split", tOut ) )
  11728. return;
  11729. // check index existance
  11730. auto sIndex = tCmd.m_sParam;
  11731. auto pIndex = GetServed ( sIndex );
  11732. if ( !ServedDesc_t::IsMutable ( pIndex ) )
  11733. {
  11734. tOut.Error ( tCmd.m_szStmt, "SPLIT requires an existing RT index" );
  11735. return;
  11736. }
  11737. bool bVarFound = false;
  11738. IterateUservars ( [&tCmd, &bVarFound] ( const NamedRefVectorPair_t & dVar ) {
  11739. if ( dVar.first == tCmd.m_sParam2
  11740. // && dVar.second.m_eType==USERVAR_INT_SET_TMP // uncomment this to split only by session (result of delete .. store) variables
  11741. )
  11742. bVarFound = true;
  11743. } );
  11744. if ( !bVarFound )
  11745. {
  11746. tOut.Error ( tCmd.m_szStmt, "SPLIT requires an existing session @uservar" );
  11747. return;
  11748. }
  11749. OptimizeTask_t tTask;
  11750. tTask.m_eVerb = OptimizeTask_t::eSplit;
  11751. tTask.m_iFrom = (int)tCmd.m_iPar1;
  11752. tTask.m_sUvarFilter = tCmd.m_sParam2;
  11753. tTask.m_bByOrder = !tCmd.bOpt ( "byid", session::GetOptimizeById() );
  11754. tTask.m_sIndex = std::move ( sIndex );
  11755. if ( tCmd.bOpt ( "sync" ) )
  11756. RIdx_T<RtIndex_i*> ( pIndex )->Optimize ( std::move ( tTask ) );
  11757. else
  11758. RunOptimizeRtIndex ( std::move ( tTask ) );
  11759. tOut.Ok();
  11760. }
  11761. void HandleMysqlDebugMeta ( RowBuffer_i & tOut, const DebugCmd::DebugCommand_t & tCmd, const QueryProfile_c & tProfile )
  11762. {
  11763. VectorLike tLike ( tCmd.sOpt ( "like" ) );
  11764. tLike.MatchTupletf ( "pseudo_shards", "%d", tProfile.m_iPseudoShards );
  11765. tLike.MatchTupletf ( "max_matches", "%d", tProfile.m_iMaxMatches );
  11766. tOut.DataTable(tLike);
  11767. }
  11768. void HandleMysqlfiles ( RowBuffer_i & tOut, const DebugCmd::DebugCommand_t & tCmd )
  11769. {
  11770. auto sIndex = tCmd.m_sParam;
  11771. auto pIndex = GetServed ( sIndex );
  11772. if ( !ServedDesc_t::IsLocal ( pIndex ) )
  11773. {
  11774. tOut.Error ( tCmd.m_szStmt, "FILES requires an existing local index" );
  11775. return;
  11776. }
  11777. StrVec_t dFiles;
  11778. StrVec_t dExt;
  11779. RIdx_c ( pIndex )->GetIndexFiles ( dFiles, dExt );
  11780. VectorLike dOut ( 0 );
  11781. dOut.SetColNames ( { "file" } );
  11782. auto sFormat = tCmd.sOpt ( "format" );
  11783. if ( sFormat!="external" )
  11784. dFiles.Apply ( [&dOut] ( const CSphString & a ) { dOut.Add ( a ); } );
  11785. if ( sFormat=="all" || sFormat=="external" )
  11786. {
  11787. dExt.Uniq ();
  11788. dExt.Apply ( [&dOut] ( const CSphString & a ) { dOut.Add ( a ); } );
  11789. }
  11790. tOut.DataTable ( dOut );
  11791. }
  11792. void HandleMysqlclose ( RowBuffer_i & tOut )
  11793. {
  11794. tOut.HeadTuplet ( "command", "result" );
  11795. tOut.DataTuplet ( "Close", "SUCCESS" );
  11796. tOut.Eof ();
  11797. DebugClose();
  11798. }
  11799. // same for select ... from index.files
  11800. void HandleSelectFiles ( RowBuffer_i & tOut, const SqlStmt_t * pStmt )
  11801. {
  11802. tOut.HeadBegin ( 3 );
  11803. tOut.HeadColumn ( "file" );
  11804. tOut.HeadColumn ( "normalized" );
  11805. tOut.HeadColumn ( "size", MYSQL_COL_LONGLONG );
  11806. if ( !tOut.HeadEnd () )
  11807. return;
  11808. const auto & tStmt = *pStmt;
  11809. auto pServed = GetServed ( tStmt.m_sIndex );
  11810. if ( !ServedDesc_t::IsLocal ( pServed ) )
  11811. {
  11812. tOut.Error ( tStmt.m_sStmt, "FILES requires an existing local index" );
  11813. return;
  11814. }
  11815. StrVec_t dFiles;
  11816. StrVec_t dExt;
  11817. RIdx_c ( pServed )->GetIndexFiles ( dFiles, dExt );
  11818. auto sFormat = tStmt.m_sThreadFormat;
  11819. if ( sFormat!="external" )
  11820. ARRAY_CONSTFOREACH( i, dFiles )
  11821. {
  11822. tOut.PutString ( dFiles[i] );
  11823. tOut.PutString ( RealPath ( dFiles[i] ) );
  11824. tOut.PutNumAsString ( sphGetFileSize ( dFiles[i], nullptr ) );
  11825. if ( !tOut.Commit () )
  11826. return;
  11827. }
  11828. if ( sFormat=="all" || sFormat=="external" )
  11829. {
  11830. dExt.Uniq ();
  11831. ARRAY_CONSTFOREACH( i, dExt )
  11832. {
  11833. tOut.PutString ( dExt[i] );
  11834. tOut.PutString ( RealPath ( dExt[i] ) );
  11835. tOut.PutNumAsString ( sphGetFileSize ( dExt[i], nullptr ) );
  11836. if ( !tOut.Commit () )
  11837. return;
  11838. }
  11839. }
  11840. tOut.Eof();
  11841. }
  11842. void HandleShutdownCrash ( RowBuffer_i & tOut, const CSphString & sPasswd, DebugCmd::Cmd_e eCmd )
  11843. {
  11844. const char * szCmd = DebugCmd::dCommands[(BYTE) eCmd].m_szExample;
  11845. if ( g_sShutdownToken.IsEmpty () )
  11846. {
  11847. tOut.Error ( szCmd, "shutdown_token is empty. Provide it in searchd config section." );
  11848. return;
  11849. }
  11850. if ( strSHA1 ( sPasswd )!=g_sShutdownToken )
  11851. {
  11852. tOut.Error ( szCmd, "FAIL" );
  11853. return;
  11854. }
  11855. tOut.HeadTuplet ( "command", "result" );
  11856. tOut.DataTuplet ( szCmd, "SUCCESS" );
  11857. tOut.Eof ();
  11858. if ( eCmd==DebugCmd::Cmd_e::SHUTDOWN )
  11859. {
  11860. #if _WIN32
  11861. sigterm(1);
  11862. #else
  11863. kill ( 0, SIGTERM );
  11864. #endif
  11865. } else // crash
  11866. {
  11867. BYTE * pSegv = (BYTE *) ( 0 );
  11868. *pSegv = 'a';
  11869. }
  11870. }
  11871. #if !_WIN32
  11872. void HandleProcDump ( RowBuffer_i & tOut )
  11873. {
  11874. tOut.HeadTuplet ( "command", "result" );
  11875. if ( g_iParentPID<=0 )
  11876. tOut.DataTuplet ( "procdump", "Unavailable (no watchdog)" );
  11877. else
  11878. {
  11879. kill ( g_iParentPID, SIGUSR1 );
  11880. tOut.DataTupletf ( "procdump", "Sent USR1 to wathcdog (%d)", g_iParentPID );
  11881. }
  11882. tOut.Eof ();
  11883. }
  11884. void HandleGdbStatus ( RowBuffer_i & tOut )
  11885. {
  11886. tOut.HeadTuplet ( "command", "result" );
  11887. const auto & g_bSafeGDB = getSafeGDB ();
  11888. if ( g_iParentPID>0 )
  11889. tOut.DataTupletf ( "setgdb", "Enabled, managed by watchdog (pid=%d)", g_iParentPID );
  11890. else if ( g_bSafeGDB )
  11891. tOut.DataTupletf ( "setgdb", "Enabled, managed locally because of jemalloc", g_iParentPID );
  11892. else if ( g_iParentPID==-1 )
  11893. tOut.DataTuplet ( "setgdb", "Enabled locally, MAY HANG!" );
  11894. else
  11895. tOut.DataTuplet ( "setgdb", "Disabled" );
  11896. tOut.Eof ();
  11897. }
  11898. void HandleSetGdb ( RowBuffer_i & tOut, bool bParam )
  11899. {
  11900. tOut.HeadTuplet ( "command", "result" );
  11901. const auto & g_bSafeGDB = getSafeGDB ();
  11902. if ( g_iParentPID>0 )
  11903. tOut.DataTupletf ( "setgdb", "Enabled by watchdog (pid=%d)", g_iParentPID );
  11904. else if ( g_bSafeGDB )
  11905. tOut.DataTuplet ( "setgdb", "Enabled locally because of jemalloc" );
  11906. else if ( bParam )
  11907. {
  11908. g_iParentPID = -1;
  11909. tOut.DataTuplet ( "setgdb", "Ok, enabled locally, MAY HANG!" );
  11910. } else if ( !bParam )
  11911. {
  11912. g_iParentPID = 0;
  11913. tOut.DataTuplet ( "setgdb", "Ok, disabled" );
  11914. }
  11915. tOut.Eof ();
  11916. }
  11917. void HandleWait ( RowBuffer_i& tOutBuf, const DebugCmd::DebugCommand_t& tCmd )
  11918. {
  11919. auto iTimeoutS = tCmd.iOpt ( "timeout" );
  11920. auto sCluster = tCmd.m_sParam;
  11921. auto iTime = -sphMicroTimer();
  11922. auto sState = WaitClusterReady ( sCluster, iTimeoutS );
  11923. iTime += sphMicroTimer();
  11924. VectorLike tOut { tCmd.sOpt ( "like" ) };
  11925. tOut.SetColName("name");
  11926. tOut.MatchTuplet ( "cluster", sCluster.cstr() );
  11927. tOut.MatchTuplet ( "state", sState.cstr() );
  11928. tOut.MatchTupletf ( "time", "%.2t", iTime );
  11929. tOutBuf.DataTable ( tOut );
  11930. }
  11931. void HandleWaitStatus ( RowBuffer_i& tOutBuf, const DebugCmd::DebugCommand_t& tCmd )
  11932. {
  11933. auto iTimeoutS = tCmd.iOpt ( "timeout" );
  11934. auto sCluster = tCmd.m_sParam;
  11935. auto iTxn = (int)tCmd.m_iPar1;
  11936. auto iTime = -sphMicroTimer();
  11937. auto tAchieved = WaitClusterCommit ( sCluster, iTxn, iTimeoutS );
  11938. iTime += sphMicroTimer();
  11939. VectorLike tOut { tCmd.sOpt ( "like" ) };
  11940. tOut.SetColName ( "name" );
  11941. tOut.MatchTuplet ( "cluster", sCluster.cstr() );
  11942. tOut.MatchTupletf ( "wanted", "%d", iTxn );
  11943. if ( tAchieved.first>=0 )
  11944. tOut.MatchTupletf ( "state", "%d", tAchieved.first );
  11945. else
  11946. tOut.MatchTuplet ( "achieved", tAchieved.second.cstr() );
  11947. tOut.MatchTupletf ( "time", "%.2t", iTime );
  11948. tOutBuf.DataTable ( tOut );
  11949. }
  11950. #endif
  11951. void HandleTrace ( RowBuffer_i& tOut, const DebugCmd::DebugCommand_t& tCmd )
  11952. {
  11953. tOut.HeadTuplet ( "command", "result" );
  11954. #ifdef PERFETTO
  11955. if ( tCmd.m_sParam.IsEmpty() )
  11956. {
  11957. if ( !tCmd.m_iPar1 )
  11958. {
  11959. Tracer::Stop();
  11960. }
  11961. } else
  11962. {
  11963. Tracer::Start ( tCmd.m_sParam, tCmd.m_iPar1 );
  11964. }
  11965. tOut.DataTuplet ( "debug trace ...", "SUCCESS" );
  11966. #else
  11967. tOut.DataTuplet ( "debug trace ...", "FAIL, need to rebuild with Perfetto, look to src/perfetto/README.txt" );
  11968. #endif
  11969. tOut.Eof();
  11970. }
  11971. void HandleToken ( RowBuffer_i & tOut, const CSphString & sParam )
  11972. {
  11973. auto sSha = strSHA1 ( sParam );
  11974. tOut.HeadTuplet ( "command", "result" );
  11975. tOut.DataTuplet ( "debug token", sSha.cstr () );
  11976. tOut.Eof ();
  11977. }
  11978. #if HAVE_MALLOC_STATS
  11979. void HandleMallocStats ( RowBuffer_i & tOut, const CSphString& sParam )
  11980. {
  11981. tOut.HeadTuplet ( "command", "result" );
  11982. // check where is stderr...
  11983. int iOldErr = ::dup ( STDERR_FILENO );
  11984. ::dup2 ( GetLogFD (), STDERR_FILENO );
  11985. sphMallocStats ( sParam.cstr() );
  11986. ::close ( STDERR_FILENO );
  11987. ::dup2 ( iOldErr, STDERR_FILENO );
  11988. ::close ( iOldErr );
  11989. tOut.DataTuplet ( "malloc_stats", g_sLogFile.cstr () );
  11990. tOut.Eof ();
  11991. }
  11992. #endif
  11993. #if HAVE_MALLOC_TRIM
  11994. void HandleMallocTrim ( RowBuffer_i & tOut )
  11995. {
  11996. tOut.HeadTuplet ( "command", "result" );
  11997. CSphString sResult;
  11998. sResult.SetSprintf ( "%d", PerformMallocTrim ( 0 ) );
  11999. tOut.DataTuplet ( "malloc_trim", sResult.cstr () );
  12000. tOut.Eof ();
  12001. }
  12002. #endif
  12003. void HandleSleep ( RowBuffer_i & tOut, int64_t iParam )
  12004. {
  12005. int64_t tmStart = sphMicroTimer ();
  12006. Threads::Coro::SleepMsec ( Max ( iParam/1000, 1 ) );
  12007. int64_t tmDelta = sphMicroTimer ()-tmStart;
  12008. tOut.HeadTuplet ( "command", "result" );
  12009. CSphString sResult;
  12010. sResult.SetSprintf ( "%.3f", (float) tmDelta / 1000000.0f );
  12011. tOut.DataTuplet ( "sleep", sResult.cstr () );
  12012. tOut.Eof ();
  12013. }
  12014. void HandleTasks ( RowBuffer_i & tOut )
  12015. {
  12016. if (!tOut.HeadOfStrings ( { "Name", "MaxRunners", "CurrentRunners", "TotalSpent", "LastFinished", "Executed", "Dropped", "Enqueued" } ))
  12017. return;
  12018. auto dTasks = TaskManager::GetTaskInfo ();
  12019. for ( const auto & dTask : dTasks )
  12020. {
  12021. tOut.PutString ( dTask.m_sName );
  12022. if ( dTask.m_iMaxRunners > 0 )
  12023. tOut.PutNumAsString ( dTask.m_iMaxRunners );
  12024. else
  12025. tOut.PutString ( "unlimited" );
  12026. tOut.PutNumAsString ( dTask.m_iCurrentRunners );
  12027. tOut.PutTimeAsString ( dTask.m_iTotalSpent );
  12028. tOut.PutTimestampAsString ( dTask.m_iLastFinished );
  12029. tOut.PutNumAsString ( dTask.m_iTotalRun );
  12030. tOut.PutNumAsString ( dTask.m_iTotalDropped );
  12031. tOut.PutNumAsString ( dTask.m_iAllRunners );
  12032. if ( !tOut.Commit () )
  12033. return;
  12034. }
  12035. tOut.Eof ();
  12036. }
  12037. void HandleSched ( RowBuffer_i & tOut )
  12038. {
  12039. if (!tOut.HeadOfStrings ( { "Time rest", "Task" } ))
  12040. return;
  12041. auto dTasks = sph::GetSchedInfo ();
  12042. for ( auto& dTask : dTasks )
  12043. {
  12044. tOut.PutTimestampAsString ( dTask.m_iTimeoutStamp );
  12045. tOut.PutString ( dTask.m_sTask );
  12046. if (!tOut.Commit ())
  12047. return;
  12048. }
  12049. tOut.Eof ();
  12050. }
  12051. void HandleMysqlDebug ( RowBuffer_i &tOut, Str_t sCommand, const QueryProfile_c & tProfile )
  12052. {
  12053. using namespace DebugCmd;
  12054. CSphString sError;
  12055. bool bVipConn = session::GetVip ();
  12056. auto tCmd = ParseDebugCmd ( sCommand, sError );
  12057. if ( bVipConn )
  12058. {
  12059. switch ( tCmd.m_eCommand )
  12060. {
  12061. case Cmd_e::SHUTDOWN:
  12062. case Cmd_e::CRASH: HandleShutdownCrash ( tOut, tCmd.m_sParam, tCmd.m_eCommand ); return;
  12063. #if !_WIN32
  12064. case Cmd_e::PROCDUMP: HandleProcDump ( tOut ); return;
  12065. case Cmd_e::SETGDB: HandleSetGdb ( tOut, tCmd.m_iPar1!=0 ); return;
  12066. case Cmd_e::GDBSTATUS: HandleGdbStatus ( tOut ); return;
  12067. #endif
  12068. default: break;
  12069. }
  12070. }
  12071. switch ( tCmd.m_eCommand )
  12072. {
  12073. #if HAVE_MALLOC_STATS
  12074. case Cmd_e::MALLOC_STATS: HandleMallocStats ( tOut, tCmd.m_sParam ); return;
  12075. #endif
  12076. #if HAVE_MALLOC_TRIM
  12077. case Cmd_e::MALLOC_TRIM: HandleMallocTrim ( tOut ); return;
  12078. #endif
  12079. case Cmd_e::TOKEN: HandleToken ( tOut, tCmd.m_sParam ); return;
  12080. case Cmd_e::SLEEP: HandleSleep ( tOut, tCmd.m_iPar1 ); return;
  12081. case Cmd_e::TASKS: HandleTasks ( tOut ); return;
  12082. case Cmd_e::SCHED: HandleSched ( tOut ); return;
  12083. case Cmd_e::MERGE: HandleMysqlOptimizeManual ( tOut, tCmd ); return;
  12084. case Cmd_e::DROP: HandleMysqlDropManual ( tOut, tCmd ); return;
  12085. case Cmd_e::FILES: HandleMysqlfiles ( tOut, tCmd ); return;
  12086. case Cmd_e::CLOSE: HandleMysqlclose ( tOut ); return;
  12087. case Cmd_e::COMPRESS: HandleMysqlCompress ( tOut, tCmd ); return;
  12088. case Cmd_e::SPLIT: HandleMysqlSplit ( tOut, tCmd ); return;
  12089. case Cmd_e::META: HandleMysqlDebugMeta ( tOut, tCmd, tProfile ); return;
  12090. #if !_WIN32
  12091. case Cmd_e::WAIT: HandleWait ( tOut, tCmd ); return;
  12092. case Cmd_e::WAIT_STATUS: HandleWaitStatus ( tOut, tCmd ); return;
  12093. #endif
  12094. case Cmd_e::TRACE: HandleTrace ( tOut, tCmd ); return;
  12095. default: break;
  12096. }
  12097. // no known command; provide short help.
  12098. BYTE uMask = bVipConn ? DebugCmd::NEED_VIP : DebugCmd::NONE;
  12099. #if !_WIN32
  12100. uMask |= DebugCmd::NO_WIN;
  12101. #endif
  12102. #if HAVE_MALLOC_STATS
  12103. uMask |= DebugCmd::MALLOC_STATS;
  12104. #endif
  12105. #if HAVE_MALLOC_TRIM
  12106. uMask |= DebugCmd::MALLOC_TRIM;
  12107. #endif
  12108. // display a short help
  12109. tOut.HeadTuplet ( "command", "meaning" );
  12110. tOut.DataTuplet ( "flush logs", "emulate USR1 signal" );
  12111. tOut.DataTuplet ( "reload indexes", "emulate HUP signal" );
  12112. for ( const auto& dCommand : DebugCmd::dCommands )
  12113. if ( ( dCommand.m_uTraits & uMask )==dCommand.m_uTraits )
  12114. tOut.DataTuplet ( dCommand.m_szExample, dCommand.m_szExplanation );
  12115. tOut.Eof ();
  12116. }
  12117. // fwd
  12118. static bool PrepareReconfigure ( const CSphString & sIndex, CSphReconfigureSettings & tSettings, CSphString & sError );
  12119. void HandleMysqlTruncate ( RowBuffer_i & tOut, const SqlStmt_t & tStmt )
  12120. {
  12121. if ( !sphCheckWeCanModify ( tStmt.m_sStmt, tOut ) )
  12122. return;
  12123. bool bReconfigure = ( tStmt.m_iIntParam==1 );
  12124. auto pCmd = MakeReplicationCommand ( ReplicationCommand_e::TRUNCATE, tStmt.m_sIndex, tStmt.m_sCluster );
  12125. CSphString sError;
  12126. const CSphString & sIndex = tStmt.m_sIndex;
  12127. if ( bReconfigure )
  12128. {
  12129. pCmd->m_tReconfigure = std::make_unique<CSphReconfigureSettings>();
  12130. pCmd->m_tReconfigure->m_bChangeSchema = true;
  12131. }
  12132. if ( bReconfigure && !PrepareReconfigure ( sIndex, *pCmd->m_tReconfigure, sError ) )
  12133. {
  12134. tOut.Error ( tStmt.m_sStmt, sError.cstr () );
  12135. return;
  12136. }
  12137. // get an exclusive lock for operation
  12138. // but only read lock for check
  12139. {
  12140. auto pIndex = GetServed ( sIndex );
  12141. if ( !ServedDesc_t::IsMutable ( pIndex ) )
  12142. {
  12143. tOut.Error ( tStmt.m_sStmt, "TRUNCATE RTINDEX requires an existing RT index" );
  12144. return;
  12145. }
  12146. if ( !CheckIndexCluster ( sIndex, *pIndex, tStmt.m_sCluster, IsHttpStmt ( tStmt ), sError ) )
  12147. {
  12148. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  12149. return;
  12150. }
  12151. }
  12152. auto* pSession = session::GetClientSession();
  12153. auto& tAcc = pSession->m_tAcc;
  12154. auto* pAccum = tAcc.GetAcc();
  12155. pAccum->m_dCmd.Add ( std::move ( pCmd ) );
  12156. bool bRes = HandleCmdReplicate ( *pAccum, sError );
  12157. if ( !bRes )
  12158. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  12159. else
  12160. tOut.Ok();
  12161. }
  12162. void HandleMysqlOptimize ( RowBuffer_i & tOut, const SqlStmt_t & tStmt )
  12163. {
  12164. if ( !sphCheckWeCanModify ( tStmt.m_sStmt, tOut ) )
  12165. return;
  12166. auto sIndex = tStmt.m_sIndex;
  12167. auto pIndex = GetServed ( sIndex );
  12168. if ( !ServedDesc_t::IsMutable ( pIndex ) )
  12169. {
  12170. tOut.Error ( tStmt.m_sStmt, "OPTIMIZE INDEX requires an existing RT index" );
  12171. return;
  12172. }
  12173. OptimizeTask_t tTask;
  12174. tTask.m_eVerb = OptimizeTask_t::eManualOptimize;
  12175. tTask.m_iCutoff = tStmt.m_tQuery.m_iCutoff<=0 ? 0 : tStmt.m_tQuery.m_iCutoff;
  12176. tTask.m_sIndex = std::move ( sIndex );
  12177. if ( tStmt.m_tQuery.m_bSync )
  12178. RIdx_T<RtIndex_i*> ( pIndex )->Optimize ( std::move ( tTask ) );
  12179. else
  12180. RunOptimizeRtIndex ( std::move ( tTask ) );
  12181. tOut.Ok();
  12182. }
  12183. // STMT_SELECT_SYSVAR: SELECT @@sysvar1 [ as alias] [@@sysvarN [ as alias]] [limit M]
  12184. void HandleMysqlSelectSysvar ( RowBuffer_i & tOut, const SqlStmt_t & tStmt )
  12185. {
  12186. struct SysVar_t
  12187. {
  12188. const MysqlColumnType_e m_eType;
  12189. const char * m_sName;
  12190. std::function<CSphString ( void )> m_fnValue;
  12191. };
  12192. auto pVars = session::Info().GetClientSession();
  12193. const SysVar_t dSysvars[] =
  12194. { { MYSQL_COL_STRING, nullptr, [] {return "<empty>";}}, // stub
  12195. { MYSQL_COL_LONG, "@@session.auto_increment_increment", [] {return "1";}},
  12196. { MYSQL_COL_STRING, "@@character_set_client", [] {return "utf8";}},
  12197. { MYSQL_COL_STRING, "@@character_set_connection", [] {return "utf8";}},
  12198. { MYSQL_COL_LONG, "@@max_allowed_packet", [] { StringBuilder_c s; s << g_iMaxPacketSize; return CSphString(s); }},
  12199. { MYSQL_COL_STRING, "@@version_comment", [] { return szGIT_BRANCH_ID;}},
  12200. { MYSQL_COL_LONG, "@@lower_case_table_names", [] { return "1"; }},
  12201. { MYSQL_COL_STRING, "@@session.last_insert_id", [&pVars]
  12202. {
  12203. StringBuilder_c s ( "," );
  12204. pVars->m_dLastIds.Apply ( [&s] ( int64_t iID ) { s << iID; } );
  12205. return CSphString(s);
  12206. }},
  12207. };
  12208. auto fnVar = [&dSysvars] ( const CSphString & sVar )->const SysVar_t &
  12209. {
  12210. for ( const auto & tVar : dSysvars )
  12211. if ( sVar==tVar.m_sName )
  12212. return tVar;
  12213. return dSysvars[0];
  12214. };
  12215. // fill header
  12216. tOut.HeadBegin ( tStmt.m_tQuery.m_dItems.GetLength () );
  12217. for ( const auto& dItem : tStmt.m_tQuery.m_dItems )
  12218. tOut.HeadColumn ( dItem.m_sAlias.cstr (), fnVar ( dItem.m_sExpr ).m_eType );
  12219. tOut.HeadEnd ();
  12220. // fill values
  12221. for ( const auto & tItem : tStmt.m_tQuery.m_dItems )
  12222. tOut.PutString ( fnVar ( tItem.m_sExpr ).m_fnValue().cstr() );
  12223. // finalize
  12224. tOut.Commit ();
  12225. tOut.Eof ();
  12226. }
  12227. struct ExtraLastInsertID_t : public ISphExtra
  12228. {
  12229. explicit ExtraLastInsertID_t ( const CSphVector<int64_t> & dIds )
  12230. : m_dIds ( dIds )
  12231. {}
  12232. bool ExtraDataImpl ( ExtraData_e eCmd, void ** pData ) override
  12233. {
  12234. if ( eCmd==EXTRA_GET_LAST_INSERT_ID )
  12235. {
  12236. StringBuilder_c tBuf ( "," );
  12237. for ( int64_t iID : m_dIds )
  12238. tBuf.Appendf ( INT64_FMT, iID );
  12239. auto * sVal = ( CSphString *)pData;
  12240. assert ( sVal );
  12241. *sVal = tBuf.cstr();
  12242. return true;
  12243. }
  12244. return false;
  12245. }
  12246. const CSphVector<int64_t> & m_dIds;
  12247. };
  12248. void HandleMysqlSelectDual ( RowBuffer_i & tOut, const SqlStmt_t & tStmt )
  12249. {
  12250. CSphString sVar = tStmt.m_tQuery.m_sQuery;
  12251. CSphSchema tSchema;
  12252. ESphAttr eAttrType;
  12253. CSphString sError;
  12254. ExprParseArgs_t tExprArgs;
  12255. tExprArgs.m_pAttrType = &eAttrType;
  12256. CSphRefcountedPtr<ISphExpr> pExpr { sphExprParse ( sVar.cstr(), tSchema, sError, tExprArgs ) };
  12257. if ( !pExpr )
  12258. {
  12259. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  12260. return;
  12261. }
  12262. tOut.HeadBegin(1);
  12263. tOut.HeadColumn ( sVar.cstr() );
  12264. tOut.HeadEnd();
  12265. auto pVars = session::Info().GetClientSession();
  12266. ExtraLastInsertID_t tIds ( pVars->m_dLastIds );
  12267. pExpr->Command ( SPH_EXPR_SET_EXTRA_DATA, &tIds );
  12268. CSphMatch tMatch;
  12269. const BYTE * pStr = nullptr;
  12270. switch ( eAttrType )
  12271. {
  12272. case SPH_ATTR_STRINGPTR:
  12273. {
  12274. int iLen = pExpr->StringEval ( tMatch, &pStr );
  12275. tOut.PutArray ( { pStr, iLen } );
  12276. FreeDataPtr ( *pExpr, pStr );
  12277. break;
  12278. }
  12279. case SPH_ATTR_INTEGER: tOut.PutNumAsString ( pExpr->IntEval ( tMatch ) ); break;
  12280. case SPH_ATTR_BIGINT: tOut.PutNumAsString ( pExpr->Int64Eval ( tMatch ) ); break;
  12281. case SPH_ATTR_FLOAT: tOut.PutFloatAsString ( pExpr->Eval ( tMatch ) ); break;
  12282. case SPH_ATTR_DOUBLE: tOut.PutDoubleAsString ( pExpr->Eval ( tMatch ) ); break;
  12283. default:
  12284. tOut.PutNULL();
  12285. break;
  12286. }
  12287. // done
  12288. tOut.Commit();
  12289. tOut.Eof();
  12290. }
  12291. void HandleMysqlShowCollations ( RowBuffer_i & tOut )
  12292. {
  12293. // MySQL Connector/J really expects an answer here
  12294. // field packets
  12295. tOut.HeadBegin(6);
  12296. tOut.HeadColumn ( "Collation" );
  12297. tOut.HeadColumn ( "Charset" );
  12298. tOut.HeadColumn ( "Id", MYSQL_COL_LONGLONG );
  12299. tOut.HeadColumn ( "Default" );
  12300. tOut.HeadColumn ( "Compiled" );
  12301. tOut.HeadColumn ( "Sortlen" );
  12302. tOut.HeadEnd();
  12303. // data packets
  12304. tOut.PutString ( "utf8_general_ci" );
  12305. tOut.PutString ( "utf8" );
  12306. tOut.PutString ( "33" );
  12307. tOut.PutString ( "Yes" );
  12308. tOut.PutString ( "Yes" );
  12309. tOut.PutString ( "1" );
  12310. tOut.Commit();
  12311. // done
  12312. tOut.Eof();
  12313. }
  12314. void HandleMysqlShowCharacterSet ( RowBuffer_i & tOut )
  12315. {
  12316. // MySQL Connector/J really expects an answer here
  12317. // field packets
  12318. tOut.HeadBegin(4);
  12319. tOut.HeadColumn ( "Charset" );
  12320. tOut.HeadColumn ( "Description" );
  12321. tOut.HeadColumn ( "Default collation" );
  12322. tOut.HeadColumn ( "Maxlen" );
  12323. tOut.HeadEnd();
  12324. // data packets
  12325. tOut.PutString ( "utf8" );
  12326. tOut.PutString ( "UTF-8 Unicode" );
  12327. tOut.PutString ( "utf8_general_ci" );
  12328. tOut.PutString ( "3" );
  12329. tOut.Commit();
  12330. // done
  12331. tOut.Eof();
  12332. }
  12333. const char * sphCollationToName ( ESphCollation eColl )
  12334. {
  12335. switch ( eColl )
  12336. {
  12337. case SPH_COLLATION_LIBC_CI: return "libc_ci";
  12338. case SPH_COLLATION_LIBC_CS: return "libc_cs";
  12339. case SPH_COLLATION_UTF8_GENERAL_CI: return "utf8_general_ci";
  12340. case SPH_COLLATION_BINARY: return "binary";
  12341. default: return "unknown";
  12342. }
  12343. }
  12344. static const char * LogLevelName ( ESphLogLevel eLevel )
  12345. {
  12346. switch ( eLevel )
  12347. {
  12348. case SPH_LOG_FATAL: return "fatal";
  12349. case SPH_LOG_WARNING: return "warning";
  12350. case SPH_LOG_INFO: return "info";
  12351. case SPH_LOG_DEBUG: return "debug";
  12352. case SPH_LOG_VERBOSE_DEBUG: return "debugv";
  12353. case SPH_LOG_VERY_VERBOSE_DEBUG: return "debugvv";
  12354. default: return "unknown";
  12355. }
  12356. }
  12357. // SHOW [GLOBAL|SESSION] VARIABLES WHERE variable_name='xxx' [OR variable_name='xxx']
  12358. // SHOW [GLOBAL|SESSION] VARIABLES WHERE variable_name='xxx' [OR variable_name='xxx']
  12359. void HandleMysqlShowVariables ( RowBuffer_i & dRows, const SqlStmt_t & tStmt )
  12360. {
  12361. VectorLike dTable ( tStmt.m_sStringParam );
  12362. {
  12363. auto pVars = session::Info().GetClientSession();
  12364. dTable.MatchTuplet ( "autocommit", pVars->m_bAutoCommit ? "1" : "0" );
  12365. dTable.MatchTupletf ( "auto_optimize", "%d", g_iAutoOptimizeCutoffMultiplier );
  12366. dTable.MatchTupletf ( "optimize_cutoff", "%d", MutableIndexSettings_c::GetDefaults().m_iOptimizeCutoff );
  12367. dTable.MatchTuplet ( "collation_connection", sphCollationToName ( session::GetCollation() ) );
  12368. dTable.MatchTuplet ( "query_log_format", g_eLogFormat==LOG_FORMAT_PLAIN ? "plain" : "sphinxql" );
  12369. dTable.MatchTuplet ( "session_read_only", session::GetReadOnly() ? "1" : "0" );
  12370. dTable.MatchTuplet ( "log_level", LogLevelName ( g_eLogLevel ) );
  12371. dTable.MatchTupletf ( "max_allowed_packet", "%d", g_iMaxPacketSize );
  12372. dTable.MatchTuplet ( "character_set_client", "utf8" );
  12373. dTable.MatchTuplet ( "character_set_connection", "utf8" );
  12374. dTable.MatchTuplet ( "grouping_in_utc", g_bGroupingInUtc ? "1" : "0" );
  12375. dTable.MatchTupletFn ( "last_insert_id" , [&pVars]
  12376. {
  12377. StringBuilder_c tBuf ( "," );
  12378. pVars->m_dLastIds.Apply ( [&tBuf] ( int64_t iID ) { tBuf << iID; } );
  12379. return tBuf;
  12380. });
  12381. }
  12382. dTable.MatchTuplet ( "pseudo_sharding", g_bSplit ? "1" : "0" );
  12383. dTable.MatchTuplet ( "secondary_indexes", GetSecondaryIndexDefault() ? "1" : "0" );
  12384. dTable.MatchTupletFn ( "threads_ex_effective", [] {
  12385. StringBuilder_c tBuf;
  12386. auto x = GetEffectiveBaseDispatcherTemplate();
  12387. auto y = GetEffectivePseudoShardingDispatcherTemplate();
  12388. Dispatcher::RenderTemplates ( tBuf, { x, y } );
  12389. return tBuf;
  12390. } );
  12391. if ( tStmt.m_iIntParam>=0 ) // that is SHOW GLOBAL VARIABLES
  12392. {
  12393. dTable.MatchTupletFn ( "threads_ex", [] {
  12394. StringBuilder_c tBuf;
  12395. auto x = Dispatcher::GetGlobalBaseDispatcherTemplate();
  12396. auto y = Dispatcher::GetGlobalPseudoShardingDispatcherTemplate();
  12397. Dispatcher::RenderTemplates ( tBuf, { x, y } );
  12398. return tBuf;
  12399. } );
  12400. Uservar_e eType = tStmt.m_iIntParam==0 ? USERVAR_INT_SET : USERVAR_INT_SET_TMP;
  12401. IterateUservars ( [&dTable, eType] ( const NamedRefVectorPair_t &dVar ) {
  12402. if ( dVar.second.m_eType==eType )
  12403. dTable.MatchTupletf ( dVar.first.cstr(), "%d", dVar.second.m_pVal ? dVar.second.m_pVal->GetLength() : 0 );
  12404. });
  12405. } else { // that is local (session) variables
  12406. dTable.MatchTupletFn ( "threads_ex", [] {
  12407. StringBuilder_c tBuf;
  12408. auto x = ClientTaskInfo_t::Info().GetBaseDispatcherTemplate();
  12409. auto y = ClientTaskInfo_t::Info().GetPseudoShardingDispatcherTemplate();
  12410. Dispatcher::RenderTemplates ( tBuf, { x, y } );
  12411. return tBuf;
  12412. });
  12413. }
  12414. // fine
  12415. dRows.DataTable ( dTable );
  12416. }
  12417. template <typename FORMATFN>
  12418. static void AddQueryStats ( VectorLike & dStatus, const char * szPrefix, const QueryStats_t & tStats,
  12419. //void (*FormatFn)( StringBuilder_c & sBuf, uint64_t uQueries, uint64_t uStat, const char * sType ) )
  12420. FORMATFN FormatFn )
  12421. {
  12422. using namespace QueryStats;
  12423. static const char * dStatIntervalNames[INTERVAL_TOTAL] =
  12424. {
  12425. "1min",
  12426. "5min",
  12427. "15min",
  12428. "total"
  12429. };
  12430. static const char * dStatTypeNames[TYPE_TOTAL] =
  12431. {
  12432. "avg",
  12433. "min",
  12434. "max",
  12435. "pct95",
  12436. "pct99"
  12437. };
  12438. StringBuilder_c sBuf;
  12439. for ( int i = 0; i < INTERVAL_TOTAL; ++i )
  12440. {
  12441. if ( dStatus.MatchAddf ( "%s_%s", szPrefix, dStatIntervalNames[i] ) )
  12442. {
  12443. sBuf.Clear();
  12444. {
  12445. ScopedComma_c VARIABLE_IS_NOT_USED tRootBlock( sBuf, ", ", R"({"queries":)", "}" );
  12446. sBuf << tStats.m_dStats[i].m_uTotalQueries;
  12447. for ( int j = 0; j < TYPE_TOTAL; ++j )
  12448. FormatFn ( sBuf, tStats.m_dStats[i].m_uTotalQueries,
  12449. tStats.m_dStats[i].m_dData[j], dStatTypeNames[j] );
  12450. }
  12451. dStatus.Add ( sBuf.cstr() );
  12452. }
  12453. }
  12454. }
  12455. static void AddQueryTimeStatsToOutput ( VectorLike & dStatus, const char * szPrefix, const QueryStats_t & tQueryTimeStats )
  12456. {
  12457. AddQueryStats ( dStatus, szPrefix, tQueryTimeStats,
  12458. []( StringBuilder_c & sBuf, uint64_t uQueries, uint64_t uStat, const char * sType )
  12459. {
  12460. uQueries ? sBuf.Sprintf( R"("%s_sec":%.3F)", sType, uStat ) : sBuf.AppendName( sType ) << R"("-")";
  12461. } );
  12462. }
  12463. static void AddFoundRowsStatsToOutput ( VectorLike & dStatus, const char * szPrefix, const QueryStats_t & tRowsFoundStats )
  12464. {
  12465. AddQueryStats ( dStatus, szPrefix, tRowsFoundStats,
  12466. []( StringBuilder_c & sBuf, uint64_t uQueries, uint64_t uStat, const char * sType )
  12467. {
  12468. sBuf.AppendName( sType );
  12469. uQueries ? sBuf << uStat : sBuf << R"("-")";
  12470. } );
  12471. }
  12472. static void AddIndexQueryStats ( VectorLike & dStatus, const ServedStats_c& tStats )
  12473. {
  12474. QueryStats_t tQueryTimeStats, tRowsFoundStats;
  12475. tStats.CalculateQueryStats ( tRowsFoundStats, tQueryTimeStats );
  12476. AddQueryTimeStatsToOutput ( dStatus, "query_time", tQueryTimeStats );
  12477. #ifndef NDEBUG
  12478. QueryStats_t tExactQueryTimeStats, tExactRowsFoundStats;
  12479. tStats.CalculateQueryStatsExact ( tExactQueryTimeStats, tExactRowsFoundStats );
  12480. AddQueryTimeStatsToOutput ( dStatus, "exact_query_time", tQueryTimeStats );
  12481. #endif
  12482. AddFoundRowsStatsToOutput ( dStatus, "found_rows", tRowsFoundStats );
  12483. }
  12484. static void AddFederatedIndexStatus ( const CSphSourceStats & tStats, const CSphString & sName, RowBuffer_i & tOut )
  12485. {
  12486. if (!tOut.HeadOfStrings ( { "Name", "Engine", "Version", "Row_format", "Rows", "Avg_row_length", "Data_length",
  12487. "Max_data_length", "Index_length", "Data_free", "Auto_increment", "Create_time", "Update_time", "Check_time",
  12488. "Collation", "Checksum", "Create_options", "Comment" } ))
  12489. return;
  12490. tOut.PutString ( sName ); // Name
  12491. tOut.PutString ( "InnoDB" ); // Engine
  12492. tOut.PutString ( "10" ); // Version
  12493. tOut.PutString ( "Dynamic" ); // Row_format
  12494. tOut.PutNumAsString ( tStats.m_iTotalDocuments ); // Rows
  12495. tOut.PutString ( "4096" ); // Avg_row_length
  12496. tOut.PutString ( "0" ); // Data_length
  12497. tOut.PutString ( "0" ); // Max_data_length
  12498. tOut.PutString ( "0" ); // Index_length
  12499. tOut.PutString ( "0" ); // Data_free
  12500. tOut.PutString ( "5" ); // Auto_increment
  12501. tOut.PutNULL(); // Create_time
  12502. tOut.PutNULL(); // Update_time
  12503. tOut.PutNULL(); // Check_time
  12504. tOut.PutString ( "utf8" ); // Collation
  12505. tOut.PutNULL(); // Checksum
  12506. tOut.PutString ( "" ); // Create_options
  12507. tOut.PutString ( "" ); // Comment
  12508. tOut.Commit();
  12509. tOut.Eof ();
  12510. }
  12511. static void AddDiskIndexStatus ( VectorLike & dStatus, const CSphIndex * pIndex, bool bMutable )
  12512. {
  12513. auto iDocs = pIndex->GetStats ().m_iTotalDocuments;
  12514. dStatus.MatchTupletf ( "indexed_documents", "%l", iDocs );
  12515. dStatus.MatchTupletf ( "indexed_bytes", "%l", pIndex->GetStats ().m_iTotalBytes );
  12516. const int64_t * pFieldLens = pIndex->GetFieldLens();
  12517. if ( pFieldLens )
  12518. {
  12519. int64_t iTotalTokens = 0;
  12520. for ( int i=0; i < pIndex->GetMatchSchema().GetFieldsCount(); ++i )
  12521. {
  12522. if ( dStatus.MatchAddf ( "field_tokens_%s", pIndex->GetMatchSchema ().GetFieldName ( i ) ) )
  12523. dStatus.Addf( "%l", pFieldLens[i] );
  12524. iTotalTokens += pFieldLens[i];
  12525. }
  12526. dStatus.MatchTupletf ( "total_tokens", "%l", iTotalTokens );
  12527. }
  12528. CSphIndexStatus tStatus;
  12529. pIndex->GetStatus ( &tStatus );
  12530. dStatus.MatchTupletf ( "ram_bytes", "%l", tStatus.m_iRamUse );
  12531. dStatus.MatchTupletf ( "disk_bytes", "%l", tStatus.m_iDiskUse );
  12532. dStatus.MatchTupletf ( "disk_mapped", "%l", tStatus.m_iMapped );
  12533. dStatus.MatchTupletf ( "disk_mapped_cached", "%l", tStatus.m_iMappedResident );
  12534. dStatus.MatchTupletf ( "disk_mapped_doclists", "%l", tStatus.m_iMappedDocs );
  12535. dStatus.MatchTupletf ( "disk_mapped_cached_doclists", "%l", tStatus.m_iMappedResidentDocs );
  12536. dStatus.MatchTupletf ( "disk_mapped_hitlists", "%l", tStatus.m_iMappedHits );
  12537. dStatus.MatchTupletf ( "disk_mapped_cached_hitlists", "%l", tStatus.m_iMappedResidentHits );
  12538. dStatus.MatchTupletf ( "killed_documents", "%l", tStatus.m_iDead );
  12539. dStatus.MatchTupletFn ( "killed_rate", [&tStatus, iDocs] {
  12540. StringBuilder_c sPercent;
  12541. if ( iDocs )
  12542. sPercent.Sprintf ( "%0.2F%%", tStatus.m_iDead * 10000 / iDocs );
  12543. else
  12544. sPercent << "100%";
  12545. return CSphString ( sPercent.cstr () );
  12546. } );
  12547. if ( bMutable )
  12548. {
  12549. dStatus.MatchTupletf ( "ram_chunk", "%l", tStatus.m_iRamChunkSize );
  12550. dStatus.MatchTupletf ( "ram_chunk_segments_count", "%d", tStatus.m_iNumRamChunks );
  12551. dStatus.MatchTupletf ( "disk_chunks", "%d", tStatus.m_iNumChunks );
  12552. dStatus.MatchTupletf ( "mem_limit", "%l", tStatus.m_iMemLimit );
  12553. dStatus.MatchTupletf ( "mem_limit_rate", "%0.2F%%", PercentOf ( tStatus.m_fSaveRateLimit, 1.0, 2 ) );
  12554. dStatus.MatchTupletf ( "ram_bytes_retired", "%l", tStatus.m_iRamRetired );
  12555. dStatus.MatchTupletf ( "tid", "%l", tStatus.m_iTID );
  12556. dStatus.MatchTupletf ( "tid_saved", "%l", tStatus.m_iSavedTID );
  12557. }
  12558. }
  12559. const char * szIndexType ( IndexType_e eType )
  12560. {
  12561. switch ( eType )
  12562. {
  12563. case IndexType_e::PLAIN: return "disk";
  12564. case IndexType_e::TEMPLATE: return "template";
  12565. case IndexType_e::RT: return "rt";
  12566. case IndexType_e::PERCOLATE: return "percolate";
  12567. case IndexType_e::DISTR: return "distributed";
  12568. default: return "unknown";
  12569. }
  12570. }
  12571. static void AddPlainIndexStatus ( RowBuffer_i & tOut, const cServedIndexRefPtr_c& pServed, const ServedStats_c& tStats,
  12572. bool bModeFederated, const CSphString & sName, const CSphString & sPattern )
  12573. {
  12574. assert ( pServed );
  12575. RIdx_c pIndex { pServed };
  12576. assert ( pIndex );
  12577. if ( bModeFederated )
  12578. {
  12579. AddFederatedIndexStatus ( pIndex->GetStats (), sName, tOut );
  12580. return;
  12581. }
  12582. VectorLike dStatus ( sPattern );
  12583. dStatus.MatchTuplet ( "index_type", szIndexType ( pServed->m_eType ) );
  12584. AddDiskIndexStatus ( dStatus, pIndex, ServedDesc_t::IsMutable ( pServed ) );
  12585. AddIndexQueryStats ( dStatus, tStats );
  12586. tOut.DataTable ( dStatus );
  12587. }
  12588. static void AddDistibutedIndexStatus ( RowBuffer_i & tOut, const cDistributedIndexRefPtr_t& pIndex, bool bFederatedUser, const CSphString & sName, const CSphString & sPattern )
  12589. {
  12590. assert ( pIndex );
  12591. if ( bFederatedUser )
  12592. {
  12593. CSphSourceStats tStats;
  12594. tStats.m_iTotalDocuments = 1000; // TODO: check is it worth to query that number from agents
  12595. AddFederatedIndexStatus ( tStats, sName, tOut );
  12596. return;
  12597. }
  12598. VectorLike dStatus ( sPattern );
  12599. dStatus.MatchTuplet( "index_type", "distributed" );
  12600. AddIndexQueryStats ( dStatus, pIndex->m_tStats );
  12601. tOut.DataTable ( dStatus );
  12602. }
  12603. void HandleMysqlShowIndexStatus ( RowBuffer_i & tOut, const SqlStmt_t & tStmt, bool bFederatedUser )
  12604. {
  12605. CSphString sError;
  12606. auto pServed = GetServed ( tStmt.m_sIndex );
  12607. int iChunk = tStmt.m_iIntParam;
  12608. if ( tStmt.m_dIntSubkeys.GetLength ()>=1 )
  12609. iChunk = tStmt.m_dIntSubkeys[0];
  12610. if ( pServed )
  12611. {
  12612. if ( iChunk>=0 && pServed->m_eType == IndexType_e::RT )
  12613. {
  12614. RIdx_T<const RtIndex_i*> ( pServed )->ProcessDiskChunk ( iChunk, [&tOut, &tStmt] ( const CSphIndex* pIndex )
  12615. {
  12616. if ( !pIndex )
  12617. {
  12618. tOut.Error ( tStmt.m_sStmt, "SHOW INDEX STATUS requires an existing index" );
  12619. return;
  12620. }
  12621. VectorLike dStatus ( tStmt.m_sStringParam );
  12622. AddDiskIndexStatus ( dStatus, pIndex, false );
  12623. tOut.DataTable ( dStatus );
  12624. });
  12625. } else
  12626. AddPlainIndexStatus ( tOut, pServed, *pServed->m_pStats, bFederatedUser, tStmt.m_sIndex, tStmt.m_sStringParam );
  12627. return;
  12628. }
  12629. auto pIndex = GetDistr ( tStmt.m_sIndex );
  12630. if ( pIndex )
  12631. AddDistibutedIndexStatus ( tOut, pIndex, bFederatedUser, tStmt.m_sIndex, tStmt.m_sStringParam );
  12632. else
  12633. tOut.Error ( tStmt.m_sStmt, "SHOW INDEX STATUS requires an existing index" );
  12634. }
  12635. void PutIndexStatus ( RowBuffer_i & tOut, const CSphIndex * pIndex )
  12636. {
  12637. tOut.PutString ( pIndex->GetFilename () );
  12638. auto & tStats = pIndex->GetStats ();
  12639. tOut.PutNumAsString ( tStats.m_iTotalDocuments );
  12640. tOut.PutNumAsString ( tStats.m_iTotalBytes );
  12641. CSphIndexStatus tStatus;
  12642. pIndex->GetStatus ( &tStatus );
  12643. tOut.PutNumAsString ( tStatus.m_iRamUse );
  12644. tOut.PutNumAsString ( tStatus.m_iDiskUse );
  12645. tOut.PutNumAsString ( tStatus.m_iMapped );
  12646. tOut.PutNumAsString ( tStatus.m_iMappedResident );
  12647. tOut.PutNumAsString ( tStatus.m_iMappedDocs );
  12648. tOut.PutNumAsString ( tStatus.m_iMappedResidentDocs );
  12649. tOut.PutNumAsString ( tStatus.m_iMappedHits );
  12650. tOut.PutNumAsString ( tStatus.m_iMappedResidentHits );
  12651. tOut.PutNumAsString ( tStatus.m_iDead );
  12652. }
  12653. void HandleSelectIndexStatus ( RowBuffer_i & tOut, const SqlStmt_t * pStmt )
  12654. {
  12655. tOut.HeadBegin ( 13 );
  12656. tOut.HeadColumn ( "chunk_id", MYSQL_COL_LONG );
  12657. tOut.HeadColumn ( "base_name" );
  12658. tOut.HeadColumn ( "indexed_documents", MYSQL_COL_LONG );
  12659. tOut.HeadColumn ( "indexed_bytes", MYSQL_COL_LONGLONG );
  12660. tOut.HeadColumn ( "ram_bytes", MYSQL_COL_LONGLONG );
  12661. tOut.HeadColumn ( "disk_bytes", MYSQL_COL_LONGLONG );
  12662. tOut.HeadColumn ( "disk_mapped", MYSQL_COL_LONGLONG );
  12663. tOut.HeadColumn ( "disk_mapped_cached", MYSQL_COL_LONGLONG );
  12664. tOut.HeadColumn ( "disk_mapped_doclists", MYSQL_COL_LONGLONG );
  12665. tOut.HeadColumn ( "disk_mapped_cached_doclists", MYSQL_COL_LONGLONG );
  12666. tOut.HeadColumn ( "disk_mapped_hitlists", MYSQL_COL_LONGLONG );
  12667. tOut.HeadColumn ( "disk_mapped_cached_hitlists", MYSQL_COL_LONGLONG );
  12668. tOut.HeadColumn ( "killed_documents", MYSQL_COL_LONGLONG );
  12669. if ( !tOut.HeadEnd () )
  12670. return;
  12671. const auto & tStmt = *pStmt;
  12672. auto pServed = GetServed ( tStmt.m_sIndex );
  12673. if ( !ServedDesc_t::IsLocal ( pServed ) )
  12674. {
  12675. tOut.Error ( tStmt.m_sStmt, "select INDEX.status requires an existing index" );
  12676. return;
  12677. }
  12678. RIdx_c pIndex { pServed };
  12679. if ( pIndex->IsRT () )
  12680. {
  12681. auto* pRtIndex = static_cast<const RtIndex_i*> ( pIndex.Ptr() );
  12682. int iChunk = 0;
  12683. bool bKeepIteration = true;
  12684. while ( bKeepIteration )
  12685. {
  12686. pRtIndex->ProcessDiskChunk (iChunk,[&bKeepIteration, &tOut] (const CSphIndex* pChunk) {
  12687. if ( !pChunk )
  12688. {
  12689. bKeepIteration = false;
  12690. return;
  12691. }
  12692. tOut.PutNumAsString ( pChunk->m_iChunk );
  12693. PutIndexStatus ( tOut, pChunk );
  12694. if ( !tOut.Commit () )
  12695. {
  12696. bKeepIteration = false;
  12697. return;
  12698. }
  12699. });
  12700. ++iChunk;
  12701. }
  12702. } else {
  12703. tOut.PutNumAsString ( 0 ); // dummy 'chunk' of non-rt
  12704. PutIndexStatus ( tOut, pIndex );
  12705. tOut.Commit ();
  12706. }
  12707. tOut.Eof();
  12708. }
  12709. void HandleMysqlShowIndexSettings ( RowBuffer_i & tOut, const SqlStmt_t & tStmt )
  12710. {
  12711. CSphString sError;
  12712. auto pServed = GetServed ( tStmt.m_sIndex );
  12713. if ( !pServed )
  12714. {
  12715. tOut.Error ( tStmt.m_sStmt, "SHOW INDEX SETTINGS requires an existing index" );
  12716. return;
  12717. }
  12718. int iChunk = tStmt.m_iIntParam;
  12719. if ( tStmt.m_dIntSubkeys.GetLength ()>=1 )
  12720. iChunk = (int) tStmt.m_dIntSubkeys[0];
  12721. auto fnShowSettings = [&tOut, szStmt=tStmt.m_sStmt] ( const CSphIndex* pIndex )
  12722. {
  12723. if ( !tOut.HeadOfStrings ( { "Variable_name", "Value" } ) )
  12724. return;
  12725. StringBuilder_c tBuf;
  12726. std::unique_ptr<FilenameBuilder_i> pFilenameBuilder = CreateFilenameBuilder ( pIndex->GetName () );
  12727. DumpSettings ( tBuf, *pIndex, pFilenameBuilder.get () );
  12728. tOut.DataTuplet ( "settings", tBuf.cstr () );
  12729. tOut.Eof ();
  12730. };
  12731. if ( iChunk >= 0 && pServed->m_eType == IndexType_e::RT )
  12732. RIdx_T<const RtIndex_i*> ( pServed )->ProcessDiskChunk ( iChunk, fnShowSettings );
  12733. else
  12734. fnShowSettings ( RIdx_c(pServed) );
  12735. }
  12736. void HandleMysqlShowProfile ( RowBuffer_i & tOut, const QueryProfile_c & p, bool bMoreResultsFollow )
  12737. {
  12738. #define SPH_QUERY_STATE(_name,_desc) _desc,
  12739. static const char * dStates [ SPH_QSTATE_TOTAL ] = { SPH_QUERY_STATES };
  12740. #undef SPH_QUERY_STATES
  12741. tOut.HeadBegin ( 4 );
  12742. tOut.HeadColumn ( "Status" );
  12743. tOut.HeadColumn ( "Duration" );
  12744. tOut.HeadColumn ( "Switches" );
  12745. tOut.HeadColumn ( "Percent" );
  12746. tOut.HeadEnd ( bMoreResultsFollow );
  12747. int64_t tmTotal = 0;
  12748. int iCount = 0;
  12749. for ( int i=0; i<SPH_QSTATE_TOTAL; i++ )
  12750. {
  12751. if ( p.m_dSwitches[i]<=0 )
  12752. continue;
  12753. tmTotal += p.m_tmTotal[i];
  12754. iCount += p.m_dSwitches[i];
  12755. }
  12756. char sTime[32];
  12757. for ( int i=0; i<SPH_QSTATE_TOTAL; i++ )
  12758. {
  12759. if ( p.m_dSwitches[i]<=0 )
  12760. continue;
  12761. snprintf ( sTime, sizeof(sTime), "%d.%06d", int(p.m_tmTotal[i]/1000000), int(p.m_tmTotal[i]%1000000) );
  12762. tOut.PutString ( dStates[i] );
  12763. tOut.PutString ( sTime );
  12764. tOut.PutNumAsString ( p.m_dSwitches[i] );
  12765. if ( tmTotal )
  12766. tOut.PutFloatAsString ( 100.0f * p.m_tmTotal[i]/tmTotal, "%.2f" );
  12767. else
  12768. tOut.PutString ( "INF" );
  12769. tOut.Commit();
  12770. }
  12771. snprintf ( sTime, sizeof(sTime), "%d.%06d", int(tmTotal/1000000), int(tmTotal%1000000) );
  12772. tOut.PutString ( "total" );
  12773. tOut.PutString ( sTime );
  12774. tOut.PutNumAsString ( iCount );
  12775. tOut.PutString ( "0" );
  12776. tOut.Commit();
  12777. tOut.Eof ( bMoreResultsFollow );
  12778. }
  12779. static void AddAttrToIndex ( const SqlStmt_t & tStmt, CSphIndex * pIdx, CSphString & sError )
  12780. {
  12781. CSphString sAttrToAdd = tStmt.m_sAlterAttr;
  12782. sAttrToAdd.ToLower();
  12783. bool bIndexed = tStmt.m_uFieldFlags & CSphColumnInfo::FIELD_INDEXED;
  12784. bool bStored = tStmt.m_uFieldFlags & CSphColumnInfo::FIELD_STORED;
  12785. bool bAttribute = tStmt.m_uFieldFlags & CSphColumnInfo::FIELD_IS_ATTRIBUTE; // beware, m.b. true only for strings
  12786. bool bHasAttr = pIdx->GetMatchSchema ().GetAttr ( sAttrToAdd.cstr () );
  12787. bool bHasField = pIdx->GetMatchSchema ().GetFieldIndex ( sAttrToAdd.cstr () )!=-1;
  12788. if ( !bIndexed && bHasAttr )
  12789. {
  12790. sError.SetSprintf ( "'%s' attribute already in schema", sAttrToAdd.cstr () );
  12791. return;
  12792. }
  12793. if ( bIndexed && bHasField )
  12794. {
  12795. sError.SetSprintf ( "'%s' field already in schema", sAttrToAdd.cstr() );
  12796. return;
  12797. }
  12798. if ( !bIndexed && bHasField && tStmt.m_eAlterColType!=SPH_ATTR_STRING )
  12799. {
  12800. sError.SetSprintf ( "cannot add attribute that shadows '%s' field", sAttrToAdd.cstr () );
  12801. return;
  12802. }
  12803. AttrAddRemoveCtx_t tCtx;
  12804. tCtx.m_sName = sAttrToAdd;
  12805. tCtx.m_eType = tStmt.m_eAlterColType;
  12806. tCtx.m_iBits = tStmt.m_iBits;
  12807. tCtx.m_uFlags = tStmt.m_uAttrFlags;
  12808. tCtx.m_eEngine = tStmt.m_eEngine;
  12809. if ( bIndexed || bStored )
  12810. {
  12811. pIdx->AddRemoveField ( true, sAttrToAdd, tStmt.m_uFieldFlags, sError );
  12812. if ( bAttribute )
  12813. pIdx->AddRemoveAttribute ( true, tCtx, sError );
  12814. }
  12815. else
  12816. pIdx->AddRemoveAttribute ( true, tCtx, sError );
  12817. }
  12818. static void RemoveAttrFromIndex ( const SqlStmt_t& tStmt, CSphIndex* pIdx, CSphString& sError )
  12819. {
  12820. CSphString sAttrToRemove = tStmt.m_sAlterAttr;
  12821. sAttrToRemove.ToLower();
  12822. bool bIsAttr = true;
  12823. const CSphColumnInfo * pAttr = pIdx->GetMatchSchema().GetAttr ( sAttrToRemove.cstr() );
  12824. if ( !pAttr )
  12825. {
  12826. pAttr = pIdx->GetMatchSchema ().GetField ( sAttrToRemove.cstr () );
  12827. if ( !pAttr )
  12828. {
  12829. sError.SetSprintf ( "attribute '%s' does not exist", sAttrToRemove.cstr() );
  12830. return;
  12831. }
  12832. bIsAttr = false;
  12833. }
  12834. if ( bIsAttr && ( sAttrToRemove==sphGetDocidName () || sphIsInternalAttr ( *pAttr ) ) )
  12835. {
  12836. sError.SetSprintf ( "unable to remove built-in attribute '%s'", sAttrToRemove.cstr() );
  12837. return;
  12838. }
  12839. if ( bIsAttr && pIdx->GetMatchSchema().GetAttrsCount()==1 )
  12840. {
  12841. sError.SetSprintf ( "unable to remove last attribute '%s'", sAttrToRemove.cstr() );
  12842. return;
  12843. }
  12844. if ( bIsAttr )
  12845. {
  12846. AttrAddRemoveCtx_t tCtx;
  12847. tCtx.m_sName = sAttrToRemove;
  12848. tCtx.m_eType = pAttr->m_eAttrType;
  12849. pIdx->AddRemoveAttribute ( false, tCtx, sError );
  12850. }
  12851. else
  12852. pIdx->AddRemoveField ( false, sAttrToRemove, 0, sError );
  12853. }
  12854. enum class Alter_e
  12855. {
  12856. AddColumn,
  12857. DropColumn,
  12858. RebuildSI
  12859. };
  12860. static void HandleMysqlAlter ( RowBuffer_i & tOut, const SqlStmt_t & tStmt, Alter_e eAction )
  12861. {
  12862. if ( !sphCheckWeCanModify ( tStmt.m_sStmt, tOut ) )
  12863. return;
  12864. MEMORY ( MEM_SQL_ALTER );
  12865. SearchFailuresLog_c dErrors;
  12866. CSphString sError;
  12867. if ( eAction==Alter_e::AddColumn && tStmt.m_eAlterColType==SPH_ATTR_NONE )
  12868. {
  12869. sError.SetSprintf ( "unsupported attribute type '%d'", tStmt.m_eAlterColType );
  12870. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  12871. return;
  12872. }
  12873. StrVec_t dNames;
  12874. ParseIndexList ( tStmt.m_sIndex, dNames );
  12875. if ( dNames.IsEmpty() )
  12876. {
  12877. sError.SetSprintf ( "no such index '%s'", tStmt.m_sIndex.cstr() );
  12878. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  12879. return;
  12880. }
  12881. for ( const auto & sName : dNames )
  12882. if ( !g_pLocalIndexes->Contains ( sName )
  12883. && g_pDistIndexes->Contains ( sName ) )
  12884. {
  12885. sError.SetSprintf ( "ALTER is only supported for local (not distributed) indexes" );
  12886. tOut.Error ( tStmt.m_sStmt, sError.cstr () );
  12887. return;
  12888. }
  12889. for ( const auto &sName : dNames )
  12890. {
  12891. auto pServed = GetServed ( sName );
  12892. if ( !pServed )
  12893. {
  12894. dErrors.Submit ( sName, nullptr, "unknown local index in ALTER request" );
  12895. continue;
  12896. }
  12897. // cluster does not implement ALTER for now
  12898. auto tCluster = IsPartOfCluster ( pServed );
  12899. if ( tCluster )
  12900. {
  12901. dErrors.SubmitEx ( sName, nullptr, "is part of cluster %s, ALTER is not supported for tables in cluster", tCluster->cstr() );
  12902. continue;
  12903. }
  12904. CSphString sAddError;
  12905. if ( eAction==Alter_e::AddColumn )
  12906. AddAttrToIndex ( tStmt, WIdx_c ( pServed ), sAddError );
  12907. else if ( eAction==Alter_e::DropColumn )
  12908. RemoveAttrFromIndex ( tStmt, WIdx_c ( pServed ), sAddError );
  12909. else if ( eAction==Alter_e::RebuildSI )
  12910. {
  12911. WIdx_c ( pServed )->AlterSI ( sAddError );
  12912. }
  12913. if ( !sAddError.IsEmpty() )
  12914. dErrors.Submit ( sName, nullptr, sAddError.cstr() );
  12915. }
  12916. if ( !dErrors.IsEmpty() )
  12917. {
  12918. StringBuilder_c sReport;
  12919. dErrors.BuildReport ( sReport );
  12920. tOut.Error ( tStmt.m_sStmt, sReport.cstr() );
  12921. return;
  12922. }
  12923. tOut.Ok();
  12924. }
  12925. static bool PrepareReconfigure ( const CSphString & sIndex, const CSphConfigSection & hIndex, CSphReconfigureSettings & tSettings, CSphString & sWarning, CSphString & sError )
  12926. {
  12927. std::unique_ptr<FilenameBuilder_i> pFilenameBuilder = CreateFilenameBuilder ( sIndex.cstr() );
  12928. // fixme: report warnings
  12929. tSettings.m_tTokenizer.Setup ( hIndex, sWarning );
  12930. tSettings.m_tDict.Setup ( hIndex, pFilenameBuilder.get(), sWarning );
  12931. tSettings.m_tFieldFilter.Setup ( hIndex, sWarning );
  12932. tSettings.m_tMutableSettings.Load ( hIndex, false, nullptr );
  12933. if ( !sphRTSchemaConfigure ( hIndex, tSettings.m_tSchema, tSettings.m_tIndex, sError, !tSettings.m_bChangeSchema, false ) )
  12934. {
  12935. sError.SetSprintf ( "failed to parse index '%s' schema, error: '%s'", sIndex.cstr(), sError.cstr() );
  12936. return false;
  12937. }
  12938. if ( !tSettings.m_tIndex.Setup ( hIndex, sIndex.cstr(), sWarning, sError ) )
  12939. {
  12940. sError.SetSprintf ( "failed to parse index '%s' settings, error: '%s'", sIndex.cstr(), sError.cstr() );
  12941. return false;
  12942. }
  12943. tSettings.m_tSchema.SetupFlags ( tSettings.m_tIndex, false, nullptr );
  12944. return CheckStoredFields ( tSettings.m_tSchema, tSettings.m_tIndex, sError );
  12945. }
  12946. static bool PrepareReconfigure ( const CSphString & sIndex, CSphReconfigureSettings & tSettings, CSphString & sError )
  12947. {
  12948. CSphConfig hCfg;
  12949. if ( !ParseConfig ( &hCfg, g_sConfigFile.cstr () ) )
  12950. {
  12951. sError.SetSprintf ( "failed to parse config file '%s': %s; using previous settings", g_sConfigFile.cstr (), TlsMsg::szError() );
  12952. return false;
  12953. }
  12954. if ( !hCfg.Exists ( "index" ) )
  12955. {
  12956. sError.SetSprintf ( "failed to find any index in config file '%s'; using previous settings", g_sConfigFile.cstr () );
  12957. return false;
  12958. }
  12959. if ( !hCfg["index"].Exists ( sIndex ) )
  12960. {
  12961. sError.SetSprintf ( "failed to find index '%s' in config file '%s'; using previous settings", sIndex.cstr(), g_sConfigFile.cstr () );
  12962. return false;
  12963. }
  12964. CSphString sWarning;
  12965. return PrepareReconfigure ( sIndex, hCfg["index"][sIndex], tSettings, sWarning, sError );
  12966. }
  12967. // ALTER RTINDEX/TABLE <idx> RECONFIGURE
  12968. static void HandleMysqlReconfigure ( RowBuffer_i & tOut, const SqlStmt_t & tStmt, CSphString & sWarning )
  12969. {
  12970. if ( !sphCheckWeCanModify ( tStmt.m_sStmt, tOut ) )
  12971. return;
  12972. MEMORY ( MEM_SQL_ALTER );
  12973. if ( IsConfigless() )
  12974. {
  12975. tOut.Error ( tStmt.m_sStmt, "ALTER RECONFIGURE is not supported in RT mode" );
  12976. return;
  12977. }
  12978. const CSphString & sIndex = tStmt.m_sIndex;
  12979. auto pServed = GetServed ( tStmt.m_sIndex );
  12980. if ( !ServedDesc_t::IsMutable ( pServed ) )
  12981. {
  12982. tOut.ErrorEx ( tStmt.m_sStmt, "'%s' is absent, or does not support ALTER", sIndex.cstr() );
  12983. return;
  12984. }
  12985. CSphString sError;
  12986. CSphReconfigureSettings tSettings;
  12987. CSphReconfigureSetup tSetup;
  12988. if ( !PrepareReconfigure ( sIndex, tSettings, sError ) )
  12989. {
  12990. tOut.Error ( tStmt.m_sStmt, sError.cstr () );
  12991. return;
  12992. }
  12993. StrVec_t dWarnings;
  12994. WIdx_T<RtIndex_i*> pRT { pServed };
  12995. if ( !pRT->IsSameSettings ( tSettings, tSetup, dWarnings, sError ) && sError.IsEmpty() )
  12996. {
  12997. if ( !pRT->Reconfigure ( tSetup ) )
  12998. {
  12999. sError.SetSprintf ( "index '%s': reconfigure failed; INDEX UNUSABLE (%s)", tStmt.m_sIndex.cstr(), pRT->GetLastError().cstr() );
  13000. g_pLocalIndexes->Delete ( tStmt.m_sIndex );
  13001. }
  13002. }
  13003. sWarning = ConcatWarnings ( dWarnings );
  13004. if ( sError.IsEmpty() )
  13005. tOut.Ok ( 0, dWarnings.GetLength() );
  13006. else
  13007. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13008. }
  13009. static bool ApplyIndexKillList ( const CSphIndex * pIndex, CSphString & sWarning, CSphString & sError, bool bShowMessage = false );
  13010. // STMT_ALTER_KLIST_TARGET: ALTER TABLE index KILLLIST_TARGET = 'string'
  13011. static void HandleMysqlAlterKlist ( RowBuffer_i & tOut, const SqlStmt_t & tStmt, CSphString & sWarning )
  13012. {
  13013. if ( !sphCheckWeCanModify ( tStmt.m_sStmt, tOut ) )
  13014. return;
  13015. MEMORY ( MEM_SQL_ALTER );
  13016. CSphString sError;
  13017. KillListTargets_c tNewTargets;
  13018. if ( !tNewTargets.Parse ( tStmt.m_sAlterOption, tStmt.m_sIndex.cstr(), sError ) )
  13019. {
  13020. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13021. return;
  13022. }
  13023. auto pServed = GetServed ( tStmt.m_sIndex.cstr () );
  13024. if ( !pServed )
  13025. {
  13026. if ( g_pDistIndexes->Contains ( tStmt.m_sIndex ) )
  13027. sError.SetSprintf ( "ALTER is only supported for local (not distributed) indexes" );
  13028. else
  13029. sError.SetSprintf ( "index '%s' not found", tStmt.m_sIndex.cstr () );
  13030. }
  13031. else if ( ServedDesc_t::IsMutable ( pServed ) )
  13032. sError.SetSprintf ( "'%s' does not support ALTER (real-time or percolate)", tStmt.m_sIndex.cstr () );
  13033. if ( !sError.IsEmpty () )
  13034. {
  13035. tOut.Error ( tStmt.m_sStmt, sError.cstr () );
  13036. return;
  13037. }
  13038. WIdx_c pIdx { pServed };
  13039. if ( !pIdx->AlterKillListTarget ( tNewTargets, sError ) )
  13040. {
  13041. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13042. return;
  13043. }
  13044. // apply killlist to new targets
  13045. if ( !ApplyIndexKillList ( pIdx, sWarning, sError ) )
  13046. {
  13047. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13048. return;
  13049. }
  13050. if ( sError.IsEmpty() )
  13051. tOut.Ok();
  13052. else
  13053. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13054. }
  13055. static bool SubstituteExternalIndexFiles ( const StrVec_t & dOldExternalFiles, const StrVec_t & dNewExternalFiles, CSphString & sIndexPath, StrVec_t & dBackupFiles, CSphString & sError )
  13056. {
  13057. StrVec_t dOnlyNew;
  13058. for ( const auto & i : dNewExternalFiles )
  13059. {
  13060. bool bDupe = false;
  13061. for ( const auto & j : dOldExternalFiles )
  13062. bDupe |= i==j;
  13063. if ( !bDupe )
  13064. dOnlyNew.Add(i);
  13065. }
  13066. StrVec_t dOnlyOld;
  13067. for ( const auto & i : dOldExternalFiles )
  13068. {
  13069. bool bDupe = false;
  13070. for ( const auto & j : dNewExternalFiles )
  13071. bDupe |= i==j;
  13072. if ( !bDupe )
  13073. dOnlyOld.Add(i);
  13074. }
  13075. for ( const auto & i : dOnlyOld )
  13076. dBackupFiles.Add().SetSprintf ( "%s.tmp", i.cstr() );
  13077. if ( !RenameWithRollback ( dOnlyOld, dBackupFiles, sError ) )
  13078. return false;
  13079. if ( !dOnlyNew.GetLength() )
  13080. return true;
  13081. StrVec_t dCopied;
  13082. if ( !CopyExternalIndexFiles ( dOnlyNew, sIndexPath, dCopied, sError ) )
  13083. {
  13084. // try to rename files back
  13085. CSphString sTmp;
  13086. RenameFiles ( dBackupFiles, dOnlyOld, sTmp );
  13087. return false;
  13088. }
  13089. return true;
  13090. }
  13091. // STMT_ALTER_INDEX_SETTINGS: ALTER TABLE index [ident = 'string']*
  13092. static void HandleMysqlAlterIndexSettings ( RowBuffer_i & tOut, const SqlStmt_t & tStmt, CSphString & sWarning )
  13093. {
  13094. if ( !sphCheckWeCanModify ( tStmt.m_sStmt, tOut ) )
  13095. return;
  13096. MEMORY ( MEM_SQL_ALTER );
  13097. CSphString sError;
  13098. if ( !IsConfigless() )
  13099. {
  13100. sError = "ALTER TABLE requires data_dir to be set in the config file";
  13101. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13102. return;
  13103. }
  13104. auto pServed = GetServed ( tStmt.m_sIndex.cstr() );
  13105. if ( !pServed || pServed->m_eType != IndexType_e::RT )
  13106. {
  13107. tOut.ErrorEx ( tStmt.m_sStmt, "index '%s' is not found, or not real-time", tStmt.m_sIndex.cstr() );
  13108. return;
  13109. }
  13110. WIdx_T<RtIndex_i*> pRtIndex { pServed };
  13111. // get all table settings as a string
  13112. CSphString sCreateTable = BuildCreateTable ( pRtIndex->GetName(), pRtIndex, pRtIndex->GetInternalSchema() );
  13113. CSphVector<SqlStmt_t> dCreateTableStmts;
  13114. if ( !ParseDdl ( sCreateTable.cstr(), sCreateTable.Length(), dCreateTableStmts, sError ) )
  13115. {
  13116. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13117. return;
  13118. }
  13119. if ( dCreateTableStmts.GetLength()!=1 )
  13120. {
  13121. tOut.Error ( tStmt.m_sStmt, "Unable to alter index settings" );
  13122. return;
  13123. }
  13124. // parse the options string to old-style config hash
  13125. IndexSettingsContainer_c tContainer;
  13126. tContainer.Populate ( dCreateTableStmts[0].m_tCreateTable );
  13127. StrVec_t dOldExternalFiles = tContainer.GetFiles();
  13128. // force override for old options
  13129. for ( const auto & i : tStmt.m_tCreateTable.m_dOpts )
  13130. tContainer.RemoveKeys ( i.m_sName );
  13131. for ( const auto & i : tStmt.m_tCreateTable.m_dOpts )
  13132. {
  13133. if ( !i.m_sValue.IsEmpty() )
  13134. tContainer.AddOption ( i.m_sName, i.m_sValue );
  13135. }
  13136. if ( !tContainer.CheckPaths() )
  13137. {
  13138. tOut.Error ( tStmt.m_sStmt, tContainer.GetError().cstr() );
  13139. return;
  13140. }
  13141. StrVec_t dBackupFiles;
  13142. CSphString sIndexPath = GetPathOnly ( pRtIndex->GetFilename() );
  13143. if ( !SubstituteExternalIndexFiles ( dOldExternalFiles, tContainer.GetFiles(), sIndexPath, dBackupFiles, sError ) )
  13144. {
  13145. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13146. return;
  13147. }
  13148. StrVec_t dWarnings;
  13149. CSphReconfigureSettings tSettings;
  13150. if ( !PrepareReconfigure ( tStmt.m_sIndex, tContainer.AsCfg(), tSettings, sWarning, sError ) )
  13151. {
  13152. tOut.Error ( tStmt.m_sStmt, sError.cstr () );
  13153. return;
  13154. }
  13155. if ( !sWarning.IsEmpty() )
  13156. dWarnings.Add(sWarning);
  13157. CSphReconfigureSetup tSetup;
  13158. bool bSame = pRtIndex->IsSameSettings ( tSettings, tSetup, dWarnings, sError );
  13159. sWarning = ConcatWarnings(dWarnings);
  13160. if ( !bSame && sError.IsEmpty() )
  13161. {
  13162. bool bOk = pRtIndex->Reconfigure(tSetup);
  13163. if ( !bOk )
  13164. {
  13165. sError.SetSprintf ( "index '%s': alter failed; INDEX UNUSABLE (%s)", tStmt.m_sIndex.cstr(), pRtIndex->GetLastError().cstr() );
  13166. g_pLocalIndexes->Delete ( tStmt.m_sIndex );
  13167. }
  13168. }
  13169. if ( sError.IsEmpty() )
  13170. {
  13171. // all ok, delete old files
  13172. for ( const auto & i : dBackupFiles )
  13173. ::unlink ( i.cstr() );
  13174. tOut.Ok ( 0, dWarnings.GetLength() );
  13175. }
  13176. else
  13177. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13178. }
  13179. // STMT_SHOW_PLAN: SHOW PLAN
  13180. static void HandleMysqlShowPlan ( RowBuffer_i & tOut, const QueryProfile_c & p, bool bMoreResultsFollow, bool bDot )
  13181. {
  13182. tOut.HeadBegin ( 2 );
  13183. tOut.HeadColumn ( "Variable" );
  13184. tOut.HeadColumn ( "Value" );
  13185. tOut.HeadEnd ( bMoreResultsFollow );
  13186. tOut.PutString ( "transformed_tree" );
  13187. StringBuilder_c sPlan;
  13188. sph::RenderBsonPlan ( sPlan, bson::MakeHandle ( p.m_dPlan ), bDot );
  13189. tOut.PutString ( sPlan );
  13190. tOut.Commit();
  13191. tOut.Eof ( bMoreResultsFollow );
  13192. }
  13193. // for seamless we create new index and copy it's settings from previous definition. Indexes are NOT linked
  13194. // for greedy we just make light clone (original index add-reffed).
  13195. ServedIndexRefPtr_c MakeCloneForRotation ( const cServedIndexRefPtr_c& pSource, const CSphString& sIndex )
  13196. {
  13197. assert ( pSource->m_eType == IndexType_e::PLAIN );
  13198. auto pRes = MakeServedIndex();
  13199. LightClone ( pRes, pSource );
  13200. if ( g_bSeamlessRotate )
  13201. {
  13202. pRes->SetStatsFrom ( *pSource );
  13203. auto pIdx = sphCreateIndexPhrase ( sIndex.cstr(), pRes->m_sIndexPath.cstr() );
  13204. pIdx->m_iExpansionLimit = g_iExpansionLimit;
  13205. pIdx->SetMutableSettings ( pRes->m_tSettings );
  13206. pIdx->SetGlobalIDFPath ( pRes->m_sGlobalIDFPath );
  13207. pIdx->SetCacheSize ( g_iMaxCachedDocs, g_iMaxCachedHits );
  13208. pRes->SetIdx ( std::move ( pIdx ) );
  13209. } else
  13210. pRes->SetIdxAndStatsFrom ( *pSource );
  13211. return pRes;
  13212. }
  13213. static bool LimitedRotateIndexMT ( ServedIndexRefPtr_c& pNewServed, const CSphString& sIndex, StrVec_t& dWarnings, CSphString& sError ) EXCLUDES ( MainThread );
  13214. static bool RotateIndexGreedy ( const ServedIndex_c& tServed, const char* szIndex, CSphString& sError ) REQUIRES ( tServed.m_pIndex->Locker() );
  13215. static void HandleMysqlReloadIndex ( RowBuffer_i & tOut, const SqlStmt_t & tStmt, CSphString & sWarning )
  13216. {
  13217. CSphString sError;
  13218. cServedIndexRefPtr_c pServed = GetServed ( tStmt.m_sIndex );
  13219. if ( !pServed )
  13220. {
  13221. tOut.ErrorEx ( tStmt.m_sStmt, "unknown local index '%s'", tStmt.m_sIndex.cstr() );
  13222. return;
  13223. }
  13224. if ( ServedDesc_t::IsMutable ( pServed ) )
  13225. {
  13226. tOut.ErrorEx ( tStmt.m_sStmt, "can not reload real-time or percolate index" );
  13227. return;
  13228. }
  13229. if ( tStmt.m_sStringParam == pServed->m_sIndexPath )
  13230. {
  13231. tOut.ErrorEx ( tStmt.m_sStmt, "reload path should be different from current path" );
  13232. return;
  13233. }
  13234. if ( !tStmt.m_sStringParam.IsEmpty () )
  13235. {
  13236. // try move files from arbitrary path to current index path before rotate, if needed.
  13237. // fixme! what about concurrency? if 2 sessions simultaneously ask to rotate,
  13238. // or if we have unapplied rotates from indexer - seems that it will garbage .new files?
  13239. IndexFiles_c sIndexFiles ( pServed->m_sIndexPath );
  13240. if ( !sIndexFiles.RelocateToNew ( tStmt.m_sStringParam ) )
  13241. {
  13242. tOut.Error ( tStmt.m_sStmt, sIndexFiles.ErrorMsg () );
  13243. return;
  13244. }
  13245. }
  13246. StrVec_t dWarnings;
  13247. if ( g_bSeamlessRotate )
  13248. {
  13249. ServedIndexRefPtr_c pNewServed = MakeCloneForRotation ( pServed, tStmt.m_sIndex );
  13250. if ( !LimitedRotateIndexMT ( pNewServed, tStmt.m_sIndex, dWarnings, sError ) )
  13251. {
  13252. sphWarning ( "%s", sError.cstr() );
  13253. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13254. return;
  13255. }
  13256. } else {
  13257. WIdx_c WLock { pServed };
  13258. if ( !RotateIndexGreedy ( *pServed, tStmt.m_sIndex.cstr(), sError ) )
  13259. {
  13260. sphWarning ( "%s", sError.cstr() );
  13261. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13262. g_pLocalIndexes->Delete ( tStmt.m_sIndex ); // since it unusable - no sense just to disable it.
  13263. // fixme! RotateIndexGreedy does prealloc. Do we need to perform/signal preload also?
  13264. return;
  13265. }
  13266. }
  13267. if ( dWarnings.GetLength() )
  13268. {
  13269. StringBuilder_c sWarn ( "; " );
  13270. for ( const auto & i : dWarnings )
  13271. sWarn << i;
  13272. sWarn.MoveTo ( sWarning );
  13273. }
  13274. tOut.Ok();
  13275. }
  13276. void HandleMysqlExplain ( RowBuffer_i & tOut, const SqlStmt_t & tStmt, bool bDot )
  13277. {
  13278. CSphString sProc ( tStmt.m_sCallProc );
  13279. if ( sProc.ToLower()!="query" )
  13280. {
  13281. tOut.ErrorEx ( tStmt.m_sStmt, "no such explain procedure %s", tStmt.m_sCallProc.cstr () );
  13282. return;
  13283. }
  13284. auto pServed = GetServed ( tStmt.m_sIndex );
  13285. if ( !pServed )
  13286. {
  13287. tOut.ErrorEx ( tStmt.m_sStmt, "unknown local index '%s'", tStmt.m_sIndex.cstr ());
  13288. return;
  13289. }
  13290. TlsMsg::Err (); // reset error
  13291. auto dPlan = RIdx_c ( pServed )->ExplainQuery ( tStmt.m_tQuery.m_sQuery );
  13292. if ( TlsMsg::HasErr ())
  13293. {
  13294. tOut.Error ( tStmt.m_sStmt, TlsMsg::szError ());
  13295. return;
  13296. }
  13297. StringBuilder_c sRes;
  13298. sph::RenderBsonPlan ( sRes, bson::MakeHandle ( dPlan ), bDot );
  13299. tOut.HeadBegin ( 2 );
  13300. tOut.HeadColumn ( "Variable" );
  13301. tOut.HeadColumn ( "Value" );
  13302. tOut.HeadEnd ();
  13303. tOut.PutString ( "transformed_tree" );
  13304. tOut.PutString ( sRes );
  13305. tOut.Commit();
  13306. tOut.Eof ();
  13307. }
  13308. void HandleMysqlImportTable ( RowBuffer_i & tOut, const SqlStmt_t & tStmt, CSphString & sWarning )
  13309. {
  13310. if ( !sphCheckWeCanModify ( tStmt.m_sStmt, tOut ) )
  13311. return;
  13312. CSphString sError;
  13313. if ( !IsConfigless() )
  13314. {
  13315. sError = "IMPORT TABLE requires data_dir to be set in the config file";
  13316. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13317. return;
  13318. }
  13319. if ( IndexIsServed ( tStmt.m_sIndex ) )
  13320. {
  13321. sError.SetSprintf ( "index '%s' already exists", tStmt.m_sIndex.cstr() );
  13322. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13323. return;
  13324. }
  13325. bool bPQ = false;
  13326. StrVec_t dWarnings;
  13327. if ( !CopyIndexFiles ( tStmt.m_sIndex, tStmt.m_sStringParam, bPQ, dWarnings, sError ) )
  13328. {
  13329. sError.SetSprintf ( "unable to import index '%s': %s", tStmt.m_sIndex.cstr(), sError.cstr() );
  13330. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13331. return;
  13332. }
  13333. if ( !AddExistingIndexConfigless ( tStmt.m_sIndex, bPQ ? IndexType_e::PERCOLATE : IndexType_e::RT, dWarnings, sError ) )
  13334. {
  13335. sError.SetSprintf ( "unable to import index '%s': %s", tStmt.m_sIndex.cstr(), sError.cstr() );
  13336. tOut.Error ( tStmt.m_sStmt, sError.cstr() );
  13337. return;
  13338. }
  13339. if ( dWarnings.GetLength() )
  13340. {
  13341. StringBuilder_c sWarn ( "; " );
  13342. for ( const auto & i : dWarnings )
  13343. sWarn << i;
  13344. sWarning = sWarn.cstr();
  13345. }
  13346. tOut.Ok();
  13347. }
  13348. //////////////////////////////////////////////////////////////////////////
  13349. void HandleMysqlFreezeIndexes ( RowBuffer_i& tOut, const CSphString& sIndexes, CSphString& sWarningOut )
  13350. {
  13351. // search through specified local indexes
  13352. StrVec_t dIndexes, dNonlockedIndexes, dIndexFiles;
  13353. ParseIndexList ( sIndexes, dIndexes );
  13354. for ( const auto& sIndex : dIndexes )
  13355. {
  13356. auto pIndex = GetServed ( sIndex );
  13357. if ( !ServedDesc_t::IsMutable ( pIndex ) )
  13358. {
  13359. dNonlockedIndexes.Add ( sIndex );
  13360. continue;
  13361. }
  13362. RIdx_T<RtIndex_i*> pRt { pIndex };
  13363. pRt->LockFileState ( dIndexFiles );
  13364. }
  13365. int iWarnings=0;
  13366. if ( !dNonlockedIndexes.IsEmpty() )
  13367. {
  13368. StringBuilder_c sWarning;
  13369. sWarning << "Some indexes are not suitable for freezing: ";
  13370. sWarning.StartBlock();
  13371. dNonlockedIndexes.for_each ( [&sWarning] ( const auto& sValue ) { sWarning << sValue; } );
  13372. sWarning.FinishBlocks ();
  13373. sWarning.MoveTo ( sWarningOut );
  13374. ++iWarnings;
  13375. }
  13376. tOut.HeadBegin ( 2 );
  13377. tOut.HeadColumn ( "file" );
  13378. tOut.HeadColumn ( "normalized" );
  13379. tOut.HeadEnd();
  13380. dIndexFiles.for_each ( [&] ( const auto& sFile ) { tOut.PutString (sFile); tOut.PutString (RealPath (sFile)); tOut.Commit(); } );
  13381. tOut.Eof ( false, iWarnings );
  13382. }
  13383. void HandleMysqlUnfreezeIndexes ( RowBuffer_i& tOut, const CSphString& sIndexes, CSphString& sWarningOut )
  13384. {
  13385. // search through specified local indexes
  13386. StrVec_t dIndexes;
  13387. int iUnlocked=0;
  13388. ParseIndexList ( sIndexes, dIndexes );
  13389. for ( const auto& sIndex : dIndexes )
  13390. {
  13391. auto pIndex = GetServed ( sIndex );
  13392. if ( !ServedDesc_t::IsMutable ( pIndex ) )
  13393. continue;
  13394. RIdx_T<RtIndex_i*> pRt { pIndex };
  13395. pRt->EnableSave ();
  13396. ++iUnlocked;
  13397. }
  13398. tOut.Ok ( iUnlocked );
  13399. }
  13400. void HandleMysqlKill ( RowBuffer_i& tOut, int iKill )
  13401. {
  13402. int iKilled = 0;
  13403. IterateTasks ( [&iKilled, iKill] ( ClientTaskInfo_t* pTask ) {
  13404. if ( pTask && pTask->GetConnID() == iKill && !pTask->GetKilled())
  13405. {
  13406. pTask->SetKilled(true);
  13407. ++iKilled;
  13408. }
  13409. } );
  13410. tOut.Ok ( iKilled );
  13411. }
  13412. RtAccum_t* CSphSessionAccum::GetAcc ( RtIndex_i* pIndex, CSphString& sError )
  13413. {
  13414. assert ( pIndex );
  13415. m_tAcc.emplace_once();
  13416. if ( !pIndex->BindAccum ( &m_tAcc.get(), &sError ) )
  13417. return nullptr;
  13418. return &m_tAcc.get();
  13419. }
  13420. RtAccum_t* CSphSessionAccum::GetAcc()
  13421. {
  13422. m_tAcc.emplace_once();
  13423. return &m_tAcc.get();
  13424. }
  13425. RtIndex_i * CSphSessionAccum::GetIndex ()
  13426. {
  13427. if ( !m_tAcc )
  13428. return nullptr;
  13429. return m_tAcc->GetIndex();
  13430. }
  13431. static bool FixupFederatedQuery ( ESphCollation eCollation, CSphVector<SqlStmt_t> & dStmt, CSphString & sError, CSphString & sFederatedQuery );
  13432. static const CSphString g_sLogDoneStmt = "/* DONE */";
  13433. static const Str_t g_tLogDoneStmt = FromStr ( g_sLogDoneStmt );
  13434. struct LogStmtGuard_t
  13435. {
  13436. LogStmtGuard_t ( const Str_t & sQuery, SqlStmt_e eStmt, bool bMulti )
  13437. {
  13438. m_tmStarted = LogFilterStatementSphinxql ( sQuery, eStmt );
  13439. m_bLogDone = ( m_tmStarted && eStmt!=STMT_UPDATE && eStmt!=STMT_SELECT && !bMulti ); // update and select will log differently
  13440. }
  13441. ~LogStmtGuard_t ()
  13442. {
  13443. if ( m_bLogDone )
  13444. {
  13445. int64_t tmDelta = sphMicroTimer() - m_tmStarted;
  13446. LogStatementSphinxql ( g_tLogDoneStmt, (int)( tmDelta / 1000 ) );
  13447. }
  13448. }
  13449. int64_t m_tmStarted = 0;
  13450. bool m_bLogDone = false;
  13451. };
  13452. void ClientSession_c::FreezeLastMeta()
  13453. {
  13454. m_tLastMeta = CSphQueryResultMeta();
  13455. m_tLastMeta.m_sError = m_sError;
  13456. m_tLastMeta.m_sWarning = "";
  13457. }
  13458. static void HandleMysqlShowSettings ( const CSphConfig & hConf, RowBuffer_i & tOut );
  13459. // just execute one sphinxql statement
  13460. //
  13461. // IMPORTANT! this does NOT start or stop profiling, as there a few external
  13462. // things (client net reads and writes) that we want to profile, too
  13463. //
  13464. // returns true if the current profile should be kept (default)
  13465. // returns false if profile should be discarded (eg. SHOW PROFILE case)
  13466. bool ClientSession_c::Execute ( Str_t sQuery, RowBuffer_i & tOut )
  13467. {
  13468. auto& tSess = session::Info();
  13469. // set on query guard
  13470. tSess.SetTaskState ( TaskState_e::QUERY );
  13471. auto& tCrashQuery = GlobalCrashQueryGetRef();
  13472. tCrashQuery.m_eType = QUERY_SQL;
  13473. tCrashQuery.m_dQuery = { (const BYTE*) sQuery.first, sQuery.second };
  13474. // ad-hoc, make generalized select()
  13475. if ( StrEq ( sQuery.first, "select DATABASE(), USER() limit 1" ) )
  13476. {
  13477. // result set header packet
  13478. tOut.HeadTuplet ( "DATABASE()", "USER()" );
  13479. tOut.DataTuplet ( g_sDbName.cstr(), tSess.GetVip () ? "VIP" : "Usual" );
  13480. tOut.Eof ( false );
  13481. return true;
  13482. }
  13483. // parse SQL query
  13484. if ( tSess.IsProfile() )
  13485. m_tProfile.Switch ( SPH_QSTATE_SQL_PARSE );
  13486. m_sError = "";
  13487. CSphVector<SqlStmt_t> dStmt;
  13488. bool bParsedOK = sphParseSqlQuery ( sQuery.first, sQuery.second, dStmt, m_sError, tSess.GetCollation () );
  13489. if ( tSess.IsProfile() )
  13490. m_tProfile.Switch ( SPH_QSTATE_UNKNOWN );
  13491. SqlStmt_e eStmt = STMT_PARSE_ERROR;
  13492. if ( bParsedOK )
  13493. {
  13494. eStmt = dStmt[0].m_eStmt;
  13495. dStmt[0].m_sStmt = sQuery.first;
  13496. }
  13497. const SqlStmt_e ePrevStmt = m_eLastStmt;
  13498. if ( eStmt!=STMT_SHOW_META )
  13499. m_eLastStmt = eStmt;
  13500. SqlStmt_t * pStmt = dStmt.Begin();
  13501. assert ( !bParsedOK || pStmt );
  13502. myinfo::SetCommand ( g_dSqlStmts[eStmt] );
  13503. LogStmtGuard_t tLogGuard ( sQuery, eStmt, dStmt.GetLength()>1 );
  13504. if ( bParsedOK && m_bFederatedUser )
  13505. {
  13506. if ( !FixupFederatedQuery ( tSess.GetCollation (), dStmt, m_sError, m_sFederatedQuery ) )
  13507. {
  13508. FreezeLastMeta();
  13509. tOut.Error ( sQuery.first, m_sError.cstr() );
  13510. return true;
  13511. }
  13512. }
  13513. // handle multi SQL query
  13514. if ( bParsedOK && dStmt.GetLength()>1 )
  13515. {
  13516. m_sError = "";
  13517. HandleMysqlMultiStmt ( dStmt, m_tLastMeta, tOut, m_sError );
  13518. return true; // FIXME? how does this work with profiling?
  13519. }
  13520. // handle SQL query
  13521. switch ( eStmt )
  13522. {
  13523. case STMT_PARSE_ERROR:
  13524. FreezeLastMeta();
  13525. tOut.Error ( sQuery.first, m_sError.cstr() );
  13526. return true;
  13527. case STMT_SELECT:
  13528. {
  13529. MEMORY ( MEM_SQL_SELECT );
  13530. StatCountCommand ( SEARCHD_COMMAND_SEARCH );
  13531. SearchHandler_c tHandler ( 1, sphCreatePlainQueryParser(), QUERY_SQL, true );
  13532. tHandler.SetQuery ( 0, dStmt.Begin()->m_tQuery, std::move ( dStmt.Begin()->m_pTableFunc ) );
  13533. tHandler.m_pStmt = pStmt;
  13534. if ( tSess.IsProfile() )
  13535. tHandler.SetProfile ( &m_tProfile );
  13536. if ( m_bFederatedUser )
  13537. tHandler.SetFederatedUser();
  13538. if ( HandleMysqlSelect ( tOut, tHandler ) )
  13539. {
  13540. // query just completed ok; reset out error message
  13541. m_sError = "";
  13542. AggrResult_t & tLast = tHandler.m_dAggrResults.Last();
  13543. SendMysqlSelectResult ( tOut, tLast, false, m_bFederatedUser, &m_sFederatedQuery, ( tSess.IsProfile() ? &m_tProfile : nullptr ) );
  13544. }
  13545. // save meta for SHOW META (profile is saved elsewhere)
  13546. m_tLastMeta = tHandler.m_dAggrResults.Last();
  13547. return true;
  13548. }
  13549. case STMT_SHOW_WARNINGS:
  13550. HandleMysqlWarning ( m_tLastMeta, tOut, false );
  13551. return true;
  13552. case STMT_SHOW_STATUS:
  13553. case STMT_SHOW_AGENT_STATUS:
  13554. if ( eStmt==STMT_SHOW_STATUS )
  13555. {
  13556. StatCountCommand ( SEARCHD_COMMAND_STATUS );
  13557. }
  13558. HandleMysqlStatus ( tOut, *pStmt, false );
  13559. return true;
  13560. case STMT_SHOW_META:
  13561. if ( ePrevStmt!=STMT_CALL )
  13562. HandleMysqlMeta ( tOut, *pStmt, m_tLastMeta, false );
  13563. else
  13564. HandleMysqlPercolateMeta ( m_tPercolateMeta, m_tLastMeta.m_sWarning, tOut );
  13565. return true;
  13566. case STMT_INSERT:
  13567. case STMT_REPLACE:
  13568. {
  13569. StmtErrorReporter_c tErrorReporter ( tOut, pStmt->m_sStmt );
  13570. sphHandleMysqlInsert ( tErrorReporter, *pStmt );
  13571. return true;
  13572. }
  13573. case STMT_DELETE:
  13574. {
  13575. StmtErrorReporter_c tErrorReporter ( tOut, pStmt->m_sStmt );
  13576. sphHandleMysqlDelete ( tErrorReporter, *pStmt, sQuery );
  13577. return true;
  13578. }
  13579. case STMT_SET:
  13580. StatCountCommand ( SEARCHD_COMMAND_UVAR );
  13581. HandleMysqlSet ( tOut, *pStmt, m_tAcc );
  13582. return false;
  13583. case STMT_BEGIN:
  13584. {
  13585. StmtErrorReporter_c tErrorReporter ( tOut, pStmt->m_sStmt );
  13586. sphHandleMysqlBegin ( tErrorReporter, sQuery );
  13587. return true;
  13588. }
  13589. case STMT_COMMIT:
  13590. case STMT_ROLLBACK:
  13591. {
  13592. StmtErrorReporter_c tErrorReporter ( tOut, pStmt->m_sStmt );
  13593. sphHandleMysqlCommitRollback ( tErrorReporter, sQuery, eStmt==STMT_COMMIT );
  13594. return true;
  13595. }
  13596. case STMT_CALL:
  13597. // IMPORTANT! if you add a new builtin here, do also add it
  13598. // in the comment to STMT_CALL line in SqlStmt_e declaration,
  13599. // the one that lists expansions for doc/check.pl
  13600. pStmt->m_sCallProc.ToUpper();
  13601. if ( pStmt->m_sCallProc=="SNIPPETS" )
  13602. HandleMysqlCallSnippets ( tOut, *pStmt );
  13603. else if ( pStmt->m_sCallProc=="KEYWORDS" )
  13604. HandleMysqlCallKeywords ( tOut, *pStmt, m_tLastMeta.m_sWarning );
  13605. else if ( pStmt->m_sCallProc=="SUGGEST" )
  13606. HandleMysqlCallSuggest ( tOut, *pStmt, false );
  13607. else if ( pStmt->m_sCallProc=="QSUGGEST" )
  13608. HandleMysqlCallSuggest ( tOut, *pStmt, true );
  13609. else if ( pStmt->m_sCallProc=="PQ" )
  13610. {
  13611. HandleMysqlCallPQ ( tOut, *pStmt, m_tAcc, m_tPercolateMeta );
  13612. m_tPercolateMeta.m_dResult.m_sMessages.MoveWarningsTo ( m_tLastMeta.m_sWarning );
  13613. m_tPercolateMeta.m_dDocids.Reset ( 0 ); // free occupied mem
  13614. } else
  13615. {
  13616. m_sError.SetSprintf ( "no such built-in procedure %s", pStmt->m_sCallProc.cstr() );
  13617. tOut.Error ( sQuery.first, m_sError.cstr() );
  13618. }
  13619. return true;
  13620. case STMT_DESCRIBE:
  13621. HandleMysqlDescribe ( tOut, pStmt );
  13622. return true;
  13623. case STMT_SHOW_TABLES:
  13624. HandleMysqlShowTables ( tOut, pStmt );
  13625. return true;
  13626. case STMT_CREATE_TABLE:
  13627. m_tLastMeta = CSphQueryResultMeta();
  13628. HandleMysqlCreateTable ( tOut, *pStmt, m_tLastMeta.m_sWarning );
  13629. return true;
  13630. case STMT_CREATE_TABLE_LIKE:
  13631. m_tLastMeta = CSphQueryResultMeta();
  13632. HandleMysqlCreateTableLike ( tOut, *pStmt, m_tLastMeta.m_sWarning );
  13633. return true;
  13634. case STMT_DROP_TABLE:
  13635. HandleMysqlDropTable ( tOut, *pStmt );
  13636. return true;
  13637. case STMT_SHOW_CREATE_TABLE:
  13638. HandleMysqlShowCreateTable ( tOut, *pStmt );
  13639. return true;
  13640. case STMT_UPDATE:
  13641. {
  13642. StmtErrorReporter_c tErrorReporter ( tOut, pStmt->m_sStmt );
  13643. sphHandleMysqlUpdate ( tErrorReporter, *pStmt, sQuery );
  13644. return true;
  13645. }
  13646. case STMT_DUMMY:
  13647. tOut.Ok();
  13648. return true;
  13649. case STMT_CREATE_FUNCTION:
  13650. if ( !sphPluginCreate ( pStmt->m_sUdfLib.cstr(), PLUGIN_FUNCTION, pStmt->m_sUdfName.cstr(), pStmt->m_eUdfType, m_sError ) )
  13651. tOut.Error ( sQuery.first, m_sError.cstr() );
  13652. else
  13653. tOut.Ok();
  13654. SphinxqlStateFlush ();
  13655. return true;
  13656. case STMT_DROP_FUNCTION:
  13657. if ( !sphPluginDrop ( PLUGIN_FUNCTION, pStmt->m_sUdfName.cstr(), m_sError ) )
  13658. tOut.Error ( sQuery.first, m_sError.cstr() );
  13659. else
  13660. tOut.Ok();
  13661. SphinxqlStateFlush ();
  13662. return true;
  13663. case STMT_CREATE_PLUGIN:
  13664. case STMT_DROP_PLUGIN:
  13665. {
  13666. // convert plugin type string to enum
  13667. PluginType_e eType = sphPluginGetType ( pStmt->m_sStringParam );
  13668. if ( eType==PLUGIN_TOTAL )
  13669. {
  13670. tOut.Error ( "unknown plugin type '%s'", pStmt->m_sStringParam.cstr() );
  13671. break;
  13672. }
  13673. // action!
  13674. bool bRes;
  13675. if ( eStmt==STMT_CREATE_PLUGIN )
  13676. bRes = sphPluginCreate ( pStmt->m_sUdfLib.cstr(), eType, pStmt->m_sUdfName.cstr(), SPH_ATTR_NONE, m_sError );
  13677. else
  13678. bRes = sphPluginDrop ( eType, pStmt->m_sUdfName.cstr(), m_sError );
  13679. // report
  13680. if ( !bRes )
  13681. tOut.Error ( sQuery.first, m_sError.cstr() );
  13682. else
  13683. tOut.Ok();
  13684. SphinxqlStateFlush ();
  13685. return true;
  13686. }
  13687. case STMT_RELOAD_PLUGINS:
  13688. if ( sphPluginReload ( pStmt->m_sUdfLib.cstr(), m_sError ) )
  13689. tOut.Ok();
  13690. else
  13691. tOut.Error ( sQuery.first, m_sError.cstr() );
  13692. return true;
  13693. case STMT_ATTACH_INDEX:
  13694. m_tLastMeta.m_sWarning = "";
  13695. HandleMysqlAttach ( tOut, *pStmt, m_tLastMeta.m_sWarning );
  13696. return true;
  13697. case STMT_FLUSH_RTINDEX:
  13698. HandleMysqlFlushRtindex ( tOut, *pStmt );
  13699. return true;
  13700. case STMT_FLUSH_RAMCHUNK:
  13701. HandleMysqlFlushRamchunk ( tOut, *pStmt );
  13702. return true;
  13703. case STMT_SHOW_VARIABLES:
  13704. HandleMysqlShowVariables ( tOut, *pStmt );
  13705. return true;
  13706. case STMT_TRUNCATE_RTINDEX:
  13707. HandleMysqlTruncate ( tOut, *pStmt );
  13708. return true;
  13709. case STMT_OPTIMIZE_INDEX:
  13710. HandleMysqlOptimize ( tOut, *pStmt );
  13711. return true;
  13712. case STMT_SELECT_SYSVAR:
  13713. HandleMysqlSelectSysvar ( tOut, *pStmt );
  13714. return true;
  13715. case STMT_SHOW_COLLATION:
  13716. HandleMysqlShowCollations ( tOut );
  13717. return true;
  13718. case STMT_SHOW_CHARACTER_SET:
  13719. HandleMysqlShowCharacterSet ( tOut );
  13720. return true;
  13721. case STMT_SHOW_INDEX_STATUS:
  13722. HandleMysqlShowIndexStatus ( tOut, *pStmt, m_bFederatedUser );
  13723. return true;
  13724. case STMT_SHOW_INDEX_SETTINGS:
  13725. HandleMysqlShowIndexSettings ( tOut, *pStmt );
  13726. return true;
  13727. case STMT_SHOW_PROFILE:
  13728. HandleMysqlShowProfile ( tOut, m_tLastProfile, false );
  13729. return false; // do not profile this call, keep last query profile
  13730. case STMT_ALTER_ADD:
  13731. HandleMysqlAlter ( tOut, *pStmt, Alter_e::AddColumn );
  13732. return true;
  13733. case STMT_ALTER_DROP:
  13734. HandleMysqlAlter ( tOut, *pStmt, Alter_e::DropColumn );
  13735. return true;
  13736. case STMT_ALTER_REBUILD_SI:
  13737. HandleMysqlAlter ( tOut, *pStmt, Alter_e::RebuildSI );
  13738. return true;
  13739. case STMT_SHOW_PLAN:
  13740. HandleMysqlShowPlan ( tOut, m_tLastProfile, false, ::IsDot ( *pStmt ) );
  13741. return false; // do not profile this call, keep last query profile
  13742. case STMT_SELECT_DUAL:
  13743. HandleMysqlSelectDual ( tOut, *pStmt );
  13744. return true;
  13745. case STMT_SHOW_DATABASES:
  13746. HandleMysqlShowDatabases ( tOut, *pStmt );
  13747. return true;
  13748. case STMT_SHOW_PLUGINS:
  13749. HandleMysqlShowPlugins ( tOut, *pStmt );
  13750. return true;
  13751. case STMT_SHOW_THREADS:
  13752. HandleMysqlShowThreads ( tOut, pStmt );
  13753. return true;
  13754. case STMT_ALTER_RECONFIGURE: // ALTER RTINDEX/TABLE <idx> RECONFIGURE
  13755. FreezeLastMeta();
  13756. HandleMysqlReconfigure ( tOut, *pStmt, m_tLastMeta.m_sWarning );
  13757. return true;
  13758. case STMT_ALTER_KLIST_TARGET: // ALTER TABLE <idx> KILLLIST_TARGET = 'the string'
  13759. FreezeLastMeta();
  13760. HandleMysqlAlterKlist ( tOut, *pStmt, m_tLastMeta.m_sWarning );
  13761. return true;
  13762. case STMT_ALTER_INDEX_SETTINGS: // ALTER TABLE <idx> create_table_option_list
  13763. FreezeLastMeta();
  13764. HandleMysqlAlterIndexSettings ( tOut, *pStmt, m_tLastMeta.m_sWarning );
  13765. return true;
  13766. case STMT_FLUSH_INDEX:
  13767. HandleMysqlFlush ( tOut, *pStmt );
  13768. return true;
  13769. case STMT_RELOAD_INDEX:
  13770. FreezeLastMeta();
  13771. HandleMysqlReloadIndex ( tOut, *pStmt, m_tLastMeta.m_sWarning );
  13772. return true;
  13773. case STMT_FLUSH_HOSTNAMES:
  13774. HandleMysqlFlushHostnames ( tOut );
  13775. return true;
  13776. case STMT_FLUSH_LOGS:
  13777. HandleMysqlFlushLogs ( tOut );
  13778. return true;
  13779. case STMT_RELOAD_INDEXES:
  13780. HandleMysqlReloadIndexes ( tOut );
  13781. return true;
  13782. case STMT_DEBUG:
  13783. HandleMysqlDebug ( tOut, sQuery, m_tLastProfile );
  13784. return false; // do not profile this call, keep last query profile
  13785. case STMT_JOIN_CLUSTER:
  13786. if ( ClusterJoin ( pStmt->m_sIndex, pStmt->m_dCallOptNames, pStmt->m_dCallOptValues, pStmt->m_bClusterUpdateNodes, m_sError ) )
  13787. tOut.Ok();
  13788. else
  13789. tOut.Error ( sQuery.first, m_sError.cstr() );
  13790. return true;
  13791. case STMT_CLUSTER_CREATE:
  13792. if ( ClusterCreate ( pStmt->m_sIndex, pStmt->m_dCallOptNames, pStmt->m_dCallOptValues, m_sError ) )
  13793. tOut.Ok();
  13794. else
  13795. tOut.Error ( sQuery.first, m_sError.cstr() );
  13796. return true;
  13797. case STMT_CLUSTER_DELETE:
  13798. m_tLastMeta = CSphQueryResultMeta();
  13799. if ( ClusterDelete ( pStmt->m_sIndex, m_tLastMeta.m_sError, m_tLastMeta.m_sWarning ) )
  13800. tOut.Ok ( 0, m_tLastMeta.m_sWarning.IsEmpty() ? 0 : 1 );
  13801. else
  13802. tOut.Error ( sQuery.first, m_tLastMeta.m_sError.cstr() );
  13803. return true;
  13804. case STMT_CLUSTER_ALTER_ADD:
  13805. case STMT_CLUSTER_ALTER_DROP:
  13806. m_tLastMeta = CSphQueryResultMeta();
  13807. if ( ClusterAlter ( pStmt->m_sCluster, pStmt->m_sIndex, ( eStmt==STMT_CLUSTER_ALTER_ADD ), m_tLastMeta.m_sError, m_tLastMeta.m_sWarning ) )
  13808. tOut.Ok ( 0, m_tLastMeta.m_sWarning.IsEmpty() ? 0 : 1 );
  13809. else
  13810. tOut.Error ( sQuery.first, m_tLastMeta.m_sError.cstr() );
  13811. return true;
  13812. case STMT_CLUSTER_ALTER_UPDATE:
  13813. m_tLastMeta = CSphQueryResultMeta();
  13814. if ( ClusterAlterUpdate ( pStmt->m_sCluster, pStmt->m_sSetName, true, m_tLastMeta.m_sError ) )
  13815. tOut.Ok();
  13816. else
  13817. tOut.Error ( sQuery.first, m_tLastMeta.m_sError.cstr() );
  13818. return true;
  13819. case STMT_EXPLAIN:
  13820. HandleMysqlExplain ( tOut, *pStmt, IsDot ( *pStmt ) );
  13821. return true;
  13822. case STMT_IMPORT_TABLE:
  13823. FreezeLastMeta();
  13824. HandleMysqlImportTable ( tOut, *pStmt, m_tLastMeta.m_sWarning );
  13825. return true;
  13826. case STMT_FREEZE:
  13827. HandleMysqlFreezeIndexes ( tOut, pStmt->m_sIndex, m_tLastMeta.m_sWarning);
  13828. return true;
  13829. case STMT_UNFREEZE:
  13830. HandleMysqlUnfreezeIndexes ( tOut, pStmt->m_sIndex, m_tLastMeta.m_sWarning );
  13831. return true;
  13832. case STMT_SHOW_SETTINGS:
  13833. {
  13834. ScRL_t dRotateConfigMutexRlocked { g_tRotateConfigMutex };
  13835. HandleMysqlShowSettings ( g_hCfg, tOut );
  13836. }
  13837. return true;
  13838. case STMT_KILL:
  13839. HandleMysqlKill ( tOut, pStmt->m_iIntParam );
  13840. return true;
  13841. default:
  13842. m_sError.SetSprintf ( "internal error: unhandled statement type (value=%d)", eStmt );
  13843. tOut.Error ( sQuery.first, m_sError.cstr() );
  13844. return true;
  13845. } // switch
  13846. return true; // for cases that break early
  13847. }
  13848. bool session::IsAutoCommit ( const ClientSession_c* pSession )
  13849. {
  13850. assert ( pSession );
  13851. return pSession->m_bAutoCommit;
  13852. }
  13853. bool session::IsAutoCommit ()
  13854. {
  13855. return IsAutoCommit ( GetClientSession() );
  13856. }
  13857. bool session::IsInTrans ( const ClientSession_c* pSession )
  13858. {
  13859. assert ( pSession );
  13860. return pSession->m_bInTransaction;
  13861. }
  13862. VecTraits_T<int64_t> session::LastIds ()
  13863. {
  13864. return GetClientSession()->m_dLastIds;
  13865. }
  13866. void session::SetOptimizeById ( bool bOptimizeById )
  13867. {
  13868. GetClientSession()->m_bOptimizeById = bOptimizeById;
  13869. }
  13870. bool session::GetOptimizeById()
  13871. {
  13872. return GetClientSession()->m_bOptimizeById;
  13873. }
  13874. bool session::Execute ( Str_t sQuery, RowBuffer_i& tOut )
  13875. {
  13876. return GetClientSession()->Execute ( sQuery, tOut );
  13877. }
  13878. void session::SetFederatedUser ()
  13879. {
  13880. GetClientSession()->m_bFederatedUser = true;
  13881. }
  13882. void session::SetAutoCommit ( bool bAutoCommit )
  13883. {
  13884. GetClientSession()->m_bAutoCommit = bAutoCommit;
  13885. }
  13886. void session::SetInTrans ( bool bInTrans )
  13887. {
  13888. GetClientSession()->m_bInTransaction = bInTrans;
  13889. }
  13890. bool session::IsInTrans ()
  13891. {
  13892. return IsInTrans ( GetClientSession() );
  13893. }
  13894. QueryProfile_c * session::StartProfiling ( ESphQueryState eState )
  13895. {
  13896. auto pSession = GetClientSession();
  13897. QueryProfile_c* pProfile = nullptr;
  13898. if ( session::IsProfile() ) // the current statement might change it
  13899. {
  13900. pProfile = &pSession->m_tProfile;
  13901. pProfile->Start ( eState );
  13902. }
  13903. return pProfile;
  13904. }
  13905. void session::SaveLastProfile ()
  13906. {
  13907. auto pSession = GetClientSession();
  13908. pSession->m_tLastProfile = pSession->m_tProfile;
  13909. }
  13910. /// sphinxql command over API
  13911. void HandleCommandSphinxql ( ISphOutputBuffer & tOut, WORD uVer, InputBuffer_c & tReq ) REQUIRES (HandlerThread)
  13912. {
  13913. if ( !CheckCommandVersion ( uVer, VER_COMMAND_SPHINXQL, tOut ) )
  13914. return;
  13915. auto tReply = APIAnswer ( tOut, VER_COMMAND_SPHINXQL );
  13916. // parse and run request
  13917. CSphVector<BYTE> dString;
  13918. tReq.GetString ( dString );
  13919. RunSingleSphinxqlCommand ( dString, tOut );
  13920. }
  13921. /// json command over API
  13922. void HandleCommandJson ( ISphOutputBuffer & tOut, WORD uVer, InputBuffer_c & tReq )
  13923. {
  13924. if ( !CheckCommandVersion ( uVer, VER_COMMAND_JSON, tOut ) )
  13925. return;
  13926. // parse request
  13927. CSphString sEndpoint = tReq.GetString ();
  13928. CSphString sCommand = tReq.GetString ();
  13929. CSphVector<BYTE> dResult;
  13930. sphProcessHttpQueryNoResponce ( sEndpoint, sCommand, dResult );
  13931. auto tReply = APIAnswer ( tOut, VER_COMMAND_JSON );
  13932. tOut.SendString ( sEndpoint.cstr() );
  13933. tOut.SendArray ( dResult );
  13934. }
  13935. void StatCountCommand ( SearchdCommand_e eCmd )
  13936. {
  13937. if ( eCmd<SEARCHD_COMMAND_TOTAL )
  13938. gStats ().m_iCommandCount[eCmd].fetch_add ( 1, std::memory_order_relaxed );
  13939. }
  13940. bool FixupFederatedQuery ( ESphCollation eCollation, CSphVector<SqlStmt_t> & dStmt, CSphString & sError, CSphString & sFederatedQuery )
  13941. {
  13942. if ( !dStmt.GetLength() )
  13943. return true;
  13944. if ( dStmt.GetLength()>1 )
  13945. {
  13946. sError.SetSprintf ( "multi-query not supported" );
  13947. return false;
  13948. }
  13949. SqlStmt_t & tStmt = dStmt[0];
  13950. if ( tStmt.m_eStmt==STMT_SHOW_INDEX_STATUS )
  13951. return true;
  13952. else if ( tStmt.m_eStmt == STMT_SET )
  13953. return true;
  13954. else if ( tStmt.m_eStmt != STMT_SELECT)
  13955. {
  13956. sError.SetSprintf ( "unhandled statement type (value=%d)", tStmt.m_eStmt );
  13957. return false;
  13958. }
  13959. CSphQuery & tSrcQuery = tStmt.m_tQuery;
  13960. // remove query column as it got generated
  13961. ARRAY_FOREACH ( i, tSrcQuery.m_dItems )
  13962. {
  13963. if ( tSrcQuery.m_dItems[i].m_sAlias=="query" )
  13964. {
  13965. tSrcQuery.m_dItems.Remove ( i );
  13966. break;
  13967. }
  13968. }
  13969. // move actual query from filter to query itself
  13970. if ( tSrcQuery.m_dFilters.GetLength()!=1 ||
  13971. tSrcQuery.m_dFilters[0].m_sAttrName!="query" || tSrcQuery.m_dFilters[0].m_eType!=SPH_FILTER_STRING || tSrcQuery.m_dFilters[0].m_dStrings.GetLength()!=1 )
  13972. return true;
  13973. const CSphString & sRealQuery = tSrcQuery.m_dFilters[0].m_dStrings[0];
  13974. // parse real query
  13975. CSphVector<SqlStmt_t> dRealStmt;
  13976. bool bParsedOK = sphParseSqlQuery ( sRealQuery.cstr(), sRealQuery.Length(), dRealStmt, sError, eCollation );
  13977. if ( !bParsedOK )
  13978. return false;
  13979. if ( dRealStmt.GetLength()!=1 )
  13980. {
  13981. sError.SetSprintf ( "multi-query not supported, got queries=%d", dRealStmt.GetLength() );
  13982. return false;
  13983. }
  13984. SqlStmt_t & tRealStmt = dRealStmt[0];
  13985. if ( tRealStmt.m_eStmt!=STMT_SELECT )
  13986. {
  13987. sError.SetSprintf ( "unhandled statement type (value=%d)", tRealStmt.m_eStmt );
  13988. return false;
  13989. }
  13990. // keep originals
  13991. CSphQuery & tRealQuery = tRealStmt.m_tQuery;
  13992. tRealQuery.m_dRefItems = tSrcQuery.m_dItems; //select list items
  13993. tRealQuery.m_sIndexes = tSrcQuery.m_sIndexes; // index name
  13994. sFederatedQuery = sRealQuery;
  13995. // merge select list items
  13996. SmallStringHash_T<int> hItems;
  13997. ARRAY_FOREACH ( i, tRealQuery.m_dItems )
  13998. hItems.Add ( i, tRealQuery.m_dItems[i].m_sAlias );
  13999. ARRAY_FOREACH ( i, tSrcQuery.m_dItems )
  14000. {
  14001. const CSphQueryItem & tItem = tSrcQuery.m_dItems[i];
  14002. if ( !hItems.Exists ( tItem.m_sAlias ) )
  14003. tRealQuery.m_dItems.Add ( tItem );
  14004. }
  14005. // query setup
  14006. tSrcQuery = tRealQuery;
  14007. return true;
  14008. }
  14009. /////////////////////////////////////////////////////////////////////////////
  14010. // INDEX ROTATION
  14011. /////////////////////////////////////////////////////////////////////////////
  14012. static bool ApplyIndexKillList ( const CSphIndex * pIndex, CSphString & sWarning, CSphString & sError, bool bShowMessage )
  14013. {
  14014. CSphFixedVector<DocID_t> dKillList(0);
  14015. KillListTargets_c tTargets;
  14016. if ( !pIndex->LoadKillList ( &dKillList, tTargets, sError ) )
  14017. return false;
  14018. if ( !tTargets.m_dTargets.GetLength() )
  14019. return true;
  14020. if ( bShowMessage )
  14021. sphInfo ( "applying killlist of index '%s'", pIndex->GetName() );
  14022. for ( const auto & tIndex : tTargets.m_dTargets )
  14023. {
  14024. // just in case; otherwise we'll be rlocking an already rlocked index
  14025. if ( tIndex.m_sIndex==pIndex->GetName() )
  14026. {
  14027. sWarning.SetSprintf ( "index '%s': applying killlist to itself", tIndex.m_sIndex.cstr() );
  14028. continue;
  14029. }
  14030. auto pServed = GetServed ( tIndex.m_sIndex );
  14031. if ( pServed )
  14032. {
  14033. RWIdx_c pTarget { pServed };
  14034. // kill the docids provided by sql_query_killlist and similar
  14035. if ( tIndex.m_uFlags & KillListTarget_t::USE_KLIST )
  14036. pTarget->KillMulti ( dKillList );
  14037. // kill all the docids present in this index
  14038. if ( tIndex.m_uFlags & KillListTarget_t::USE_DOCIDS )
  14039. pIndex->KillExistingDocids ( pTarget );
  14040. }
  14041. else
  14042. sWarning.SetSprintf ( "index '%s' from killlist_target not found", tIndex.m_sIndex.cstr() );
  14043. }
  14044. return true;
  14045. }
  14046. // we don't rlock/wlock the index because we assume that we are being called from a place that already did that for us
  14047. bool ApplyKillListsTo ( CSphIndex* pKillListTarget, CSphString & sError )
  14048. {
  14049. KillListTargets_c tTargets;
  14050. ServedSnap_t hLocal = g_pLocalIndexes->GetHash();
  14051. for ( const auto& tIt : *hLocal )
  14052. {
  14053. if ( tIt.first==pKillListTarget->GetName () || !tIt.second )
  14054. continue;
  14055. RWIdx_c pIndexWithKillList { tIt.second };
  14056. CSphFixedVector<DocID_t> dKillList(0);
  14057. tTargets.m_dTargets.Resize(0);
  14058. if ( !pIndexWithKillList->LoadKillList ( &dKillList, tTargets, sError ) )
  14059. return false;
  14060. if ( !dKillList.GetLength() )
  14061. continue;
  14062. // if this index has 'our' index as its killlist_target, apply the killlist
  14063. for ( const auto & tIndex : tTargets.m_dTargets )
  14064. if ( tIndex.m_sIndex== pKillListTarget->GetName() )
  14065. {
  14066. if ( tIndex.m_uFlags & KillListTarget_t::USE_KLIST )
  14067. pKillListTarget->KillMulti ( dKillList );
  14068. // kill all the docids present in this index
  14069. if ( tIndex.m_uFlags & KillListTarget_t::USE_DOCIDS )
  14070. pKillListTarget->KillExistingDocids ( pIndexWithKillList );
  14071. }
  14072. }
  14073. return true;
  14074. }
  14075. bool PreloadKlistTarget ( const ServedDesc_t & tServed, RotateFrom_e eFrom, StrVec_t & dKlistTarget )
  14076. {
  14077. switch ( eFrom )
  14078. {
  14079. case RotateFrom_e::NEW:
  14080. return IndexFiles_c ( tServed.m_sIndexPath ).ReadKlistTargets ( dKlistTarget, ".new" );
  14081. case RotateFrom_e::REENABLE:
  14082. return IndexFiles_c ( tServed.m_sIndexPath ).ReadKlistTargets ( dKlistTarget );
  14083. default:
  14084. return false;
  14085. }
  14086. }
  14087. static bool ApplyOthersKillListsToMe ( CSphIndex* pIndex, const char* szIndex, CSphString& sError )
  14088. {
  14089. sphLogDebug ( "rotating index '%s': applying other indexes' killlists", szIndex );
  14090. // apply other indexes' killlists to THIS index
  14091. if ( !ApplyKillListsTo ( pIndex, sError ) )
  14092. {
  14093. sphWarning ( "rotating index '%s': %s", szIndex, sError.cstr() );
  14094. return false;
  14095. }
  14096. sphLogDebug ( "rotating index '%s': applying other indexes' killlists... DONE", szIndex );
  14097. return true;
  14098. }
  14099. static bool ApplyMyKillListsToOthers ( const CSphIndex* pIndex, const char* szIndex, CSphString& sError )
  14100. {
  14101. sphLogDebug ( "rotating index '%s': apply killlist from this index to other indexes (killlist_target)", szIndex );
  14102. // apply killlist from this index to other indexes (killlist_target)
  14103. // if this fails, only show a warning
  14104. CSphString sWarning;
  14105. if ( !ApplyIndexKillList ( pIndex, sWarning, sError ) )
  14106. {
  14107. return false;
  14108. sphWarning ( "rotating index '%s': %s", szIndex, sError.cstr() );
  14109. }
  14110. if ( sWarning.Length() )
  14111. sphWarning ( "rotating index '%s': %s", szIndex, sWarning.cstr() );
  14112. sphLogDebug ( "rotating index '%s': apply killlist from this index to other indexes (killlist_target)... DONE", szIndex );
  14113. return true;
  14114. }
  14115. bool ApplyKilllistsMyAndToMe ( CSphIndex* pIdx, const char* szIndex, CSphString& sError )
  14116. {
  14117. return ApplyOthersKillListsToMe ( pIdx, szIndex, sError ) && ApplyMyKillListsToOthers ( pIdx, szIndex, sError );
  14118. }
  14119. // tServed here might be one of:
  14120. // 1. Not yet served, and with .new ext. Need to be rotated, then loaded from scratch
  14121. // 2. Not yet served, need to be loaded from scratch
  14122. // 3. Served, but with now with .new. Need to be rotated, then loaded and and need to be rotated
  14123. bool RotateIndexGreedy ( const ServedIndex_c& tServed, const char* szIndex, CSphString& sError )
  14124. {
  14125. assert ( tServed.m_eType == IndexType_e::PLAIN );
  14126. sphLogDebug ( "RotateIndexGreedy for '%s' invoked", szIndex );
  14127. //////////////////
  14128. /// bool RotateIndexFilesGreedy ( const ServedDesc_t& tServed, const char* szIndex, CSphString& sError )
  14129. //////////////////
  14130. CheckIndexRotate_c tCheck ( tServed );
  14131. if ( tCheck.NothingToRotate() )
  14132. return false;
  14133. IndexFiles_c dServedFiles ( tServed.m_sIndexPath, szIndex );
  14134. IndexFiles_c dFreshFiles ( dServedFiles.MakePath ( tCheck.RotateFromNew() ? ".new" : "" ), szIndex );
  14135. if ( !dFreshFiles.CheckHeader() )
  14136. {
  14137. // no files or wrong files - no rotation
  14138. sError = dFreshFiles.ErrorMsg();
  14139. return false;
  14140. }
  14141. if ( !dFreshFiles.HasAllFiles() )
  14142. {
  14143. sphWarning ( "rotating index '%s': unreadable: %s; abort rotation", szIndex, strerrorm ( errno ) );
  14144. return false;
  14145. }
  14146. bool bHasOldServedFiles = dServedFiles.HasAllFiles();
  14147. Optional_T<ActionSequence_c> tActions;
  14148. if ( tCheck.RotateFromNew() )
  14149. {
  14150. tActions.emplace();
  14151. if ( bHasOldServedFiles )
  14152. tActions->Defer ( RenameFiles ( dServedFiles, "", ".old") );
  14153. tActions->Defer ( RenameFiles ( dServedFiles, ".new", "" ) );
  14154. // do files rotation
  14155. if ( !tActions->RunDefers() )
  14156. {
  14157. bool bFatal;
  14158. std::tie ( sError, bFatal ) = tActions->GetError();
  14159. sphWarning ( "RotateIndexGreedy error: %s", sError.cstr() );
  14160. if ( bFatal )
  14161. sphFatal ( "RotateIndexGreedy error: %s", sError.cstr() ); // fixme! Do we really need to fatal? (adopted from prev version)
  14162. return false;
  14163. }
  14164. }
  14165. // try to use new index
  14166. auto pIdx = UnlockedHazardIdxFromServed ( tServed ); // it should be locked, if necessary, before
  14167. StrVec_t dWarnings;
  14168. if ( !pIdx->Prealloc ( g_bStripPath, nullptr, dWarnings ) )
  14169. {
  14170. sphWarning ( "rotating index '%s': .new preload failed: %s", szIndex, pIdx->GetLastError().cstr() );
  14171. if ( tActions )
  14172. {
  14173. if ( !tActions->UnRunDefers() )
  14174. {
  14175. bool bFatal;
  14176. std::tie ( sError, bFatal ) = tActions->GetError();
  14177. sphWarning ( "RotateIndexGreedy error: %s, NOT SERVING", sError.cstr() );
  14178. if ( bFatal )
  14179. sphFatal ( "RotateIndexGreedy error: %s", sError.cstr() ); // fixme! Do we really need to fatal? (adopted from prev version)
  14180. return false;
  14181. }
  14182. sphLogDebug ( "PreallocIndexGreedy: has recovered. Prealloc it." );
  14183. if ( !pIdx->Prealloc ( g_bStripPath, nullptr, dWarnings ) )
  14184. {
  14185. sError.SetSprintf ( "rotating index '%s': .new preload failed; ROLLBACK FAILED; INDEX UNUSABLE", szIndex );
  14186. return false;
  14187. }
  14188. }
  14189. }
  14190. assert ( pIdx->GetTokenizer() && pIdx->GetDictionary() );
  14191. for ( const auto& i : dWarnings )
  14192. sphWarning ( "rotating index '%s': %s", szIndex, i.cstr() );
  14193. if ( !pIdx->GetLastWarning().IsEmpty() )
  14194. sphWarning ( "rotating index '%s': %s", szIndex, pIdx->GetLastWarning().cstr() );
  14195. // unlink .old
  14196. if ( bHasOldServedFiles )
  14197. dServedFiles.Unlink (".old");
  14198. // finalize
  14199. if ( !ApplyKilllistsMyAndToMe ( pIdx, szIndex, sError ) )
  14200. return false;
  14201. // uff. all done
  14202. sphInfo ( "rotating index '%s': success", szIndex );
  14203. return true;
  14204. }
  14205. void DumpMemStat ()
  14206. {
  14207. #if SPH_ALLOCS_PROFILER
  14208. sphMemStatDump ( g_iLogFile );
  14209. #endif
  14210. }
  14211. /// check and report if there were any leaks since last call
  14212. void CheckLeaks () REQUIRES ( MainThread )
  14213. {
  14214. #if SPH_DEBUG_LEAKS
  14215. static int iHeadAllocs = sphAllocsCount ();
  14216. static int iHeadCheckpoint = sphAllocsLastID ();
  14217. if ( g_dThd.GetLength()==0 && !g_bInRotate && iHeadAllocs!=sphAllocsCount() )
  14218. {
  14219. sphSeek ( g_iLogFile, 0, SEEK_END );
  14220. sphAllocsDump ( g_iLogFile, iHeadCheckpoint );
  14221. iHeadAllocs = sphAllocsCount ();
  14222. iHeadCheckpoint = sphAllocsLastID ();
  14223. }
  14224. #endif
  14225. #if SPH_ALLOCS_PROFILER
  14226. int iAllocLogPeriod = 60 * 1000000;
  14227. static int64_t tmLastLog = -iAllocLogPeriod*10;
  14228. const int iAllocCount = sphAllocsCount();
  14229. const float fMemTotal = (float)sphAllocBytes();
  14230. if ( iAllocLogPeriod>0 && tmLastLog+iAllocLogPeriod<sphMicroTimer() )
  14231. {
  14232. tmLastLog = sphMicroTimer ();
  14233. const int iThdsCount = g_dThd.GetLength ();
  14234. const float fMB = 1024.0f*1024.0f;
  14235. sphInfo ( "--- allocs-count=%d, mem-total=%.4f Mb, active-threads=%d", iAllocCount, fMemTotal/fMB, iThdsCount );
  14236. DumpMemStat ();
  14237. }
  14238. #endif
  14239. }
  14240. bool LockIndex ( ServedIndex_c& tIdx, CSphIndex* pIdx, CSphString& sError )
  14241. {
  14242. if ( !g_bOptNoLock && !pIdx->Lock() )
  14243. {
  14244. sError.SetSprintf ( "lock: %s", pIdx->GetLastError().cstr() );
  14245. return false;
  14246. }
  14247. tIdx.UpdateMass();
  14248. return true;
  14249. }
  14250. // tricky bit
  14251. // fixup was initially intended for (very old) index formats that did not store dict/tokenizer settings
  14252. // however currently it also ends up configuring dict/tokenizer for fresh RT indexes!
  14253. // (and for existing RT indexes, settings get loaded during the Prealloc() call)
  14254. bool FixupAndLockIndex ( ServedIndex_c& tIdx, CSphIndex* pIdx, const CSphConfigSection* pConfig, const char* szIndexName, StrVec_t& dWarnings, CSphString& sError )
  14255. {
  14256. if ( pConfig )
  14257. {
  14258. std::unique_ptr<FilenameBuilder_i> pFilenameBuilder = CreateFilenameBuilder ( szIndexName );
  14259. if ( !sphFixupIndexSettings ( pIdx, *pConfig, g_bStripPath, pFilenameBuilder.get(), dWarnings, sError ) )
  14260. return false;
  14261. }
  14262. // try to lock it
  14263. return LockIndex ( tIdx, pIdx, sError );
  14264. }
  14265. /// this gets called for every new physical index
  14266. /// that is, local and RT indexes, but not distributed one
  14267. bool PreallocNewIndex ( ServedIndex_c & tIdx, const CSphConfigSection * pConfig, const char * szIndexName, StrVec_t & dWarnings, CSphString & sError )
  14268. {
  14269. std::unique_ptr<FilenameBuilder_i> pFilenameBuilder = CreateFilenameBuilder ( szIndexName );
  14270. CSphIndex* pIdx = UnlockedHazardIdxFromServed ( tIdx );
  14271. assert (pIdx);
  14272. if ( !pIdx->Prealloc ( g_bStripPath, pFilenameBuilder.get(), dWarnings ) )
  14273. {
  14274. sError.SetSprintf ( "prealloc: %s", pIdx->GetLastError().cstr() );
  14275. return false;
  14276. }
  14277. return FixupAndLockIndex ( tIdx, pIdx, pConfig, szIndexName, dWarnings, sError );
  14278. }
  14279. // same as above, but self-load config section for given index
  14280. static bool PreallocNewIndex ( ServedIndex_c & tIdx, const char * szIndexName, StrVec_t & dWarnings, CSphString & sError )
  14281. {
  14282. const CSphConfigSection * pIndexConfig = nullptr;
  14283. CSphConfigSection tIndexConfig;
  14284. {
  14285. ScRL_t dRLockConfig { g_tRotateConfigMutex };
  14286. if ( g_hCfg ( "index" ) )
  14287. pIndexConfig = g_hCfg["index"] ( szIndexName );
  14288. if ( pIndexConfig )
  14289. {
  14290. tIndexConfig = *pIndexConfig;
  14291. pIndexConfig = &tIndexConfig;
  14292. }
  14293. }
  14294. return PreallocNewIndex ( tIdx, pIndexConfig, szIndexName, dWarnings, sError );
  14295. }
  14296. // called either from MysqlReloadIndex, either from Rotation task (never from main thread).
  14297. bool RotateIndexMT ( ServedIndexRefPtr_c& pNewServed, const CSphString & sIndex, StrVec_t & dWarnings, CSphString & sError ) EXCLUDES ( MainThread )
  14298. {
  14299. assert ( pNewServed && pNewServed->m_eType == IndexType_e::PLAIN );
  14300. sphInfo ( "rotating index '%s': started", sIndex.cstr() );
  14301. CheckIndexRotate_c tCheck ( *pNewServed );
  14302. if ( tCheck.NothingToRotate() )
  14303. {
  14304. sError.SetSprintf ( "nothing to rotate for index '%s'", sIndex.cstr() );
  14305. return false;
  14306. }
  14307. //////////////////
  14308. /// load new index
  14309. //////////////////
  14310. CSphIndex* pNewIndex = UnlockedHazardIdxFromServed ( *pNewServed );
  14311. if ( tCheck.RotateFromNew() )
  14312. pNewIndex->SetBase ( IndexFiles_c::MakePath ( ".new", pNewServed->m_sIndexPath ) );
  14313. // prealloc enough RAM and lock new index
  14314. sphLogDebug ( "prealloc enough RAM and lock new index" );
  14315. if ( !PreallocNewIndex ( *pNewServed, sIndex.cstr(), dWarnings, sError ) )
  14316. return false;
  14317. pNewIndex->Preread();
  14318. pNewServed->UpdateMass(); // that is second update, first was at the end of Prealloc, this one is to correct after preread
  14319. //////////////////////
  14320. /// activate new index
  14321. //////////////////////
  14322. sphLogDebug ( "activate new index" );
  14323. if ( tCheck.RotateFromNew() )
  14324. {
  14325. ActionSequence_c tActions;
  14326. auto pServed = GetServed ( sIndex );
  14327. if ( pServed && pServed->m_sIndexPath == pNewServed->m_sIndexPath )
  14328. tActions.Defer ( RenameIdxSuffix ( pServed, ".old" ) );
  14329. tActions.Defer ( RenameIdx ( pNewIndex, pNewServed->m_sIndexPath ) ); // rename 'new' to 'current'
  14330. if ( !tActions.RunDefers() )
  14331. {
  14332. bool bFatal;
  14333. std::tie ( sError, bFatal ) = tActions.GetError();
  14334. sphWarning ( "RotateIndexMT error: index %s, error %s", sIndex.cstr(), sError.cstr() );
  14335. if ( bFatal )
  14336. g_pLocalIndexes->Delete ( sIndex );
  14337. return false;
  14338. }
  14339. if ( pServed )
  14340. {
  14341. RIdx_c pOldIdx { pServed };
  14342. pNewIndex->m_iTID = pOldIdx->m_iTID;
  14343. pServed->SetUnlink ( pOldIdx->GetFilename() );
  14344. }
  14345. }
  14346. if ( !ApplyKilllistsMyAndToMe ( pNewIndex, sIndex.cstr(), sError ) )
  14347. return false;
  14348. // all went fine; swap them
  14349. sphLogDebug ( "all went fine; swap them" );
  14350. Binlog::NotifyIndexFlush ( sIndex.cstr(), pNewIndex->m_iTID, false );
  14351. g_pLocalIndexes->AddOrReplace ( pNewServed, sIndex );
  14352. sphInfo ( "rotating index '%s': success", sIndex.cstr() );
  14353. return true;
  14354. }
  14355. static void InvokeRotation ( VecOfServed_c&& dDeferredIndexes ) REQUIRES ( MainThread )
  14356. {
  14357. assert ( !dDeferredIndexes.IsEmpty () && "Rotation queue must be checked before invoking rotation!");
  14358. Threads::StartJob ( [dIndexes = std::move ( dDeferredIndexes )] () mutable
  14359. {
  14360. // want to track rotation thread only at work
  14361. auto pDesc = PublishSystemInfo ( "ROTATION" );
  14362. sphLogDebug ( "TaskRotation starts with %d deferred indexes", dIndexes.GetLength() );
  14363. for ( auto& tIndex : dIndexes )
  14364. {
  14365. ServedIndexRefPtr_c& pReplacementServed = tIndex.second;
  14366. const CSphString& sIndex = tIndex.first;
  14367. // cluster indexes got managed by different path
  14368. assert ( !ServedDesc_t::IsCluster ( pReplacementServed ) && "Rotation of clusters MUST never happens!" );
  14369. // prealloc RT and percolate here
  14370. StrVec_t dWarnings;
  14371. CSphString sError;
  14372. if ( ServedDesc_t::IsMutable ( pReplacementServed ) )
  14373. {
  14374. sphLogDebug ( "seamless rotate (prealloc) mutable index %s", sIndex.cstr() );
  14375. if ( PreallocNewIndex ( *pReplacementServed, sIndex.cstr(), dWarnings, sError ) )
  14376. g_pLocalIndexes->AddOrReplace ( pReplacementServed, sIndex );
  14377. else
  14378. sphWarning ( "index '%s': %s", sIndex.cstr(), sError.cstr() );
  14379. } else
  14380. {
  14381. sphLogDebug ( "seamless rotate local index %s", sIndex.cstr() );
  14382. if ( !RotateIndexMT ( pReplacementServed, sIndex, dWarnings, sError ) )
  14383. sphWarning ( "index '%s': %s", sIndex.cstr(), sError.cstr() );
  14384. }
  14385. for ( const auto& i : dWarnings )
  14386. sphWarning ( "index '%s': %s", sIndex.cstr(), i.cstr() );
  14387. g_pDistIndexes->Delete ( sIndex ); // postponed delete of same-named distributed (if any)
  14388. }
  14389. g_bInRotate = false;
  14390. RotateGlobalIdf();
  14391. sphInfo ( "rotating index: all indexes done" );
  14392. });
  14393. }
  14394. bool LimitedRotateIndexMT ( ServedIndexRefPtr_c& pNewServed, const CSphString& sIndex, StrVec_t& dWarnings, CSphString& sError ) EXCLUDES ( MainThread )
  14395. {
  14396. assert ( Threads::IsInsideCoroutine() );
  14397. // allow to run several rotations a time (in parallel)
  14398. // vip conns has no limit
  14399. if ( session::GetVip() )
  14400. return RotateIndexMT ( pNewServed, sIndex, dWarnings, sError );
  14401. // limit is arbitrary set to N/2 of threadpool
  14402. static Coro::Waitable_T<int> iParallelRotations { 0 };
  14403. iParallelRotations.Wait ( [] ( int i ) { return i < Max ( 1, NThreads() / 2 ); } );
  14404. iParallelRotations.ModifyValue ( [] ( int& i ) { ++i; } );
  14405. auto _ = AtScopeExit ( [] {
  14406. iParallelRotations.ModifyValueAndNotifyOne ( [] ( int& i ) { --i; } );
  14407. });
  14408. return RotateIndexMT ( pNewServed, sIndex, dWarnings, sError );
  14409. }
  14410. void ConfigureLocalIndex ( ServedDesc_t * pIdx, const CSphConfigSection & hIndex, bool bMutableOpt, StrVec_t * pWarnings )
  14411. {
  14412. pIdx->m_tSettings.Load ( hIndex, bMutableOpt, pWarnings );
  14413. pIdx->m_sGlobalIDFPath = hIndex.GetStr ( "global_idf" );
  14414. }
  14415. void ConfigureDistributedIndex ( std::function<bool(const CSphString&)>&& fnCheck, DistributedIndex_t & tIdx, const char * szIndexName, const CSphConfigSection & hIndex, StrVec_t * pWarnings )
  14416. {
  14417. assert ( hIndex("type") && hIndex["type"]=="distributed" );
  14418. bool bSetHA = false;
  14419. // configure ha_strategy
  14420. if ( hIndex("ha_strategy") )
  14421. {
  14422. bSetHA = ParseStrategyHA ( hIndex["ha_strategy"].cstr(), tIdx.m_eHaStrategy );
  14423. if ( !bSetHA )
  14424. sphWarning ( "index '%s': ha_strategy (%s) is unknown for me, will use random", szIndexName, hIndex["ha_strategy"].cstr() );
  14425. }
  14426. bool bEnablePersistentConns = ( g_iPersistentPoolSize>0 );
  14427. if ( hIndex ( "agent_persistent" ) && !bEnablePersistentConns )
  14428. {
  14429. sphWarning ( "index '%s': agent_persistent used, but no persistent_connections_limit defined. Fall back to non-persistent agent", szIndexName );
  14430. bEnablePersistentConns = false;
  14431. }
  14432. // add local agents
  14433. StrVec_t dLocs;
  14434. for ( CSphVariant * pLocal = hIndex("local"); pLocal; pLocal = pLocal->m_pNext )
  14435. {
  14436. dLocs.Resize(0);
  14437. sphSplit ( dLocs, pLocal->cstr(), " \t," );
  14438. for ( const auto & sLocal: dLocs )
  14439. {
  14440. if ( !fnCheck ( sLocal ) )
  14441. {
  14442. sphWarning ( "index '%s': no such local index '%s', SKIPPED", szIndexName, sLocal.cstr() );
  14443. continue;
  14444. }
  14445. tIdx.m_dLocal.Add ( sLocal );
  14446. }
  14447. }
  14448. // index-level agent_retry_count
  14449. if ( hIndex ( "agent_retry_count" ) )
  14450. {
  14451. if ( hIndex["agent_retry_count"].intval ()<=0 )
  14452. sphWarning ( "index '%s': agent_retry_count must be positive, ignored", szIndexName );
  14453. else
  14454. tIdx.m_iAgentRetryCount = hIndex["agent_retry_count"].intval ();
  14455. }
  14456. if ( hIndex ( "mirror_retry_count" ) )
  14457. {
  14458. if ( hIndex["mirror_retry_count"].intval ()<=0 )
  14459. sphWarning ( "index '%s': mirror_retry_count must be positive, ignored", szIndexName );
  14460. else
  14461. {
  14462. if ( tIdx.m_iAgentRetryCount>0 )
  14463. sphWarning ("index '%s': `agent_retry_count` and `mirror_retry_count` both specified (they are aliases)."
  14464. "Value of `mirror_retry_count` will be used", szIndexName );
  14465. tIdx.m_iAgentRetryCount = hIndex["mirror_retry_count"].intval ();
  14466. }
  14467. }
  14468. if ( !tIdx.m_iAgentRetryCount )
  14469. tIdx.m_iAgentRetryCount = g_iAgentRetryCount;
  14470. // add remote agents
  14471. struct { const char* sSect; bool bBlh; bool bPrs; } dAgentVariants[] =
  14472. {
  14473. { "agent", false, false},
  14474. { "agent_persistent", false, bEnablePersistentConns },
  14475. { "agent_blackhole", true, false }
  14476. };
  14477. for ( auto & tAg : dAgentVariants )
  14478. {
  14479. for ( CSphVariant * pAgentCnf = hIndex ( tAg.sSect ); pAgentCnf; pAgentCnf = pAgentCnf->m_pNext )
  14480. {
  14481. AgentOptions_t tAgentOptions { tAg.bBlh, tAg.bPrs, tIdx.m_eHaStrategy, tIdx.m_iAgentRetryCount, 0 };
  14482. auto pAgent = ConfigureMultiAgent ( pAgentCnf->cstr(), szIndexName, tAgentOptions, pWarnings );
  14483. if ( pAgent )
  14484. tIdx.m_dAgents.Add ( pAgent );
  14485. }
  14486. }
  14487. // configure options
  14488. if ( hIndex("agent_connect_timeout") )
  14489. {
  14490. if ( hIndex["agent_connect_timeout"].intval()<=0 )
  14491. sphWarning ( "index '%s': agent_connect_timeout must be positive, ignored", szIndexName );
  14492. else
  14493. tIdx.m_iAgentConnectTimeoutMs = hIndex.GetMsTimeMs ( "agent_connect_timeout" );
  14494. }
  14495. tIdx.m_bDivideRemoteRanges = hIndex.GetInt ( "divide_remote_ranges", 0 )!=0;
  14496. if ( hIndex("agent_query_timeout") )
  14497. {
  14498. if ( hIndex["agent_query_timeout"].intval()<=0 )
  14499. sphWarning ( "index '%s': agent_query_timeout must be positive, ignored", szIndexName );
  14500. else
  14501. tIdx.m_iAgentQueryTimeoutMs = hIndex.GetMsTimeMs ( "agent_query_timeout");
  14502. }
  14503. bool bHaveHA = tIdx.m_dAgents.any_of ( [] ( const auto& ag ) { return ag->IsHA (); } );
  14504. // configure ha_strategy
  14505. if ( bSetHA && !bHaveHA )
  14506. sphWarning ( "index '%s': ha_strategy defined, but no ha agents in the index", szIndexName );
  14507. }
  14508. //////////////////////////////////////////////////
  14509. /// configure distributed index and add it to hash
  14510. //////////////////////////////////////////////////
  14511. // AddIndex -> AddDistributedIndex
  14512. static ResultAndIndex_t AddDistributedIndex ( const char * szIndexName, const CSphConfigSection & hIndex, CSphString & sError, StrVec_t * pWarnings=nullptr )
  14513. {
  14514. DistributedIndexRefPtr_t pIdx ( new DistributedIndex_t );
  14515. ConfigureDistributedIndex ( [] ( const auto& sIdx ) { return g_pLocalIndexes->Contains ( sIdx ); }, *pIdx, szIndexName, hIndex, pWarnings );
  14516. if ( pIdx->IsEmpty () )
  14517. {
  14518. sError.SetSprintf ( "index '%s': no valid local/remote indexes in distributed index", szIndexName );
  14519. return { ADD_ERROR, nullptr };
  14520. }
  14521. // finally, check and add distributed index to global table
  14522. if ( !g_pDistIndexes->Add ( pIdx, szIndexName ) )
  14523. {
  14524. sError.SetSprintf ( "index '%s': unable to add name (duplicate?)", szIndexName );
  14525. return { ADD_ERROR, nullptr };
  14526. }
  14527. return ResultAndIndex_t { ADD_DISTR, nullptr };
  14528. }
  14529. // common preconfiguration of mutable indexes
  14530. static bool ConfigureRTPercolate ( CSphSchema & tSchema, CSphIndexSettings & tSettings, const char * szIndexName, const CSphConfigSection & hIndex, bool bWordDict, bool bPercolate, StrVec_t * pWarnings, CSphString & sError )
  14531. {
  14532. // pick config settings
  14533. // they should be overriden later by Preload() if needed
  14534. {
  14535. CSphString sWarning;
  14536. if ( !tSettings.Setup ( hIndex, szIndexName, sWarning, sError ) )
  14537. {
  14538. sphWarning ( "index '%s': %s - NOT SERVING", szIndexName, sError.cstr() );
  14539. return false;
  14540. }
  14541. if ( !sWarning.IsEmpty() )
  14542. sphWarning ( "index '%s': %s", szIndexName, sWarning.cstr() );
  14543. }
  14544. if ( !sphRTSchemaConfigure ( hIndex, tSchema, tSettings, sError, bPercolate, bPercolate ) )
  14545. {
  14546. sphWarning ( "index '%s': %s - NOT SERVING", szIndexName, sError.cstr () );
  14547. return false;
  14548. }
  14549. if ( bPercolate )
  14550. FixPercolateSchema ( tSchema );
  14551. if ( !sError.IsEmpty() )
  14552. {
  14553. if ( pWarnings )
  14554. pWarnings->Add(sError);
  14555. else
  14556. sphWarning ( "index '%s': %s", szIndexName, sError.cstr () );
  14557. }
  14558. // path
  14559. if ( !hIndex ( "path" ) )
  14560. {
  14561. sphWarning ( "index '%s': path must be specified - NOT SERVING", szIndexName );
  14562. return false;
  14563. }
  14564. if ( !CheckStoredFields ( tSchema, tSettings, sError ) )
  14565. {
  14566. sphWarning ( "index '%s': %s - NOT SERVING", szIndexName, sError.cstr() );
  14567. return false;
  14568. }
  14569. int iIndexSP = hIndex.GetInt ( "index_sp" );
  14570. auto sIndexZones = hIndex.GetStr ( "index_zones" );
  14571. bool bHasStripEnabled ( hIndex.GetInt ( "html_strip" )!=0 );
  14572. if ( ( iIndexSP!=0 || !sIndexZones.IsEmpty() ) && !bHasStripEnabled )
  14573. {
  14574. // SENTENCE indexing w\o stripper is valid combination
  14575. if ( !sIndexZones.IsEmpty() )
  14576. {
  14577. sphWarning ( "index '%s': has index_sp=%d, index_zones='%s' but disabled html_strip - NOT SERVING", szIndexName, iIndexSP, sIndexZones.cstr() );
  14578. return false;
  14579. }
  14580. else
  14581. {
  14582. CSphString sWarning;
  14583. sWarning.SetSprintf ( "has index_sp=%d but disabled html_strip - PARAGRAPH unavailable", iIndexSP );
  14584. if ( pWarnings )
  14585. pWarnings->Add(sWarning);
  14586. else
  14587. sphWarning ( "index '%s': %s", szIndexName, sWarning.cstr() );
  14588. }
  14589. }
  14590. // upgrading schema to store field lengths
  14591. if ( tSettings.m_bIndexFieldLens )
  14592. if ( !AddFieldLens ( tSchema, false, sError ) )
  14593. {
  14594. sphWarning ( "index '%s': failed to create field lengths attributes: %s", szIndexName, sError.cstr () );
  14595. return false;
  14596. }
  14597. if ( bWordDict && ( tSettings.m_dPrefixFields.GetLength () || tSettings.m_dInfixFields.GetLength () ) )
  14598. {
  14599. CSphString sWarning = "prefix_fields and infix_fields has no effect with dict=keywords, ignoring";
  14600. if ( pWarnings )
  14601. pWarnings->Add(sWarning);
  14602. else
  14603. sphWarning ( "index '%s': %s", szIndexName, sWarning.cstr() );
  14604. }
  14605. if ( bWordDict && tSettings.m_iMinInfixLen==1 )
  14606. {
  14607. CSphString sWarning = "min_infix_len must be greater than 1, changed to 2";
  14608. if ( pWarnings )
  14609. pWarnings->Add(sWarning);
  14610. else
  14611. sphWarning ( "index '%s': %s", szIndexName, sWarning.cstr() );
  14612. tSettings.m_iMinInfixLen = 2;
  14613. }
  14614. tSchema.SetupFlags ( tSettings, bPercolate, pWarnings );
  14615. return true;
  14616. }
  14617. ///////////////////////////////////////////////
  14618. /// create, configure and load realtime index
  14619. ///////////////////////////////////////////////
  14620. static ResultAndIndex_t LoadRTPercolate ( bool bRT, const char* szIndexName, const CSphConfigSection& hIndex, bool bMutableOpt, StrVec_t* pWarnings, CSphString& sError )
  14621. {
  14622. bool bWordDict = true;
  14623. if ( bRT )
  14624. {
  14625. auto sIndexType = hIndex.GetStr ( "dict", "keywords" );
  14626. bWordDict = true;
  14627. if ( sIndexType=="crc" )
  14628. bWordDict = false;
  14629. else if ( sIndexType!="keywords" )
  14630. {
  14631. sError.SetSprintf ( "index '%s': unknown dict=%s; only 'keywords' or 'crc' values allowed", szIndexName, sIndexType.cstr() );
  14632. return { ADD_ERROR, nullptr };
  14633. }
  14634. }
  14635. CSphSchema tSchema ( szIndexName );
  14636. CSphIndexSettings tSettings;
  14637. if ( !ConfigureRTPercolate ( tSchema, tSettings, szIndexName, hIndex, bWordDict, !bRT, pWarnings, sError ))
  14638. return { ADD_ERROR, nullptr };
  14639. // index
  14640. auto pServed = MakeServedIndex();
  14641. ConfigureLocalIndex ( pServed, hIndex, bMutableOpt, pWarnings );
  14642. pServed->m_sIndexPath = hIndex["path"].strval();
  14643. std::unique_ptr<CSphIndex> pIdx;
  14644. if ( bRT )
  14645. {
  14646. pIdx = sphCreateIndexRT ( tSchema, szIndexName, pServed->m_tSettings.m_iMemLimit, pServed->m_sIndexPath.cstr(), bWordDict );
  14647. pServed->m_eType = IndexType_e::RT;
  14648. } else
  14649. {
  14650. pIdx = CreateIndexPercolate ( tSchema, szIndexName, pServed->m_sIndexPath.cstr() );
  14651. pServed->m_eType = IndexType_e::PERCOLATE;
  14652. }
  14653. pIdx->SetMutableSettings ( pServed->m_tSettings );
  14654. pIdx->m_iExpansionLimit = g_iExpansionLimit;
  14655. pIdx->SetGlobalIDFPath ( pServed->m_sGlobalIDFPath );
  14656. pIdx->Setup ( tSettings );
  14657. pIdx->SetCacheSize ( g_iMaxCachedDocs, g_iMaxCachedHits );
  14658. pServed->SetIdx ( std::move ( pIdx ) );
  14659. return ResultAndIndex_t { ADD_NEEDLOAD, std::move ( pServed ) }; // use Leak to avoid extra addref/release on copying
  14660. }
  14661. ////////////////////////////////////////////
  14662. /// configure and load local index
  14663. ////////////////////////////////////////////
  14664. static ResultAndIndex_t LoadPlainIndex ( const char * szIndexName, const CSphConfigSection & hIndex, bool bMutableOpt, StrVec_t * pWarnings, CSphString & sError )
  14665. {
  14666. // check path
  14667. if ( !hIndex.Exists ( "path" ) )
  14668. {
  14669. sError = "key 'path' not found";
  14670. return { ADD_ERROR, nullptr };
  14671. }
  14672. ServedIndexRefPtr_c pServed = MakeServedIndex();
  14673. pServed->m_eType = IndexType_e::PLAIN;
  14674. // configure memlocking, star
  14675. ConfigureLocalIndex ( pServed, hIndex, bMutableOpt, pWarnings );
  14676. // try to create index
  14677. pServed->m_sIndexPath = hIndex["path"].strval ();
  14678. auto pIdx = sphCreateIndexPhrase ( szIndexName, pServed->m_sIndexPath.cstr() );
  14679. pIdx->m_iExpansionLimit = g_iExpansionLimit;
  14680. pIdx->SetMutableSettings ( pServed->m_tSettings );
  14681. pIdx->SetGlobalIDFPath ( pServed->m_sGlobalIDFPath );
  14682. pIdx->SetCacheSize ( g_iMaxCachedDocs, g_iMaxCachedHits );
  14683. pServed->SetIdx ( std::move ( pIdx ) );
  14684. return ResultAndIndex_t { ADD_NEEDLOAD, std::move ( pServed ) };
  14685. }
  14686. ///////////////////////////////////////////////
  14687. /// make and configure template index
  14688. ///////////////////////////////////////////////
  14689. static ResultAndIndex_t LoadTemplateIndex ( const char * szIndexName, const CSphConfigSection &hIndex, bool bMutableOpt, StrVec_t * pWarnings )
  14690. {
  14691. CSphIndexSettings tSettings;
  14692. CSphString sWarning, sError;
  14693. if ( !tSettings.Setup ( hIndex, szIndexName, sWarning, sError ) )
  14694. {
  14695. sphWarning ( "failed to configure index %s: %s", szIndexName, sError.cstr () );
  14696. return { ADD_ERROR, nullptr };
  14697. }
  14698. if ( !sWarning.IsEmpty() )
  14699. sphWarning ( "index '%s': %s - NOT SERVING", szIndexName, sWarning.cstr () );
  14700. auto pIdx = sphCreateIndexTemplate ( szIndexName );
  14701. pIdx->Setup ( tSettings );
  14702. auto pServed = MakeServedIndex();
  14703. pServed->m_eType = IndexType_e::TEMPLATE;
  14704. // configure memlocking, star
  14705. ConfigureLocalIndex ( pServed, hIndex, bMutableOpt, pWarnings );
  14706. pIdx->SetMutableSettings ( pServed->m_tSettings );
  14707. pIdx->m_iExpansionLimit = g_iExpansionLimit;
  14708. std::unique_ptr<FilenameBuilder_i> pFilenameBuilder = CreateFilenameBuilder ( szIndexName );
  14709. StrVec_t dWarnings;
  14710. if ( !sphFixupIndexSettings ( pIdx.get(), hIndex, g_bStripPath, pFilenameBuilder.get(), dWarnings, sError ) )
  14711. {
  14712. sphWarning ( "index '%s': %s - NOT SERVING", szIndexName, sError.cstr () );
  14713. return { ADD_ERROR, nullptr };
  14714. }
  14715. for ( const auto & i : dWarnings )
  14716. sphWarning ( "index '%s': %s", szIndexName, i.cstr() );
  14717. // templates we either add, either replace depending on requested action
  14718. // at this point they are production-ready
  14719. pServed->SetIdx ( std::move ( pIdx ) );
  14720. return ResultAndIndex_t { ADD_SERVED, std::move ( pServed ) };
  14721. }
  14722. // HandleCommandClusterPq() -> RemoteLoadIndex() -> LoadIndex() -> AddIndex() // only Percolate! From other threads
  14723. // HandleMysqlCreateTable() -> CreateNewIndexConfigless() -> AddIndex() // from other threads
  14724. // ServiceMain() -> TickHead() -> CheckRotate() -> ReloadConfigAndRotateIndexes() -> AddIndex()
  14725. // ConfigureAndPreloadIndex() -> AddIndex() // maybe from non-main thread!
  14726. // ClientSession_c::Execute -> HandleMysqlImportTable -> AddExistingIndexConfigless -> ConfiglessPreloadIndex -> ConfigureAndPreloadIndex -> AddIndex
  14727. ResultAndIndex_t AddIndex ( const char * szIndexName, const CSphConfigSection & hIndex, bool bCheckDupe, bool bMutableOpt, StrVec_t * pWarnings, CSphString & sError )
  14728. {
  14729. // check name
  14730. if ( bCheckDupe && IndexIsServed ( szIndexName ) )
  14731. {
  14732. sphWarning ( "index '%s': duplicate name - NOT SERVING", szIndexName );
  14733. return { ADD_ERROR, nullptr };
  14734. }
  14735. switch ( TypeOfIndexConfig ( hIndex.GetStr ( "type", nullptr )))
  14736. {
  14737. case IndexType_e::DISTR:
  14738. return AddDistributedIndex ( szIndexName, hIndex, sError, pWarnings );
  14739. case IndexType_e::RT:
  14740. return LoadRTPercolate ( true, szIndexName, hIndex, bMutableOpt, pWarnings, sError );
  14741. case IndexType_e::PERCOLATE:
  14742. return LoadRTPercolate ( false, szIndexName, hIndex, bMutableOpt, pWarnings, sError );
  14743. case IndexType_e::TEMPLATE:
  14744. return LoadTemplateIndex ( szIndexName, hIndex, bMutableOpt, pWarnings );
  14745. case IndexType_e::PLAIN:
  14746. return LoadPlainIndex ( szIndexName, hIndex, bMutableOpt, pWarnings, sError );
  14747. case IndexType_e::ERROR_:
  14748. default:
  14749. break;
  14750. }
  14751. sphWarning ( "index '%s': unknown type '%s' - NOT SERVING", szIndexName, hIndex["type"].cstr() );
  14752. return { ADD_ERROR, nullptr };
  14753. }
  14754. // check if config changed, and also cache content into g_dConfig (will be used instead of one more config touching)
  14755. CSphVector<char> g_dConfig;
  14756. bool LoadAndCheckConfig ()
  14757. {
  14758. static DWORD uCfgCRC32 = 0;
  14759. static struct stat tCfgStat;
  14760. g_dConfig.Reset();
  14761. DWORD uCRC32 = 0;
  14762. struct_stat tStat = {0};
  14763. const size_t BUF_SIZE = 8192;
  14764. char sBuf [ BUF_SIZE ];
  14765. FILE * fp = nullptr;
  14766. fp = fopen ( g_sConfigFile.scstr(), "rb" );
  14767. if ( !fp )
  14768. return true;
  14769. if ( fstat ( fileno ( fp ), &tStat )<0 )
  14770. memset ( &tStat, 0, sizeof ( tStat ) );
  14771. bool bGotLine = !!fgets ( sBuf, sizeof(sBuf), fp );
  14772. if ( !bGotLine )
  14773. {
  14774. fclose ( fp );
  14775. return true;
  14776. }
  14777. char * p;
  14778. const char* pEnd = sBuf + BUF_SIZE;
  14779. for ( p = sBuf; p<pEnd; ++p )
  14780. if ( !isspace(*p) )
  14781. break;
  14782. #if _WIN32
  14783. bool bIsWindows = true;
  14784. #else
  14785. bool bIsWindows = false;
  14786. #endif
  14787. if ( !bIsWindows && p<sBuf+BUF_SIZE-1 && p[0]=='#' && p[1]=='!' )
  14788. {
  14789. sBuf[BUF_SIZE-1] = '\0'; // just safety
  14790. fclose ( fp );
  14791. if ( !TryToExec ( p+2, g_sConfigFile.cstr(), g_dConfig ) )
  14792. {
  14793. g_dConfig.Reset();
  14794. return true;
  14795. }
  14796. uCRC32 = sphCRC32 ( g_dConfig.Begin(), g_dConfig.GetLength() );
  14797. } else
  14798. {
  14799. while ( bGotLine ) {
  14800. auto iLen = (int) strlen ( sBuf );
  14801. g_dConfig.Append ( sBuf, iLen );
  14802. bGotLine = !!fgets ( sBuf, sizeof ( sBuf ), fp );
  14803. }
  14804. g_dConfig.Add('\0');
  14805. fclose ( fp );
  14806. uCRC32 = sphCRC32 ( g_dConfig.Begin (), g_dConfig.GetLength ());
  14807. }
  14808. if ( uCfgCRC32==uCRC32 && tStat.st_size==tCfgStat.st_size
  14809. && tStat.st_mtime==tCfgStat.st_mtime && tStat.st_ctime==tCfgStat.st_ctime )
  14810. return false;
  14811. uCfgCRC32 = uCRC32;
  14812. tCfgStat = tStat;
  14813. return true;
  14814. }
  14815. void CleanLoadedConfig ()
  14816. {
  14817. if ( g_bCleanLoadedConfig )
  14818. g_dConfig.Reset();
  14819. }
  14820. // add or remove persistent pools to hosts
  14821. void InitPersistentPool()
  14822. {
  14823. if ( !g_iPersistentPoolSize )
  14824. {
  14825. ClosePersistentSockets();
  14826. return;
  14827. }
  14828. Dashboard::GetActiveHosts ().Apply ( [] ( HostDashboardRefPtr_t& pHost ) {
  14829. if ( !pHost->m_pPersPool )
  14830. pHost->m_pPersPool = new PersistentConnectionsPool_c;
  14831. pHost->m_pPersPool->ReInit ( g_iPersistentPoolSize );
  14832. } );
  14833. }
  14834. // special pass for 'simple' rotation (i.e. *.new to current)
  14835. static void IssuePlainOldRotation ( HashOfServed_c& hDeferred )
  14836. {
  14837. ConfigReloader_c tReloader { hDeferred };
  14838. tReloader.IssuePlainOldRotation();
  14839. }
  14840. // Reloading called always from same thread (so, for now not need to be th-safe for itself)
  14841. // ServiceMain() -> TickHead() -> CheckRotate() -> ReloadConfigAndRotateIndexes().
  14842. static void ReloadIndexesFromConfig ( const CSphConfig& hConf, HashOfServed_c& hDeferred ) REQUIRES ( MainThread )
  14843. {
  14844. assert ( !IsConfigless() );
  14845. if ( !hConf.Exists ("index") )
  14846. {
  14847. sphInfo ( "No indexes found in config came to rotation. Abort reloading");
  14848. return;
  14849. }
  14850. ConfigReloader_c tReloader { hDeferred };
  14851. for ( const auto& dIndex : hConf["index"] )
  14852. {
  14853. const auto & sIndexName = dIndex.first;
  14854. const CSphConfigSection & hIndex = dIndex.second;
  14855. IndexType_e eNewType = TypeOfIndexConfig ( hIndex.GetStr ( "type", nullptr ) );
  14856. if ( eNewType==IndexType_e::ERROR_ )
  14857. continue;
  14858. tReloader.LoadIndexFromConfig ( sIndexName, eNewType, hIndex );
  14859. }
  14860. InitPersistentPool();
  14861. }
  14862. struct IndexWithPriority_t
  14863. {
  14864. CSphString m_sIndex;
  14865. StrVec_t m_dKilllistTargets;
  14866. int m_iPriority {-1};
  14867. int m_nReferences {0};
  14868. };
  14869. static void SetIndexPriority ( IndexWithPriority_t & tIndex, int iPriority, const SmallStringHash_T<IndexWithPriority_t> & tIndexHash )
  14870. {
  14871. tIndex.m_iPriority = Max ( tIndex.m_iPriority, iPriority );
  14872. for ( const auto & i : tIndex.m_dKilllistTargets )
  14873. {
  14874. IndexWithPriority_t * pIdx = tIndexHash(i);
  14875. if ( pIdx )
  14876. SetIndexPriority ( *pIdx, iPriority+1, tIndexHash );
  14877. }
  14878. }
  14879. static VecOfServed_c ConvertHashToPrioritySortedVec ( const HashOfServed_c& hDeferredIndexes ) REQUIRES ( MainThread )
  14880. {
  14881. SmallStringHash_T<IndexWithPriority_t> tIndexesToRotate;
  14882. VecOfServed_c dResult;
  14883. for ( const auto& it : hDeferredIndexes )
  14884. {
  14885. assert ( it.second );
  14886. // check for rt/percolate. they don't need killlist_target
  14887. if ( !ServedDesc_t::IsMutable ( it.second ) && g_pLocalIndexes->Contains ( it.first ) )
  14888. {
  14889. IndexWithPriority_t tToRotate;
  14890. tToRotate.m_sIndex = it.first;
  14891. tToRotate.m_dKilllistTargets = it.second->m_dKilllistTargets;
  14892. tIndexesToRotate.Add ( std::move ( tToRotate ), it.first );
  14893. }
  14894. else
  14895. dResult.Add ( { it.first, it.second } ); // out or priority - will be processed first.
  14896. };
  14897. // set priorities
  14898. for ( const auto& tIndexToRotate : tIndexesToRotate )
  14899. for ( const auto & i : tIndexToRotate.second.m_dKilllistTargets )
  14900. {
  14901. IndexWithPriority_t * pIdx = tIndexesToRotate(i);
  14902. if ( pIdx )
  14903. ++pIdx->m_nReferences;
  14904. }
  14905. // start with the least-referenced index
  14906. IndexWithPriority_t * pMin;
  14907. do
  14908. {
  14909. pMin = nullptr;
  14910. for ( auto & tIndexToRotate : tIndexesToRotate )
  14911. {
  14912. auto & tIdx = tIndexToRotate.second;
  14913. if ( tIdx.m_iPriority==-1 && ( !pMin || tIdx.m_nReferences<pMin->m_nReferences ) )
  14914. pMin = &tIdx;
  14915. }
  14916. if ( pMin )
  14917. SetIndexPriority ( *pMin, 0, tIndexesToRotate );
  14918. }
  14919. while ( pMin );
  14920. // collect and sort by priority processed indexes names
  14921. StrVec_t dSorted;
  14922. for ( auto& tIndexToRotate : tIndexesToRotate )
  14923. dSorted.Add ( tIndexToRotate.first );
  14924. dSorted.Sort ( Lesser ( [&tIndexesToRotate] ( auto a, auto b ) { return tIndexesToRotate[a].m_iPriority < tIndexesToRotate[a].m_iPriority; } ) );
  14925. // append priority names to non-prioritized
  14926. for ( const auto& sIdx : dSorted )
  14927. dResult.Add ( { sIdx, hDeferredIndexes[sIdx] } );
  14928. return dResult;
  14929. }
  14930. // ServiceMain() -> TickHead() -> CheckRotate() -> CheckIndexesForSeamlessAndStartRotation()
  14931. static void CheckIndexesForSeamlessAndStartRotation ( VecOfServed_c dDeferredIndexes ) REQUIRES ( MainThread )
  14932. {
  14933. // check what indexes need to be rotated
  14934. int iNotCapableForSeamlessRotation = 0;
  14935. ARRAY_FOREACH ( i, dDeferredIndexes )
  14936. {
  14937. const auto& sIdx = dDeferredIndexes[i].first;
  14938. auto* pIndex = dDeferredIndexes[i].second.Ptr();
  14939. assert ( pIndex );
  14940. if ( !ServedDesc_t::IsMutable ( pIndex ) && CheckIndexRotate_c ( *pIndex ).NothingToRotate() )
  14941. {
  14942. ++iNotCapableForSeamlessRotation;
  14943. sphLogDebug ( "queue[] = %s", sIdx.cstr() );
  14944. sphLogDebug ( "Index %s (%s) is not capable for seamless rotate. Skipping", sIdx.cstr ()
  14945. , pIndex->m_sIndexPath.cstr () );
  14946. dDeferredIndexes.Remove(i--);
  14947. }
  14948. }
  14949. if ( iNotCapableForSeamlessRotation )
  14950. sphWarning ( "internal error: non-empty queue on a rotation cycle start, got %d elements", iNotCapableForSeamlessRotation );
  14951. if ( dDeferredIndexes.IsEmpty () )
  14952. {
  14953. sphInfo ( "nothing to rotate after SIGHUP" );
  14954. g_bInRotate = false;
  14955. return;
  14956. }
  14957. InvokeRotation ( std::move ( dDeferredIndexes ) );
  14958. }
  14959. // hDeferredIndexes includes both - fresh new, changed and 'just new to current' indexes.
  14960. static void DoGreedyRotation ( VecOfServed_c&& dDeferredIndexes ) REQUIRES ( MainThread )
  14961. {
  14962. assert ( !g_bSeamlessRotate );
  14963. ScRL_t tRotateConfigMutex { g_tRotateConfigMutex };
  14964. for ( auto& dDeferredIndex : dDeferredIndexes )
  14965. {
  14966. const CSphString& sDeferredIndex = dDeferredIndex.first;
  14967. ServedIndexRefPtr_c& pDeferredIndex = dDeferredIndex.second;
  14968. assert ( pDeferredIndex );
  14969. CSphString sError;
  14970. StrVec_t dWarnings;
  14971. // prealloc RT and percolate here
  14972. if ( ServedDesc_t::IsMutable ( pDeferredIndex ) )
  14973. {
  14974. sphLogDebug ( "greedy rotate (prealloc) mutable %s", sDeferredIndex.cstr() );
  14975. if ( PreallocNewIndex ( *pDeferredIndex, &g_hCfg["index"][sDeferredIndex], sDeferredIndex.cstr(), dWarnings, sError ) )
  14976. g_pLocalIndexes->AddOrReplace ( pDeferredIndex, sDeferredIndex );
  14977. else
  14978. sphWarning ( "index '%s': %s - NOT SERVING", sDeferredIndex.cstr(), sError.cstr() );
  14979. }
  14980. else if ( pDeferredIndex->m_eType==IndexType_e::PLAIN )
  14981. {
  14982. sphLogDebug ( "greedy rotate local %s", sDeferredIndex.cstr() );
  14983. auto pRotating = GetServed ( sDeferredIndex );
  14984. bool bSame = pRotating && pRotating.Ptr() == pDeferredIndex.Ptr();
  14985. WIdx_c WIdx { pDeferredIndex };
  14986. bool bOk = RotateIndexGreedy ( *pDeferredIndex, sDeferredIndex.cstr(), sError );
  14987. if ( !bOk )
  14988. sphWarning ( "index '%s': %s - NOT SERVING", sDeferredIndex.cstr(), sError.cstr() );
  14989. if ( !bSame && bOk && !sphFixupIndexSettings ( WIdx, g_hCfg["index"][sDeferredIndex], g_bStripPath, nullptr, dWarnings, sError ) )
  14990. {
  14991. sphWarning ( "index '%s': %s - NOT SERVING", sDeferredIndex.cstr(), sError.cstr() );
  14992. bOk = false;
  14993. }
  14994. if ( bOk )
  14995. {
  14996. WIdx->Preread();
  14997. pDeferredIndex->UpdateMass();
  14998. g_pLocalIndexes->AddOrReplace ( pDeferredIndex, sDeferredIndex );
  14999. }
  15000. }
  15001. for ( const auto & i : dWarnings )
  15002. sphWarning ( "index '%s': %s", sDeferredIndex.cstr(), i.cstr() );
  15003. g_pDistIndexes->Delete ( sDeferredIndex ); // postponed delete of same-named distributed (if any)
  15004. }
  15005. // assert ( dDeferredIndexes.IsEmpty() );
  15006. g_bInRotate = false;
  15007. RotateGlobalIdf ();
  15008. sphInfo ( "rotating finished" );
  15009. }
  15010. // ServiceMain() -> TickHead() -> [CallCoroutine] -> CheckRotate()
  15011. static void CheckRotate () REQUIRES ( MainThread )
  15012. {
  15013. // do we need to rotate now? If no sigHUP received, or if we are already rotating - no.
  15014. // if ( !g_bNeedRotate || g_bInRotate || IsConfigless() )
  15015. // return;
  15016. assert ( !IsConfigless() );
  15017. g_bInRotate = true; // ok, another rotation cycle just started
  15018. g_bNeedRotate = false; // which therefore clears any previous HUP signals
  15019. sphLogDebug ( "CheckRotate invoked" );
  15020. bool bReloadHappened = false;
  15021. HashOfServed_c hDeferredIndexes;
  15022. {
  15023. if ( LoadAndCheckConfig () || g_bReloadForced )
  15024. {
  15025. sphInfo( "Config changed (read %d chars)", g_dConfig.GetLength());
  15026. if ( !g_dConfig.IsEmpty() )
  15027. {
  15028. {
  15029. ScWL_t dRotateConfigMutexWlocked { g_tRotateConfigMutex };
  15030. bReloadHappened = ParseConfig ( &g_hCfg, g_sConfigFile.cstr (), g_dConfig.begin ());
  15031. }
  15032. if ( bReloadHappened )
  15033. {
  15034. ScRL_t dRotateConfigMutexRlocked { g_tRotateConfigMutex };
  15035. ReloadIndexesFromConfig ( g_hCfg, hDeferredIndexes );
  15036. } else
  15037. sphWarning ( "failed to parse config file '%s': %s; using previous settings", g_sConfigFile.cstr(), TlsMsg::szError() );
  15038. }
  15039. }
  15040. CleanLoadedConfig();
  15041. g_bReloadForced = false;
  15042. }
  15043. if ( !bReloadHappened )
  15044. IssuePlainOldRotation ( hDeferredIndexes );
  15045. VecOfServed_c dDeferredIndexes = ConvertHashToPrioritySortedVec ( hDeferredIndexes );
  15046. for ( const auto& s : dDeferredIndexes )
  15047. sphLogDebug ( "will rotate %s", s.first.cstr() );
  15048. if ( g_bSeamlessRotate )
  15049. CheckIndexesForSeamlessAndStartRotation ( std::move ( dDeferredIndexes ) );
  15050. else
  15051. DoGreedyRotation ( std::move ( dDeferredIndexes ) );
  15052. }
  15053. void CheckReopenLogs () REQUIRES ( MainThread )
  15054. {
  15055. if ( !g_bGotSigusr1 )
  15056. return;
  15057. // reopen searchd log
  15058. if ( g_iLogFile>=0 && !g_bLogTty )
  15059. {
  15060. int iFD = ::open ( g_sLogFile.cstr(), O_CREAT | O_RDWR | O_APPEND, S_IREAD | S_IWRITE );
  15061. if ( iFD<0 )
  15062. {
  15063. sphWarning ( "failed to reopen log file '%s': %s", g_sLogFile.cstr(), strerrorm(errno) );
  15064. } else
  15065. {
  15066. ::close ( g_iLogFile );
  15067. g_iLogFile = iFD;
  15068. g_bLogTty = ( isatty ( g_iLogFile )!=0 );
  15069. LogChangeMode ( g_iLogFile, g_iLogFileMode );
  15070. sphInfo ( "log reopened" );
  15071. }
  15072. }
  15073. // reopen query log
  15074. if ( !g_bQuerySyslog && g_iQueryLogFile!=g_iLogFile && g_iQueryLogFile>=0 && !isatty ( g_iQueryLogFile ) )
  15075. {
  15076. int iFD = ::open ( g_sQueryLogFile.cstr(), O_CREAT | O_RDWR | O_APPEND, S_IREAD | S_IWRITE );
  15077. if ( iFD<0 )
  15078. {
  15079. sphWarning ( "failed to reopen query log file '%s': %s", g_sQueryLogFile.cstr(), strerrorm(errno) );
  15080. } else
  15081. {
  15082. ::close ( g_iQueryLogFile );
  15083. g_iQueryLogFile = iFD;
  15084. LogChangeMode ( g_iQueryLogFile, g_iLogFileMode );
  15085. sphInfo ( "query log reopened" );
  15086. }
  15087. }
  15088. g_bGotSigusr1 = 0;
  15089. }
  15090. #if !_WIN32
  15091. #define WINAPI
  15092. #else
  15093. SERVICE_STATUS g_ss;
  15094. SERVICE_STATUS_HANDLE g_ssHandle;
  15095. void MySetServiceStatus ( DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint )
  15096. {
  15097. static DWORD dwCheckPoint = 1;
  15098. if ( dwCurrentState==SERVICE_START_PENDING )
  15099. g_ss.dwControlsAccepted = 0;
  15100. else
  15101. g_ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
  15102. g_ss.dwCurrentState = dwCurrentState;
  15103. g_ss.dwWin32ExitCode = dwWin32ExitCode;
  15104. g_ss.dwWaitHint = dwWaitHint;
  15105. if ( dwCurrentState==SERVICE_RUNNING || dwCurrentState==SERVICE_STOPPED )
  15106. g_ss.dwCheckPoint = 0;
  15107. else
  15108. g_ss.dwCheckPoint = dwCheckPoint++;
  15109. SetServiceStatus ( g_ssHandle, &g_ss );
  15110. }
  15111. void WINAPI ServiceControl ( DWORD dwControlCode )
  15112. {
  15113. switch ( dwControlCode )
  15114. {
  15115. case SERVICE_CONTROL_STOP:
  15116. case SERVICE_CONTROL_SHUTDOWN:
  15117. MySetServiceStatus ( SERVICE_STOP_PENDING, NO_ERROR, 0 );
  15118. g_bServiceStop = true;
  15119. break;
  15120. default:
  15121. MySetServiceStatus ( g_ss.dwCurrentState, NO_ERROR, 0 );
  15122. break;
  15123. }
  15124. }
  15125. // warning! static buffer, non-reentrable
  15126. const char * WinErrorInfo ()
  15127. {
  15128. static char sBuf[1024];
  15129. DWORD uErr = ::GetLastError ();
  15130. snprintf ( sBuf, sizeof(sBuf), "code=%d, error=", uErr );
  15131. auto iLen = (int) strlen(sBuf);
  15132. if ( !FormatMessage ( FORMAT_MESSAGE_FROM_SYSTEM, NULL, uErr, 0, sBuf+iLen, sizeof(sBuf)-iLen, NULL ) ) // FIXME? force US-english langid?
  15133. snprintf ( sBuf+iLen, sizeof(sBuf)-iLen, "(no message)" );
  15134. return sBuf;
  15135. }
  15136. SC_HANDLE ServiceOpenManager ()
  15137. {
  15138. SC_HANDLE hSCM = OpenSCManager (
  15139. NULL, // local computer
  15140. NULL, // ServicesActive database
  15141. SC_MANAGER_ALL_ACCESS ); // full access rights
  15142. if ( hSCM==NULL )
  15143. sphFatal ( "OpenSCManager() failed: %s", WinErrorInfo() );
  15144. return hSCM;
  15145. }
  15146. void AppendArg ( char * sBuf, int iBufLimit, const char * sArg )
  15147. {
  15148. char * sBufMax = sBuf + iBufLimit - 2; // reserve place for opening space and trailing zero
  15149. sBuf += strlen(sBuf);
  15150. if ( sBuf>=sBufMax )
  15151. return;
  15152. auto iArgLen = (int) strlen(sArg);
  15153. bool bQuote = false;
  15154. for ( int i=0; i<iArgLen && !bQuote; i++ )
  15155. if ( sArg[i]==' ' || sArg[i]=='"' )
  15156. bQuote = true;
  15157. *sBuf++ = ' ';
  15158. if ( !bQuote )
  15159. {
  15160. // just copy
  15161. int iToCopy = Min ( sBufMax-sBuf, iArgLen );
  15162. memcpy ( sBuf, sArg, iToCopy );
  15163. sBuf[iToCopy] = '\0';
  15164. } else
  15165. {
  15166. // quote
  15167. sBufMax -= 2; // reserve place for quotes
  15168. if ( sBuf>=sBufMax )
  15169. return;
  15170. *sBuf++ = '"';
  15171. while ( sBuf<sBufMax && *sArg )
  15172. {
  15173. if ( *sArg=='"' )
  15174. {
  15175. // quote
  15176. if ( sBuf<sBufMax-1 )
  15177. {
  15178. *sBuf++ = '\\';
  15179. *sBuf++ = *sArg++;
  15180. }
  15181. } else
  15182. {
  15183. // copy
  15184. *sBuf++ = *sArg++;
  15185. }
  15186. }
  15187. *sBuf++ = '"';
  15188. *sBuf++ = '\0';
  15189. }
  15190. }
  15191. void ServiceInstall ( int argc, char ** argv )
  15192. {
  15193. if ( g_bService )
  15194. return;
  15195. sphInfo ( "Installing service..." );
  15196. char szBinary[MAX_PATH];
  15197. if ( !GetModuleFileName ( NULL, szBinary, MAX_PATH ) )
  15198. sphFatal ( "GetModuleFileName() failed: %s", WinErrorInfo() );
  15199. char szPath[MAX_PATH];
  15200. szPath[0] = '\0';
  15201. AppendArg ( szPath, sizeof(szPath), szBinary );
  15202. AppendArg ( szPath, sizeof(szPath), "--ntservice" );
  15203. for ( int i=1; i<argc; i++ )
  15204. if ( strcmp ( argv[i], "--install" ) )
  15205. AppendArg ( szPath, sizeof(szPath), argv[i] );
  15206. SC_HANDLE hSCM = ServiceOpenManager ();
  15207. SC_HANDLE hService = CreateService (
  15208. hSCM, // SCM database
  15209. g_sServiceName, // name of service
  15210. g_sServiceName, // service name to display
  15211. SERVICE_ALL_ACCESS, // desired access
  15212. SERVICE_WIN32_OWN_PROCESS, // service type
  15213. SERVICE_AUTO_START, // start type
  15214. SERVICE_ERROR_NORMAL, // error control type
  15215. szPath+1, // path to service's binary
  15216. NULL, // no load ordering group
  15217. NULL, // no tag identifier
  15218. NULL, // no dependencies
  15219. NULL, // LocalSystem account
  15220. NULL ); // no password
  15221. if ( !hService )
  15222. {
  15223. CloseServiceHandle ( hSCM );
  15224. sphFatal ( "CreateService() failed: %s", WinErrorInfo() );
  15225. } else
  15226. {
  15227. sphInfo ( "Service '%s' installed successfully.", g_sServiceName );
  15228. }
  15229. CSphString sDesc;
  15230. sDesc.SetSprintf ( "%s-%s", g_sServiceName, g_sStatusVersion.cstr() );
  15231. SERVICE_DESCRIPTION tDesc;
  15232. tDesc.lpDescription = (LPSTR) sDesc.cstr();
  15233. if ( !ChangeServiceConfig2 ( hService, SERVICE_CONFIG_DESCRIPTION, &tDesc ) )
  15234. sphWarning ( "failed to set service description" );
  15235. CloseServiceHandle ( hService );
  15236. CloseServiceHandle ( hSCM );
  15237. }
  15238. void ServiceDelete ()
  15239. {
  15240. if ( g_bService )
  15241. return;
  15242. sphInfo ( "Deleting service..." );
  15243. // open manager
  15244. SC_HANDLE hSCM = ServiceOpenManager ();
  15245. // open service
  15246. SC_HANDLE hService = OpenService ( hSCM, g_sServiceName, DELETE );
  15247. if ( !hService )
  15248. {
  15249. CloseServiceHandle ( hSCM );
  15250. sphFatal ( "OpenService() failed: %s", WinErrorInfo() );
  15251. }
  15252. // do delete
  15253. bool bRes = !!DeleteService ( hService );
  15254. CloseServiceHandle ( hService );
  15255. CloseServiceHandle ( hSCM );
  15256. if ( !bRes )
  15257. sphFatal ( "DeleteService() failed: %s", WinErrorInfo() );
  15258. else
  15259. sphInfo ( "Service '%s' deleted successfully.", g_sServiceName );
  15260. }
  15261. #endif // _WIN32
  15262. void ShowHelp ()
  15263. {
  15264. fprintf ( stdout,
  15265. "Usage: searchd [OPTIONS]\n"
  15266. "\n"
  15267. "Options are:\n"
  15268. "-h, --help\t\tdisplay this help message\n"
  15269. "-v, --version\t\tdisplay version information\n"
  15270. "-c, --config <file>\tread configuration from specified file\n"
  15271. "\t\t\t(default is manticore.conf)\n"
  15272. "--stop\t\t\tsend SIGTERM to currently running searchd\n"
  15273. "--stopwait\t\tsend SIGTERM and wait until actual exit\n"
  15274. "--status\t\tget ant print status variables\n"
  15275. "\t\t\t(PID is taken from pid_file specified in config file)\n"
  15276. "--iostats\t\tlog per-query io stats\n"
  15277. "--cpustats\t\tlog per-query cpu stats\n"
  15278. #if _WIN32
  15279. "--install\t\tinstall as Windows service\n"
  15280. "--delete\t\tdelete Windows service\n"
  15281. "--servicename <name>\tuse given service name (default is 'searchd')\n"
  15282. "--ntservice\t\tinternal option used to invoke a Windows service\n"
  15283. #endif
  15284. "--strip-path\t\tstrip paths from stopwords, wordforms, exceptions\n"
  15285. "\t\t\tand other file names stored in the index header\n"
  15286. "--replay-flags=<OPTIONS>\n"
  15287. "\t\t\textra binary log replay options (current options \n"
  15288. "\t\t\tare 'accept-desc-timestamp' and 'ignore-open-errors')\n"
  15289. "--new-cluster\tbootstraps a replication cluster with cluster restart protection\n"
  15290. "--new-cluster-force\tbootstraps a replication cluster without cluster restart protection\n"
  15291. "\n"
  15292. "Debugging options are:\n"
  15293. "--console\t\trun in console mode (do not fork, do not log to files)\n"
  15294. "-p, --port <port>\tlisten on given port (overrides config setting)\n"
  15295. "-l, --listen <spec>\tlisten on given address, port or path (overrides\n"
  15296. "\t\t\tconfig settings)\n"
  15297. "-i, --index <index>\tonly serve given index(es)\n"
  15298. #if !_WIN32
  15299. "--nodetach\t\tdo not detach into background\n"
  15300. #endif
  15301. "--logdebug, --logdebugv, --logdebugvv\n"
  15302. "\t\t\tenable additional debug information logging\n"
  15303. "\t\t\t(with different verboseness)\n"
  15304. "--pidfile\t\tforce using the PID file (useful with --console)\n"
  15305. "--safetrace\t\tonly use system backtrace() call in crash reports\n"
  15306. "--coredump\t\tsave core dump file on crash\n"
  15307. "\n"
  15308. "Examples:\n"
  15309. "searchd --config /usr/local/sphinx/etc/manticore.conf\n"
  15310. #if _WIN32
  15311. "searchd --install --config c:\\sphinx\\manticore.conf\n"
  15312. #endif
  15313. );
  15314. }
  15315. void InitSharedBuffer ()
  15316. {
  15317. static CSphLargeBuffer<SharedData_t, true> g_dShared;
  15318. CSphString sError;
  15319. if ( !g_dShared.Alloc ( 1, sError ) )
  15320. sphDie ( "failed to allocate shared buffer (msg=%s)", sError.cstr() );
  15321. // reset
  15322. g_pShared = g_dShared.GetWritePtr();
  15323. g_pShared->m_bDaemonAtShutdown = false;
  15324. g_pShared->m_bHaveTTY = false;
  15325. }
  15326. #if _WIN32
  15327. BOOL WINAPI CtrlHandler ( DWORD )
  15328. {
  15329. if ( !g_bService )
  15330. sphInterruptNow();
  15331. return TRUE;
  15332. }
  15333. #endif
  15334. #if !_WIN32
  15335. static char g_sNameBuf[512] = { 0 };
  15336. static char g_sPid[30] = { 0 };
  15337. // returns 'true' only once - at the very start, to show it beatiful way.
  15338. bool SetWatchDog ( int iDevNull ) REQUIRES ( MainThread )
  15339. {
  15340. InitSharedBuffer ();
  15341. // Fork #1 - detach from controlling terminal
  15342. switch ( fork() )
  15343. {
  15344. case -1:
  15345. // error
  15346. sphFatalLog ( "fork() failed (reason: %s)", strerrorm ( errno ) );
  15347. exit ( 1 );
  15348. case 0:
  15349. // daemonized child - or new and free watchdog :)
  15350. break;
  15351. default:
  15352. // tty-controlled parent
  15353. while ( !g_pShared->m_bHaveTTY )
  15354. sphSleepMsec ( 100 );
  15355. exit ( 0 );
  15356. }
  15357. // became the session leader
  15358. if ( setsid()==-1 )
  15359. {
  15360. sphFatalLog ( "setsid() failed (reason: %s)", strerrorm ( errno ) );
  15361. exit ( 1 );
  15362. }
  15363. // Fork #2 - detach from session leadership (may be not necessary, however)
  15364. switch ( fork() )
  15365. {
  15366. case -1:
  15367. // error
  15368. sphFatalLog ( "fork() failed (reason: %s)", strerrorm ( errno ) );
  15369. exit ( 1 );
  15370. case 0:
  15371. // daemonized child - or new and free watchdog :)
  15372. break;
  15373. default:
  15374. // tty-controlled parent
  15375. exit ( 0 );
  15376. }
  15377. // save path to our binary
  15378. g_sNameBuf[::readlink ( "/proc/self/exe", g_sNameBuf, 511 )] = 0;
  15379. // now we are the watchdog. Let us fork the actual process
  15380. enum class EFork { Startup, Disabled, Restart } eReincarnate = EFork::Startup;
  15381. bool bShutdown = false;
  15382. bool bStreamsActive = true;
  15383. int iChild = 0;
  15384. g_iParentPID = getpid();
  15385. assert ( g_pShared );
  15386. while (true)
  15387. {
  15388. if ( eReincarnate!=EFork::Disabled )
  15389. iChild = fork();
  15390. if ( iChild==-1 )
  15391. {
  15392. sphFatalLog ( "fork() failed during watchdog setup (error=%s)", strerrorm(errno) );
  15393. exit ( 1 );
  15394. }
  15395. // child process; return true to show that we have to reload everything
  15396. if ( iChild==0 )
  15397. {
  15398. atexit ( &ReleaseTTYFlag );
  15399. return bStreamsActive;
  15400. }
  15401. // parent process, watchdog
  15402. // close the io files
  15403. if ( bStreamsActive )
  15404. {
  15405. close ( STDIN_FILENO );
  15406. close ( STDOUT_FILENO );
  15407. close ( STDERR_FILENO );
  15408. dup2 ( iDevNull, STDIN_FILENO );
  15409. dup2 ( iDevNull, STDOUT_FILENO );
  15410. dup2 ( iDevNull, STDERR_FILENO );
  15411. bStreamsActive = false;
  15412. }
  15413. if ( eReincarnate!=EFork::Disabled )
  15414. {
  15415. sphInfo ( "watchdog: main process %d forked ok", iChild );
  15416. sprintf ( g_sPid, "%d", iChild);
  15417. }
  15418. SetSignalHandlers();
  15419. eReincarnate = EFork::Disabled;
  15420. int iPid, iStatus;
  15421. while ( ( iPid = wait ( &iStatus ) )>0 )
  15422. {
  15423. const char * sWillRestart = ( g_pShared->m_bDaemonAtShutdown ? "will not be restarted (daemon is shutting down)" : "will be restarted" );
  15424. assert ( iPid==iChild );
  15425. if ( WIFEXITED ( iStatus ) )
  15426. {
  15427. int iExit = WEXITSTATUS ( iStatus );
  15428. if ( iExit==2 || iExit==6 ) // really crash
  15429. {
  15430. sphInfo ( "watchdog: main process %d crashed via CRASH_EXIT (exit code %d), %s", iPid, iExit, sWillRestart );
  15431. eReincarnate = EFork::Restart;
  15432. } else
  15433. {
  15434. sphInfo ( "watchdog: main process %d exited cleanly (exit code %d), shutting down", iPid, iExit );
  15435. bShutdown = true;
  15436. }
  15437. } else if ( WIFSIGNALED ( iStatus ) )
  15438. {
  15439. int iSig = WTERMSIG ( iStatus );
  15440. const char * sSig = NULL;
  15441. if ( iSig==SIGINT )
  15442. sSig = "SIGINT";
  15443. else if ( iSig==SIGTERM )
  15444. sSig = "SIGTERM";
  15445. else if ( WATCHDOG_SIGKILL && iSig==SIGKILL )
  15446. sSig = "SIGKILL";
  15447. if ( sSig )
  15448. {
  15449. sphInfo ( "watchdog: main process %d killed cleanly with %s, shutting down", iPid, sSig );
  15450. bShutdown = true;
  15451. } else
  15452. {
  15453. if ( WCOREDUMP ( iStatus ) )
  15454. sphInfo ( "watchdog: main process %d killed dirtily with signal %d, core dumped, %s",
  15455. iPid, iSig, sWillRestart );
  15456. else
  15457. sphInfo ( "watchdog: main process %d killed dirtily with signal %d, %s",
  15458. iPid, iSig, sWillRestart );
  15459. eReincarnate = EFork::Restart;
  15460. }
  15461. } else if ( WIFSTOPPED ( iStatus ) )
  15462. sphInfo ( "watchdog: main process %d stopped with signal %d", iPid, WSTOPSIG ( iStatus ) );
  15463. #ifdef WIFCONTINUED
  15464. else if ( WIFCONTINUED ( iStatus ) )
  15465. sphInfo ( "watchdog: main process %d resumed", iPid );
  15466. #endif
  15467. }
  15468. if ( iPid==-1 )
  15469. {
  15470. if ( g_bGotSigusr2 )
  15471. {
  15472. g_bGotSigusr2 = 0;
  15473. sphInfo ( "watchdog: got USR2, performing dump of child's stack" );
  15474. sphDumpGdb ( g_iLogFile, g_sNameBuf, g_sPid );
  15475. }
  15476. }
  15477. if ( bShutdown || sphInterrupted() || g_pShared->m_bDaemonAtShutdown )
  15478. {
  15479. exit ( 0 );
  15480. }
  15481. }
  15482. }
  15483. #else
  15484. const int WIN32_PIPE_BUFSIZE=32;
  15485. #endif // !_WIN32
  15486. /// check for incoming signals, and react on them
  15487. void CheckSignals () REQUIRES ( MainThread )
  15488. {
  15489. #if _WIN32
  15490. if ( g_bService && g_bServiceStop )
  15491. {
  15492. Shutdown ();
  15493. MySetServiceStatus ( SERVICE_STOPPED, NO_ERROR, 0 );
  15494. exit ( 0 );
  15495. }
  15496. #endif
  15497. if ( g_bGotSighup )
  15498. {
  15499. sphInfo ( "caught SIGHUP (seamless=%d, in_rotate=%d, need_rotate=%d)", (int)g_bSeamlessRotate, (int)g_bInRotate, (int)g_bNeedRotate );
  15500. g_bNeedRotate = true;
  15501. g_bGotSighup = false;
  15502. }
  15503. if ( sphInterrupted() )
  15504. {
  15505. sphInfo ( "caught SIGTERM, shutting down" );
  15506. Shutdown ();
  15507. exit ( 0 );
  15508. }
  15509. #if _WIN32
  15510. BYTE dPipeInBuf [ WIN32_PIPE_BUFSIZE ];
  15511. DWORD nBytesRead = 0;
  15512. BOOL bSuccess = ReadFile ( g_hPipe, dPipeInBuf, WIN32_PIPE_BUFSIZE, &nBytesRead, NULL );
  15513. if ( nBytesRead > 0 && bSuccess )
  15514. {
  15515. for ( DWORD i=0; i<nBytesRead; i++ )
  15516. {
  15517. switch ( dPipeInBuf[i] )
  15518. {
  15519. case 0:
  15520. g_bGotSighup = 1;
  15521. break;
  15522. case 1:
  15523. sphInterruptNow();
  15524. if ( g_bService )
  15525. g_bServiceStop = true;
  15526. break;
  15527. }
  15528. }
  15529. DisconnectNamedPipe ( g_hPipe );
  15530. ConnectNamedPipe ( g_hPipe, NULL );
  15531. }
  15532. #endif
  15533. }
  15534. void TickHead () REQUIRES ( MainThread )
  15535. {
  15536. CheckSignals ();
  15537. CheckLeaks ();
  15538. CheckReopenLogs ();
  15539. if ( g_bNeedRotate && !g_bInRotate && !IsConfigless() )
  15540. Threads::CallCoroutine ( [] {
  15541. ScopedRole_c thMain ( MainThread );
  15542. CheckRotate();
  15543. } );
  15544. sphInfo ( nullptr ); // flush dupes
  15545. #if _WIN32
  15546. // at windows there is no signals that interrupt sleep
  15547. // need to sleep less to make main loop more responsible
  15548. int tmSleep = 100;
  15549. #else
  15550. int tmSleep = 500;
  15551. #endif
  15552. sphSleepMsec ( tmSleep );
  15553. }
  15554. bool g_bVtune = false;
  15555. int64_t g_tmStarted = 0;
  15556. static int g_iNetWorkers = 1;
  15557. /////////////////////////////////////////////////////////////////////////////
  15558. // DAEMON OPTIONS
  15559. /////////////////////////////////////////////////////////////////////////////
  15560. static std::unique_ptr<QueryParser_i> PercolateQueryParserFactory ( bool bJson )
  15561. {
  15562. if ( bJson )
  15563. return sphCreateJsonQueryParser();
  15564. else
  15565. return sphCreatePlainQueryParser();
  15566. }
  15567. static void ParsePredictedTimeCosts ( const char * p )
  15568. {
  15569. // yet another mini-parser!
  15570. // ident=value [, ident=value [...]]
  15571. sph::ParseKeyValues ( p, [] (CSphString&& sIdent, CSphString&& sValue)
  15572. {
  15573. // bind value
  15574. if ( sIdent=="skip" )
  15575. g_iPredictorCostSkip = atoi ( sValue.cstr ());
  15576. else if ( sIdent=="doc" )
  15577. g_iPredictorCostDoc = atoi ( sValue.cstr ());
  15578. else if ( sIdent=="hit" )
  15579. g_iPredictorCostHit = atoi ( sValue.cstr ());
  15580. else if ( sIdent=="match" )
  15581. g_iPredictorCostMatch = atoi ( sValue.cstr ());
  15582. else
  15583. sphDie ( "predicted_time_costs: unknown identifier '%s' (known ones are skip, doc, hit, match)",
  15584. sIdent.cstr ());
  15585. });
  15586. }
  15587. // read system TFO settings and init g_ITFO according to it.
  15588. /* From https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt
  15589. * possible bitmask values are:
  15590. 0x1: (client) enables sending data in the opening SYN on the client.
  15591. 0x2: (server) enables the server support, i.e., allowing data in
  15592. a SYN packet to be accepted and passed to the
  15593. application before 3-way handshake finishes.
  15594. 0x4: (client) send data in the opening SYN regardless of cookie
  15595. availability and without a cookie option.
  15596. 0x200: (server) accept data-in-SYN w/o any cookie option present.
  15597. 0x400: (server) enable all listeners to support Fast Open by
  15598. default without explicit TCP_FASTOPEN socket option.
  15599. Actually we interested only in first 2 bits.
  15600. */
  15601. static void CheckSystemTFO ()
  15602. {
  15603. #if defined (MSG_FASTOPEN)
  15604. char sBuf[20] = { 0 };
  15605. g_iTFO = TFO_ABSENT;
  15606. FILE * fp = fopen ( "/proc/sys/net/ipv4/tcp_fastopen", "rb" );
  15607. if ( !fp )
  15608. {
  15609. sphInfo ( "TCP fast open unavailable (can't read /proc/sys/net/ipv4/tcp_fastopen, look Server_settings/Searchd#Technical-details-about-Sphinx-API-protocol-and-TFO in manual)" );
  15610. return;
  15611. }
  15612. auto szResult = fgets ( sBuf, 20, fp );
  15613. fclose ( fp );
  15614. if ( !szResult )
  15615. return;
  15616. g_iTFO = atoi ( szResult );
  15617. #else
  15618. g_iTFO = 3; // suggest it is available.
  15619. #endif
  15620. }
  15621. static void ConfigureDaemonLog ( const CSphString & sMode )
  15622. {
  15623. if ( sMode.IsEmpty() )
  15624. return;
  15625. StrVec_t dOpts = sphSplit ( sMode.cstr(), "," );
  15626. SmallStringHash_T<int> hStmt;
  15627. for ( int i=0; i<(int)( sizeof(g_dSqlStmts)/sizeof(g_dSqlStmts[0]) ); i++ )
  15628. hStmt.Add ( i, g_dSqlStmts[i] );
  15629. CSphBitvec tLogStatements ( STMT_TOTAL );
  15630. StringBuilder_c sWrongModes ( "," );
  15631. for ( const CSphString & sOpt : dOpts )
  15632. {
  15633. if ( sOpt=="0" ) // emplicitly disable all statements
  15634. return;
  15635. if ( sOpt=="1" || sOpt=="*" ) // enable all statements
  15636. {
  15637. tLogStatements.Set();
  15638. g_tLogStatements = tLogStatements;
  15639. return;
  15640. }
  15641. // check for whole statement enumerated
  15642. int * pMode = hStmt ( sOpt );
  15643. if ( pMode )
  15644. {
  15645. tLogStatements.BitSet ( *pMode );
  15646. continue;
  15647. }
  15648. bool bHasWild = false;
  15649. for ( const char * s = sOpt.cstr(); *s && !bHasWild; s++ )
  15650. bHasWild = sphIsWild ( *s );
  15651. if ( bHasWild )
  15652. {
  15653. bool bMatched = false;
  15654. for ( int i=0; i<(int)( sizeof(g_dSqlStmts)/sizeof(g_dSqlStmts[0]) ); i++ )
  15655. {
  15656. if ( sphWildcardMatch ( g_dSqlStmts[i], sOpt.cstr() ) )
  15657. {
  15658. tLogStatements.BitSet ( i );
  15659. bMatched = true;
  15660. break;
  15661. }
  15662. }
  15663. if ( bMatched )
  15664. continue;
  15665. }
  15666. sWrongModes += sOpt.cstr();
  15667. }
  15668. if ( tLogStatements.BitCount() )
  15669. g_tLogStatements = tLogStatements;
  15670. if ( !sWrongModes.IsEmpty() )
  15671. sphWarning ( "query_log_statements invalid values: %s", sWrongModes.cstr() );
  15672. }
  15673. void ConfigureSearchd ( const CSphConfig & hConf, bool bOptPIDFile, bool bTestMode ) REQUIRES ( MainThread )
  15674. {
  15675. if ( !hConf.Exists ( "searchd" ) || !hConf["searchd"].Exists ( "searchd" ) )
  15676. sphFatal ( "'searchd' config section not found in '%s'", g_sConfigFile.cstr () );
  15677. const CSphConfigSection & hSearchd = hConf["searchd"]["searchd"];
  15678. sphCheckDuplicatePaths ( hConf );
  15679. if ( bOptPIDFile )
  15680. if ( !hSearchd ( "pid_file" ) )
  15681. sphFatal ( "mandatory option 'pid_file' not found in 'searchd' section" );
  15682. // read_timeout is now deprecated
  15683. g_iReadTimeoutS = hSearchd.GetSTimeS ( "read_timeout", 5);
  15684. // network_timeout overrides read_timeout
  15685. g_iReadTimeoutS = hSearchd.GetSTimeS ( "network_timeout", g_iReadTimeoutS );
  15686. g_iWriteTimeoutS = g_iReadTimeoutS;
  15687. g_iClientQlTimeoutS = hSearchd.GetSTimeS( "sphinxql_timeout", 900);
  15688. g_iClientTimeoutS = hSearchd.GetSTimeS ( "client_timeout", 300 );
  15689. g_iMaxConnection = hSearchd.GetInt ( "max_connections", g_iMaxConnection );
  15690. g_iThreads = hSearchd.GetInt ( "threads", sphCpuThreadsCount() );
  15691. SetMaxChildrenThreads ( g_iThreads );
  15692. g_iThdQueueMax = hSearchd.GetInt ( "jobs_queue_size", g_iThdQueueMax );
  15693. g_iPersistentPoolSize = hSearchd.GetInt ("persistent_connections_limit");
  15694. MutableIndexSettings_c::GetDefaults().m_bPreopen = hSearchd.GetBool ( "preopen_indexes" );
  15695. sphSetUnlinkOld ( hSearchd.GetBool ( "unlink_old" ) );
  15696. g_iExpansionLimit = hSearchd.GetInt ( "expansion_limit" );
  15697. // initialize buffering settings
  15698. SetUnhintedBuffer ( hSearchd.GetSize( "read_unhinted", DEFAULT_READ_UNHINTED ) );
  15699. int iReadBuffer = hSearchd.GetSize ( "read_buffer", DEFAULT_READ_BUFFER );
  15700. FileAccessSettings_t & tDefaultFA = MutableIndexSettings_c::GetDefaults().m_tFileAccess;
  15701. tDefaultFA.m_iReadBufferDocList = hSearchd.GetSize ( "read_buffer_docs", iReadBuffer );
  15702. tDefaultFA.m_iReadBufferHitList = hSearchd.GetSize ( "read_buffer_hits", iReadBuffer );
  15703. tDefaultFA.m_eDoclist = GetFileAccess( hSearchd, "access_doclists", true, FileAccess_e::FILE );
  15704. tDefaultFA.m_eHitlist = GetFileAccess( hSearchd, "access_hitlists", true, FileAccess_e::FILE );
  15705. tDefaultFA.m_eAttr = FileAccess_e::MMAP_PREREAD;
  15706. tDefaultFA.m_eBlob = FileAccess_e::MMAP_PREREAD;
  15707. tDefaultFA.m_eAttr = GetFileAccess( hSearchd, "access_plain_attrs", false, tDefaultFA.m_eAttr );
  15708. tDefaultFA.m_eBlob = GetFileAccess( hSearchd, "access_blob_attrs", false, tDefaultFA.m_eBlob );
  15709. if ( hSearchd("subtree_docs_cache") )
  15710. g_iMaxCachedDocs = hSearchd.GetSize ( "subtree_docs_cache", g_iMaxCachedDocs );
  15711. if ( hSearchd("subtree_hits_cache") )
  15712. g_iMaxCachedHits = hSearchd.GetSize ( "subtree_hits_cache", g_iMaxCachedHits );
  15713. if ( hSearchd("seamless_rotate") )
  15714. g_bSeamlessRotate = ( hSearchd["seamless_rotate"].intval()!=0 );
  15715. if ( hSearchd ( "grouping_in_utc" ) )
  15716. {
  15717. g_bGroupingInUtc = (hSearchd["grouping_in_utc"].intval ()!=0);
  15718. SetGroupingInUtcExpr ( g_bGroupingInUtc );
  15719. SetGroupingInUtcSort ( g_bGroupingInUtc );
  15720. }
  15721. // sha1 password hash for shutdown action
  15722. g_sShutdownToken = hSearchd.GetStr ("shutdown_token");
  15723. if ( !g_bSeamlessRotate && MutableIndexSettings_c::GetDefaults().m_bPreopen && !bTestMode )
  15724. sphWarning ( "preopen_indexes=1 has no effect with seamless_rotate=0" );
  15725. SetAttrFlushPeriod ( hSearchd.GetUsTime64S ( "attr_flush_period", 0 ));
  15726. g_iMaxPacketSize = hSearchd.GetSize ( "max_packet_size", g_iMaxPacketSize );
  15727. g_iMaxFilters = hSearchd.GetInt ( "max_filters", g_iMaxFilters );
  15728. g_iMaxFilterValues = hSearchd.GetInt ( "max_filter_values", g_iMaxFilterValues );
  15729. g_iMaxBatchQueries = hSearchd.GetInt ( "max_batch_queries", g_iMaxBatchQueries );
  15730. g_iDistThreads = hSearchd.GetInt ( "max_threads_per_query", g_iDistThreads );
  15731. sphSetThrottling ( hSearchd.GetInt ( "rt_merge_iops", 0 ), hSearchd.GetSize ( "rt_merge_maxiosize", 0 ) );
  15732. g_iPingIntervalUs = hSearchd.GetUsTime64Ms ( "ha_ping_interval", 1000000 );
  15733. g_uHAPeriodKarmaS = hSearchd.GetSTimeS ( "ha_period_karma", 60 );
  15734. g_iQueryLogMinMs = hSearchd.GetMsTimeMs ( "query_log_min_msec", g_iQueryLogMinMs );
  15735. g_iAgentConnectTimeoutMs = hSearchd.GetMsTimeMs ( "agent_connect_timeout", g_iAgentConnectTimeoutMs );
  15736. g_iAgentQueryTimeoutMs = hSearchd.GetMsTimeMs ( "agent_query_timeout", g_iAgentQueryTimeoutMs );
  15737. g_iAgentRetryDelayMs = hSearchd.GetMsTimeMs ( "agent_retry_delay", g_iAgentRetryDelayMs );
  15738. if ( g_iAgentRetryDelayMs > MAX_RETRY_DELAY )
  15739. sphWarning ( "agent_retry_delay %d exceeded max recommended %d", g_iAgentRetryDelayMs, MAX_RETRY_DELAY );
  15740. g_iAgentRetryCount = hSearchd.GetInt ( "agent_retry_count", g_iAgentRetryCount );
  15741. if ( g_iAgentRetryCount > MAX_RETRY_COUNT )
  15742. sphWarning ( "agent_retry_count %d exceeded max recommended %d", g_iAgentRetryCount, MAX_RETRY_COUNT );
  15743. g_tmWaitUS = hSearchd.GetUsTime64Ms ( "net_wait_tm", g_tmWaitUS );
  15744. g_iThrottleAction = hSearchd.GetInt ( "net_throttle_action", g_iThrottleAction );
  15745. g_iThrottleAccept = hSearchd.GetInt ( "net_throttle_accept", g_iThrottleAccept );
  15746. g_iNetWorkers = hSearchd.GetInt ( "net_workers", g_iNetWorkers );
  15747. g_iNetWorkers = Max ( g_iNetWorkers, 1 );
  15748. CheckSystemTFO();
  15749. if ( g_iTFO!=TFO_ABSENT && hSearchd.GetInt ( "listen_tfo", 1 )==0 )
  15750. {
  15751. g_iTFO &= ~TFO_LISTEN;
  15752. }
  15753. if ( hSearchd ( "collation_libc_locale" ) )
  15754. {
  15755. auto sLocale = hSearchd.GetStr ( "collation_libc_locale" );
  15756. if ( !setlocale ( LC_COLLATE, sLocale.cstr() ) )
  15757. sphWarning ( "setlocale failed (locale='%s')", sLocale.cstr() );
  15758. }
  15759. if ( hSearchd ( "collation_server" ) )
  15760. {
  15761. CSphString sCollation = hSearchd.GetStr ( "collation_server" );
  15762. CSphString sError;
  15763. GlobalCollation () = sphCollationFromName ( sCollation, &sError );
  15764. if ( !sError.IsEmpty() )
  15765. sphWarning ( "%s", sError.cstr() );
  15766. }
  15767. if ( hSearchd("thread_stack") ) // fixme! rename? That is limit for stack of the coro, not of the thread!
  15768. {
  15769. constexpr int iThreadStackSizeMin = 128*1024;
  15770. int iStackSize = hSearchd.GetSize ( "thread_stack", iThreadStackSizeMin );
  15771. if ( iStackSize<iThreadStackSizeMin )
  15772. sphWarning ( "thread_stack %d less than default (128K), increased", iStackSize );
  15773. iStackSize = Max ( iStackSize, iThreadStackSizeMin );
  15774. Threads::SetMaxCoroStackSize ( iStackSize );
  15775. }
  15776. if ( hSearchd("predicted_time_costs") )
  15777. ParsePredictedTimeCosts ( hSearchd["predicted_time_costs"].cstr() );
  15778. if ( hSearchd("shutdown_timeout") )
  15779. g_iShutdownTimeoutUs = hSearchd.GetUsTime64S ( "shutdown_timeout", 60000000);
  15780. g_iDocstoreCache = hSearchd.GetSize64 ( "docstore_cache_size", 16777216 );
  15781. g_iSkipCache = hSearchd.GetSize64 ( "skiplist_cache_size", 67108864 );
  15782. if ( hSearchd.Exists ( "max_open_files" ) )
  15783. {
  15784. #if HAVE_GETRLIMIT & HAVE_SETRLIMIT
  15785. auto uLimit = ( rlim_t ) hSearchd["max_open_files"].intval ();
  15786. bool bMax = hSearchd["max_open_files"].strval ()=="max";
  15787. if ( !uLimit && !bMax )
  15788. sphWarning ( "max_open_files is %d, expected positive value; ignored", (int) uLimit );
  15789. else
  15790. {
  15791. struct rlimit dRlimit;
  15792. if ( 0!=getrlimit ( RLIMIT_NOFILE, &dRlimit ) )
  15793. sphWarning ( "Failed to getrlimit (RLIMIT_NOFILE), error %d: %s", errno, strerrorm ( errno ) );
  15794. else
  15795. {
  15796. auto uPrevLimit = dRlimit.rlim_cur;
  15797. if ( bMax )
  15798. uLimit = dRlimit.rlim_max;
  15799. dRlimit.rlim_cur = Min ( dRlimit.rlim_max, uLimit );
  15800. if ( 0!=setrlimit ( RLIMIT_NOFILE, &dRlimit ) )
  15801. sphWarning ( "Failed to setrlimit on %d, error %d: %s", (int)uLimit, errno, strerrorm ( errno ) );
  15802. else
  15803. sphInfo ( "Set max_open_files to %d (previous was %d), hardlimit is %d.",
  15804. (int)uLimit, (int)uPrevLimit, (int)dRlimit.rlim_max );
  15805. }
  15806. }
  15807. #else
  15808. sphWarning ("max_open_files defined, but this binary don't know about setrlimit() function");
  15809. #endif
  15810. }
  15811. QcacheStatus_t s = QcacheGetStatus();
  15812. s.m_iMaxBytes = hSearchd.GetSize64 ( "qcache_max_bytes", s.m_iMaxBytes );
  15813. s.m_iThreshMs = hSearchd.GetMsTimeMs ( "qcache_thresh_msec", s.m_iThreshMs );
  15814. s.m_iTtlS = hSearchd.GetSTimeS ( "qcache_ttl_sec", s.m_iTtlS );
  15815. QcacheSetup ( s.m_iMaxBytes, s.m_iThreshMs, s.m_iTtlS );
  15816. // hostname_lookup = {config_load | request}
  15817. g_bHostnameLookup = ( hSearchd.GetStr ( "hostname_lookup" ) == "request" );
  15818. CSphVariant * pLogMode = hSearchd ( "query_log_mode" );
  15819. if ( pLogMode && !pLogMode->strval().IsEmpty() )
  15820. {
  15821. errno = 0;
  15822. int iMode = strtol ( pLogMode->strval().cstr(), NULL, 8 );
  15823. int iErr = errno;
  15824. if ( iErr==ERANGE || iErr==EINVAL )
  15825. {
  15826. sphWarning ( "query_log_mode invalid value (value=%o, error=%s); skipped", iMode, strerrorm(iErr) );
  15827. } else
  15828. {
  15829. g_iLogFileMode = iMode;
  15830. }
  15831. }
  15832. if ( hSearchd ( "server_id" ) )
  15833. {
  15834. g_iServerID = hSearchd.GetInt ( "server_id", g_iServerID );
  15835. g_bServerID = true;
  15836. const int iServerMask = 0x7f;
  15837. if ( g_iServerID>iServerMask )
  15838. {
  15839. g_iServerID &= iServerMask;
  15840. sphWarning ( "server_id out of range 0 - 127, clamped to %d", g_iServerID );
  15841. }
  15842. }
  15843. g_sMySQLVersion = hSearchd.GetStr ( "mysql_version_string", g_sMySQLVersion.cstr() );
  15844. AllowOnlyNot ( hSearchd.GetInt ( "not_terms_only_allowed", 0 )!=0 );
  15845. ConfigureDaemonLog ( hSearchd.GetStr ( "query_log_commands" ) );
  15846. g_iAutoOptimizeCutoffMultiplier = hSearchd.GetInt ( "auto_optimize", 1 );
  15847. MutableIndexSettings_c::GetDefaults().m_iOptimizeCutoff = hSearchd.GetInt ( "optimize_cutoff", AutoOptimizeCutoff() );
  15848. g_bSplit = hSearchd.GetInt ( "pseudo_sharding", 1 )!=0;
  15849. bool bGotSecondary = ( hSearchd.GetInt ( "secondary_indexes", GetSecondaryIndexDefault() )!=0 );
  15850. if ( bGotSecondary && !IsSecondaryLibLoaded() )
  15851. sphFatal ( "secondary_indexes set but failed to initialize secondary library: %s", g_sSecondaryError.cstr() );
  15852. SetSecondaryIndexDefault ( bGotSecondary );
  15853. g_sConfigPath = sphGetCwd();
  15854. }
  15855. static void PutPath ( const CSphString & sCwd, const CSphString & sVar, RowBuffer_i & tOut )
  15856. {
  15857. if ( !IsPathAbsolute ( sVar ) )
  15858. {
  15859. CSphString sPath;
  15860. sPath.SetSprintf ( "%s/%s", sCwd.cstr(), sVar.cstr() );
  15861. tOut.PutString ( sPath );
  15862. } else
  15863. {
  15864. tOut.PutString ( sVar );
  15865. }
  15866. }
  15867. class StringSetStatic_c : public sph::StringSet
  15868. {
  15869. public:
  15870. StringSetStatic_c ( std::initializer_list<const char *> dArgs )
  15871. {
  15872. for ( const char * sName : dArgs )
  15873. Add ( sName );
  15874. }
  15875. };
  15876. static StringSetStatic_c g_hSearchdPathVars {
  15877. "binlog_path"
  15878. , "data_dir"
  15879. , "lemmatizer_base"
  15880. , "log"
  15881. , "pid_file"
  15882. , "plugin_dir"
  15883. , "query_log"
  15884. , "snippets_file_prefix"
  15885. , "sphinxql_state"
  15886. , "ssl_ca"
  15887. , "ssl_cert"
  15888. , "ssl_key"
  15889. };
  15890. static void DumpSettingsSection ( const CSphConfig & hConf, const char * sSectionName, RowBuffer_i & tOut )
  15891. {
  15892. if ( !hConf.Exists ( sSectionName ) || !hConf[sSectionName].Exists ( sSectionName ) )
  15893. return;
  15894. StringBuilder_c tTmp;
  15895. const CSphConfigSection & hNode = hConf[sSectionName][sSectionName];
  15896. for ( const auto & tIt : hNode )
  15897. {
  15898. tTmp.Clear();
  15899. tTmp.Appendf ( "%s.%s", sSectionName, tIt.first.cstr() );
  15900. const CSphVariant * pVal = &tIt.second;
  15901. do
  15902. {
  15903. // data packets
  15904. tOut.PutString ( tTmp.cstr() );
  15905. if ( g_hSearchdPathVars[tIt.first] )
  15906. PutPath ( g_sConfigPath, pVal->strval(), tOut );
  15907. else
  15908. tOut.PutString ( pVal->strval() );
  15909. tOut.Commit();
  15910. pVal = pVal->m_pNext;
  15911. } while ( pVal );
  15912. }
  15913. }
  15914. void HandleMysqlShowSettings ( const CSphConfig & hConf, RowBuffer_i & tOut )
  15915. {
  15916. tOut.HeadBegin( 2 );
  15917. tOut.HeadColumn ( "Setting_name" );
  15918. tOut.HeadColumn ( "Value" );
  15919. tOut.HeadEnd();
  15920. // configuration file path
  15921. tOut.PutString ( "configuration_file" );
  15922. PutPath ( g_sConfigPath, g_sConfigFile, tOut );
  15923. tOut.Commit();
  15924. // pid
  15925. tOut.PutString ( "worker_pid" );
  15926. tOut.PutNumAsString ( (int)getpid() );
  15927. tOut.Commit();
  15928. DumpSettingsSection ( hConf, "searchd", tOut );
  15929. DumpSettingsSection ( hConf, "common", tOut );
  15930. DumpSettingsSection ( hConf, "indexer", tOut );
  15931. // done
  15932. tOut.Eof();
  15933. }
  15934. // load index which is not yet load, and publish it in served indexes.
  15935. // ServiceMain -> ConfigureAndPreloadOnStartup -> ConfigureAndPreloadIndex
  15936. // ServiceMain -> ConfigureAndPreloadOnStartup -> ConfigureAndPreloadConfiglessIndexes -> ConfiglessPreloadIndex -> ConfigureAndPreloadIndex
  15937. // from any another thread:
  15938. // ClientSession_c::Execute -> HandleMysqlImportTable -> AddExistingIndexConfigless -> ConfiglessPreloadIndex -> ConfigureAndPreloadIndex
  15939. ESphAddIndex ConfigureAndPreloadIndex ( const CSphConfigSection & hIndex, const char * sIndexName, StrVec_t & dWarnings, CSphString & sError )
  15940. {
  15941. ESphAddIndex eAdd;
  15942. ServedIndexRefPtr_c pJustLoadedLocal;
  15943. std::tie ( eAdd, pJustLoadedLocal ) = AddIndex ( sIndexName, hIndex, true, false, nullptr, sError );
  15944. // local plain, rt, percolate added, but need to be at least preallocated before they could work.
  15945. switch ( eAdd )
  15946. {
  15947. case ADD_NEEDLOAD:
  15948. {
  15949. assert ( pJustLoadedLocal );
  15950. fprintf ( stdout, "precaching index '%s'\n", sIndexName );
  15951. fflush ( stdout );
  15952. IndexFiles_c dJustAddedFiles ( pJustLoadedLocal->m_sIndexPath );
  15953. if ( dJustAddedFiles.HasAllFiles ( ".new" ) )
  15954. {
  15955. WIdx_c WFake { pJustLoadedLocal }; // as RotateIndexGreedy wants w-locked
  15956. if ( RotateIndexGreedy ( *pJustLoadedLocal, sIndexName, sError ) )
  15957. {
  15958. if ( !FixupAndLockIndex ( *pJustLoadedLocal, UnlockedHazardIdxFromServed ( *pJustLoadedLocal ), &hIndex, sIndexName, dWarnings, sError ) )
  15959. return ADD_ERROR;
  15960. } else
  15961. {
  15962. dWarnings.Add ( sError );
  15963. if ( !PreallocNewIndex ( *pJustLoadedLocal, &hIndex, sIndexName, dWarnings, sError ) )
  15964. return ADD_ERROR;
  15965. }
  15966. } else if ( !PreallocNewIndex ( *pJustLoadedLocal, &hIndex, sIndexName, dWarnings, sError ) )
  15967. return ADD_ERROR;
  15968. }
  15969. // no break
  15970. case ADD_SERVED:
  15971. {
  15972. // finally add the index to the hash of enabled.
  15973. g_pLocalIndexes->Add ( pJustLoadedLocal, sIndexName );
  15974. if ( !pJustLoadedLocal->m_sGlobalIDFPath.IsEmpty() && !sph::PrereadGlobalIDF ( pJustLoadedLocal->m_sGlobalIDFPath, sError ) )
  15975. dWarnings.Add ( "global IDF unavailable - IGNORING" );
  15976. }
  15977. // no sense to break
  15978. case ADD_DISTR:
  15979. case ADD_ERROR:
  15980. default:
  15981. break;
  15982. }
  15983. return eAdd;
  15984. }
  15985. // invoked once on start from ServiceMain (actually it creates the hashes)
  15986. // ServiceMain -> ConfigureAndPreloadOnStartup
  15987. static void ConfigureAndPreloadOnStartup ( const CSphConfig & hConf, const StrVec_t & dOptIndexes ) REQUIRES (MainThread)
  15988. {
  15989. int iCounter = 0;
  15990. int iValidIndexes = 0;
  15991. int64_t tmLoad = -sphMicroTimer();
  15992. if ( hConf.Exists ( "index" ) )
  15993. {
  15994. assert ( !IsConfigless() );
  15995. for ( const auto& tIndex : hConf["index"] )
  15996. {
  15997. const CSphConfigSection & hIndex = tIndex.second;
  15998. const char * sIndexName = tIndex.first.cstr();
  15999. if ( !dOptIndexes.IsEmpty() && !dOptIndexes.any_of ( [&] ( const CSphString &rhs ) { return rhs.EqN ( sIndexName ); } ) )
  16000. continue;
  16001. StrVec_t dWarnings;
  16002. CSphString sError;
  16003. ESphAddIndex eAdd = ConfigureAndPreloadIndex ( hIndex, sIndexName, dWarnings, sError );
  16004. for ( const auto & i : dWarnings )
  16005. sphWarning ( "index '%s': %s", sIndexName, i.cstr() );
  16006. if ( eAdd==ADD_ERROR )
  16007. sphWarning ( "index '%s': %s - NOT SERVING", sIndexName, sError.cstr() );
  16008. iValidIndexes += ( eAdd!=ADD_ERROR ? 1 : 0 );
  16009. iCounter += ( eAdd== ADD_NEEDLOAD ? 1 : 0 );
  16010. }
  16011. } else {
  16012. assert ( IsConfigless() );
  16013. ConfigureAndPreloadConfiglessIndexes ( iValidIndexes, iCounter );
  16014. }
  16015. InitPersistentPool();
  16016. ServedSnap_t hLocal = g_pLocalIndexes->GetHash();
  16017. for ( const auto& tIt : *hLocal )
  16018. {
  16019. auto pServed = tIt.second;
  16020. if ( pServed )
  16021. {
  16022. CSphString sWarning, sError;
  16023. RIdx_c pIdx { pServed };
  16024. if ( !ApplyIndexKillList ( pIdx, sWarning, sError, true ) )
  16025. sphWarning ( "index '%s': error applying killlist: %s", pIdx->GetName(), sError.cstr() );
  16026. if ( sWarning.Length() )
  16027. sphWarning ( "%s", sWarning.cstr() );
  16028. }
  16029. }
  16030. // set index cluster name for check
  16031. for ( const ClusterDesc_t & tClusterDesc : GetClustersInt() )
  16032. for ( const CSphString & sIndexName : tClusterDesc.m_dIndexes )
  16033. SetIndexCluster ( sIndexName, tClusterDesc.m_sName );
  16034. sphLogDebugRpl ( "%d clusters loaded from config", GetClustersInt().GetLength() );
  16035. tmLoad += sphMicroTimer();
  16036. if ( !iValidIndexes )
  16037. sphLogDebug ( "no valid indexes to serve" );
  16038. else
  16039. fprintf ( stdout, "precached %d indexes in %0.3f sec\n", iCounter, float(tmLoad)/1000000 );
  16040. }
  16041. static CSphString FixupFilename ( const CSphString & sFilename )
  16042. {
  16043. CSphString sFixed = sFilename;
  16044. #if _WIN32
  16045. sFixed = AppendWinInstallDir(sFixed);
  16046. #endif
  16047. return sFixed;
  16048. }
  16049. void OpenDaemonLog ( const CSphConfigSection & hSearchd, bool bCloseIfOpened=false )
  16050. {
  16051. CSphString sLog = "searchd.log";
  16052. if ( hSearchd.Exists ( "log" ) )
  16053. {
  16054. if ( hSearchd["log"]=="syslog" )
  16055. {
  16056. #if !USE_SYSLOG
  16057. if ( g_iLogFile<0 )
  16058. {
  16059. g_iLogFile = STDOUT_FILENO;
  16060. sphWarning ( "failed to use syslog for logging. You have to reconfigure --with-syslog and rebuild the daemon!" );
  16061. sphInfo ( "will use default file 'searchd.log' for logging." );
  16062. }
  16063. #else
  16064. g_bLogSyslog = true;
  16065. #endif
  16066. } else
  16067. sLog = FixupFilename ( hSearchd["log"].cstr() );
  16068. }
  16069. umask ( 066 );
  16070. if ( bCloseIfOpened && g_iLogFile!=STDOUT_FILENO )
  16071. {
  16072. close ( g_iLogFile );
  16073. g_iLogFile = STDOUT_FILENO;
  16074. }
  16075. if ( !g_bLogSyslog )
  16076. {
  16077. g_iLogFile = open ( sLog.cstr(), O_CREAT | O_RDWR | O_APPEND, S_IREAD | S_IWRITE );
  16078. if ( g_iLogFile<0 )
  16079. {
  16080. g_iLogFile = STDOUT_FILENO;
  16081. sphFatal ( "failed to open log file '%s': %s", sLog.cstr(), strerrorm(errno) );
  16082. }
  16083. LogChangeMode ( g_iLogFile, g_iLogFileMode );
  16084. }
  16085. g_sLogFile = sLog;
  16086. g_bLogTty = isatty ( g_iLogFile )!=0;
  16087. }
  16088. static void SetUidShort ( bool bTestMode )
  16089. {
  16090. const int iServerMask = 0x7f;
  16091. int iServerId = g_iServerID;
  16092. uint64_t uStartedSec = 0;
  16093. if ( !bTestMode )
  16094. {
  16095. // server id as high part of counter
  16096. if ( g_bServerID )
  16097. {
  16098. iServerId = g_iServerID;
  16099. } else
  16100. {
  16101. CSphString sMAC = GetMacAddress();
  16102. sphLogDebug ( "MAC address %s for uuid-short server_id", sMAC.cstr() );
  16103. if ( sMAC.IsEmpty() )
  16104. {
  16105. DWORD uSeed = sphRand();
  16106. sMAC.SetSprintf ( "%u", uSeed );
  16107. sphWarning ( "failed to get MAC address, using random number %s", sMAC.cstr() );
  16108. }
  16109. // fold MAC into 1 byte
  16110. iServerId = Pearson8 ( (const BYTE *)sMAC.cstr(), sMAC.Length() );
  16111. iServerId &= iServerMask;
  16112. }
  16113. // start time Unix timestamp as middle part of counter
  16114. uStartedSec = sphMicroTimer() / 1000000;
  16115. // base timestamp is 01 May of 2019
  16116. const uint64_t uBaseSec = 1556668800;
  16117. if ( uStartedSec>uBaseSec )
  16118. uStartedSec -= uBaseSec;
  16119. } else
  16120. {
  16121. // need constant seed across all environments for tests
  16122. uStartedSec = 100000;
  16123. iServerId = g_iServerID;
  16124. }
  16125. UidShortSetup ( iServerId, (int)uStartedSec );
  16126. }
  16127. namespace { // static
  16128. // implement '--stop' and '--stopwait' (connect and stop another instance by pid file from config)
  16129. void StopOrStopWaitAnother ( CSphVariant * v, bool bWait ) REQUIRES ( MainThread )
  16130. {
  16131. if ( !v )
  16132. sphFatal ( "stop: option 'pid_file' not found in '%s' section 'searchd'", g_sConfigFile.cstr () );
  16133. CSphString sPidFile = FixupFilename ( v->cstr () );
  16134. FILE * fp = fopen ( sPidFile.cstr(), "r" );
  16135. if ( !fp )
  16136. sphFatal ( "stop: pid file '%s' does not exist or is not readable", sPidFile.cstr() );
  16137. char sBuf[16];
  16138. int iLen = (int) fread ( sBuf, 1, sizeof(sBuf)-1, fp );
  16139. sBuf[iLen] = '\0';
  16140. fclose ( fp );
  16141. int iPid = atoi(sBuf);
  16142. if ( iPid<=0 )
  16143. sphFatal ( "stop: failed to read valid pid from '%s'", sPidFile.cstr() );
  16144. int iWaitTimeout = g_iShutdownTimeoutUs + 100000;
  16145. #if _WIN32
  16146. bool bTerminatedOk = false;
  16147. char szPipeName[64];
  16148. snprintf ( szPipeName, sizeof(szPipeName), "\\\\.\\pipe\\searchd_%d", iPid );
  16149. HANDLE hPipe = INVALID_HANDLE_VALUE;
  16150. while ( hPipe==INVALID_HANDLE_VALUE )
  16151. {
  16152. hPipe = CreateFile ( szPipeName, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
  16153. if ( hPipe==INVALID_HANDLE_VALUE )
  16154. {
  16155. if ( GetLastError()!=ERROR_PIPE_BUSY )
  16156. {
  16157. fprintf ( stdout, "WARNING: could not open pipe (GetLastError()=%d)\n", GetLastError () );
  16158. break;
  16159. }
  16160. if ( !WaitNamedPipe ( szPipeName, iWaitTimeout/1000 ) )
  16161. {
  16162. fprintf ( stdout, "WARNING: could not open pipe (GetLastError()=%d)\n", GetLastError () );
  16163. break;
  16164. }
  16165. }
  16166. }
  16167. if ( hPipe!=INVALID_HANDLE_VALUE )
  16168. {
  16169. DWORD uWritten = 0;
  16170. BYTE uWrite = 1;
  16171. BOOL bResult = WriteFile ( hPipe, &uWrite, 1, &uWritten, NULL );
  16172. if ( !bResult )
  16173. fprintf ( stdout, "WARNING: failed to send SIGHTERM to searchd (pid=%d, GetLastError()=%d)\n", iPid, GetLastError () );
  16174. bTerminatedOk = !!bResult;
  16175. CloseHandle ( hPipe );
  16176. }
  16177. if ( bTerminatedOk )
  16178. {
  16179. sphInfo ( "stop: successfully terminated pid %d", iPid );
  16180. exit ( 0 );
  16181. } else
  16182. sphFatal ( "stop: error terminating pid %d", iPid );
  16183. #else
  16184. CSphString sPipeName;
  16185. int iPipeCreated = -1;
  16186. int fdPipe = -1;
  16187. if ( bWait )
  16188. {
  16189. sPipeName = GetNamedPipeName ( iPid );
  16190. ::unlink ( sPipeName.cstr () ); // avoid garbage to pollute us
  16191. int iMask = umask ( 0 );
  16192. iPipeCreated = mkfifo ( sPipeName.cstr(), 0666 );
  16193. umask ( iMask );
  16194. if ( iPipeCreated!=-1 )
  16195. fdPipe = ::open ( sPipeName.cstr(), O_RDONLY | O_NONBLOCK );
  16196. if ( iPipeCreated==-1 )
  16197. sphWarning ( "mkfifo failed (path=%s, err=%d, msg=%s); will NOT wait", sPipeName.cstr(), errno, strerrorm(errno) );
  16198. else if ( fdPipe<0 )
  16199. sphWarning ( "open failed (path=%s, err=%d, msg=%s); will NOT wait", sPipeName.cstr(), errno, strerrorm(errno) );
  16200. }
  16201. if ( kill ( iPid, SIGTERM ) )
  16202. sphFatal ( "stop: kill() on pid %d failed: %s", iPid, strerrorm(errno) );
  16203. else
  16204. sphInfo ( "stop: successfully sent SIGTERM to pid %d", iPid );
  16205. int iExitCode = ( bWait && ( iPipeCreated==-1 || fdPipe<0 ) ) ? 1 : 0;
  16206. bool bHandshake = true;
  16207. if ( bWait && fdPipe>=0 )
  16208. while ( true )
  16209. {
  16210. int iReady = sphPoll ( fdPipe, iWaitTimeout );
  16211. // error on wait
  16212. if ( iReady<0 )
  16213. {
  16214. iExitCode = 3;
  16215. sphWarning ( "stopwait%s error '%s'", ( bHandshake ? " handshake" : " " ), strerrorm(errno) );
  16216. break;
  16217. }
  16218. // timeout
  16219. if ( iReady==0 )
  16220. {
  16221. if ( !bHandshake )
  16222. continue;
  16223. iExitCode = 1;
  16224. break;
  16225. }
  16226. // reading data
  16227. DWORD uStatus = 0;
  16228. int iRead = ::read ( fdPipe, &uStatus, sizeof(DWORD) );
  16229. if ( iRead!=sizeof(DWORD) )
  16230. {
  16231. sphWarning ( "stopwait read fifo error '%s'", strerrorm(errno) );
  16232. iExitCode = 3; // stopped demon crashed during stop
  16233. break;
  16234. } else
  16235. {
  16236. iExitCode = ( uStatus==1 ? 0 : 2 ); // uStatus == 1 - AttributeSave - ok, other values - error
  16237. }
  16238. if ( !bHandshake )
  16239. break;
  16240. bHandshake = false;
  16241. }
  16242. ::unlink ( sPipeName.cstr () ); // is ok on linux after it is opened.
  16243. if ( fdPipe>=0 )
  16244. ::close ( fdPipe );
  16245. exit ( iExitCode );
  16246. #endif
  16247. }
  16248. } // static namespace
  16249. static void InitBanner()
  16250. {
  16251. const char * szColumnarVer = GetColumnarVersionStr();
  16252. CSphString sColumnar = "";
  16253. if ( szColumnarVer )
  16254. sColumnar.SetSprintf ( " (columnar %s)", szColumnarVer );
  16255. const char * sSiVer = GetSecondaryVersionStr();
  16256. CSphString sSi = "";
  16257. if ( sSiVer )
  16258. sSi.SetSprintf ( " (secondary %s)", sSiVer );
  16259. g_sBannerVersion.SetSprintf ( "%s%s%s", szMANTICORE_NAME, sColumnar.cstr(), sSi.cstr() );
  16260. g_sBanner.SetSprintf ( "%s%s", g_sBannerVersion.cstr(), szMANTICORE_BANNER_TEXT );
  16261. g_sMySQLVersion.SetSprintf ( "%s%s%s", szMANTICORE_VERSION, sColumnar.cstr(), sSi.cstr() );
  16262. g_sStatusVersion.SetSprintf ( "%s%s%s", szMANTICORE_VERSION, sColumnar.cstr(), sSi.cstr() );
  16263. }
  16264. static void CheckSSL ()
  16265. {
  16266. // check for SSL inited well
  16267. for ( const auto & tListener : g_dListeners )
  16268. {
  16269. CSphString sError;
  16270. if ( tListener.m_eProto==Proto_e::HTTPS )
  16271. {
  16272. if ( !CheckWeCanUseSSL ( &sError ) )
  16273. sphWarning ( "SSL init error: %s", sError.cstr() );
  16274. break;
  16275. }
  16276. }
  16277. }
  16278. int WINAPI ServiceMain ( int argc, char **argv ) EXCLUDES (MainThread)
  16279. {
  16280. ScopedRole_c thMain (MainThread);
  16281. g_bLogTty = isatty ( g_iLogFile )!=0;
  16282. #ifdef USE_VTUNE
  16283. __itt_pause ();
  16284. #endif // USE_VTUNE
  16285. g_tmStarted = sphMicroTimer();
  16286. #if _WIN32
  16287. CSphVector<char *> dArgs;
  16288. if ( g_bService )
  16289. {
  16290. g_ssHandle = RegisterServiceCtrlHandler ( g_sServiceName, ServiceControl );
  16291. if ( !g_ssHandle )
  16292. sphFatal ( "failed to start service: RegisterServiceCtrlHandler() failed: %s", WinErrorInfo() );
  16293. g_ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  16294. MySetServiceStatus ( SERVICE_START_PENDING, NO_ERROR, 4000 );
  16295. if ( argc<=1 )
  16296. {
  16297. dArgs.Resize ( g_dArgs.GetLength() );
  16298. ARRAY_FOREACH ( i, g_dArgs )
  16299. dArgs[i] = (char*) g_dArgs[i].cstr();
  16300. argc = g_dArgs.GetLength();
  16301. argv = &dArgs[0];
  16302. }
  16303. }
  16304. char szPipeName[64];
  16305. snprintf ( szPipeName, sizeof(szPipeName), "\\\\.\\pipe\\searchd_%d", getpid() );
  16306. g_hPipe = CreateNamedPipe ( szPipeName, PIPE_ACCESS_INBOUND,
  16307. PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT,
  16308. PIPE_UNLIMITED_INSTANCES, 0, WIN32_PIPE_BUFSIZE, NMPWAIT_NOWAIT, NULL );
  16309. ConnectNamedPipe ( g_hPipe, NULL );
  16310. #endif
  16311. tzset();
  16312. Tracer::Init();
  16313. CSphString sError;
  16314. // initialize it before other code to fetch version string for banner
  16315. bool bColumnarError = !InitColumnar ( sError );
  16316. g_bSecondaryError = !InitSecondary ( g_sSecondaryError );
  16317. sphCollationInit ();
  16318. InitBanner();
  16319. if ( !g_bService )
  16320. fprintf ( stdout, "%s", g_sBanner.cstr() );
  16321. if ( bColumnarError )
  16322. sphWarning ( "Error initializing columnar storage: %s", sError.cstr() );
  16323. if ( g_bSecondaryError )
  16324. sphWarning ( "Error initializing secondary index: %s", g_sSecondaryError.cstr() );
  16325. if ( !sError.IsEmpty() )
  16326. sError = "";
  16327. //////////////////////
  16328. // parse command line
  16329. //////////////////////
  16330. CSphConfig conf;
  16331. bool bOptStop = false;
  16332. bool bOptStopWait = false;
  16333. bool bOptStatus = false;
  16334. bool bOptPIDFile = false;
  16335. StrVec_t dOptIndexes; // indexes explicitly pointed in cmdline options
  16336. int iOptPort = 0;
  16337. bool bOptPort = false;
  16338. CSphString sOptListen;
  16339. bool bOptListen = false;
  16340. bool bTestMode = false;
  16341. bool bOptDebugQlog = true;
  16342. bool bForcedPreread = false;
  16343. bool bNewCluster = false;
  16344. bool bNewClusterForce = false;
  16345. bool bForcePseudoSharding = false;
  16346. const char* szCmdConfigFile = nullptr;
  16347. DWORD uReplayFlags = 0;
  16348. #define OPT(_a1,_a2) else if ( !strcmp(argv[i],_a1) || !strcmp(argv[i],_a2) )
  16349. #define OPT1(_a1) else if ( !strcmp(argv[i],_a1) )
  16350. int i;
  16351. for ( i=1; i<argc; i++ )
  16352. {
  16353. // handle non-options
  16354. if ( argv[i][0]!='-' ) break;
  16355. // handle no-arg options
  16356. OPT ( "-h", "--help" ) { ShowHelp(); return 0; }
  16357. OPT ( "-?", "--?" ) { ShowHelp(); return 0; }
  16358. OPT ( "-v", "--version" ) { return 0; }
  16359. OPT1 ( "--console" ) { g_bOptNoLock = true; g_bOptNoDetach = true; bTestMode = true; }
  16360. OPT1 ( "--stop" ) bOptStop = true;
  16361. OPT1 ( "--stopwait" ) { bOptStop = true; bOptStopWait = true; }
  16362. OPT1 ( "--status" ) bOptStatus = true;
  16363. OPT1 ( "--pidfile" ) bOptPIDFile = true;
  16364. OPT1 ( "--iostats" ) g_bIOStats = true;
  16365. OPT1 ( "--cpustats" ) g_bCpuStats = true;
  16366. #if _WIN32
  16367. OPT1 ( "--install" ) { if ( !g_bService ) { ServiceInstall ( argc, argv ); return 0; } }
  16368. OPT1 ( "--delete" ) { if ( !g_bService ) { ServiceDelete (); return 0; } }
  16369. OPT1 ( "--ntservice" ) {} // it's valid but handled elsewhere
  16370. #else
  16371. OPT1 ( "--nodetach" ) g_bOptNoDetach = true;
  16372. #endif
  16373. OPT1 ( "--logdebug" ) g_eLogLevel = Max ( g_eLogLevel, SPH_LOG_DEBUG );
  16374. OPT1 ( "--logdebugv" ) g_eLogLevel = Max ( g_eLogLevel, SPH_LOG_VERBOSE_DEBUG );
  16375. OPT1 ( "--logdebugvv" ) g_eLogLevel = Max ( g_eLogLevel, SPH_LOG_VERY_VERBOSE_DEBUG );
  16376. OPT1 ( "--logreplication" ) g_eLogLevel = Max ( g_eLogLevel, SPH_LOG_RPL_DEBUG );
  16377. OPT1 ( "--safetrace" ) g_bSafeTrace = true;
  16378. OPT1 ( "--test" ) { g_bWatchdog = false; bTestMode = true; } // internal option, do NOT document
  16379. OPT1 ( "--test-thd-pool" ) { g_bWatchdog = false; bTestMode = true; } // internal option, do NOT document
  16380. OPT1 ( "--force-pseudo-sharding" ) { bForcePseudoSharding = true; } // internal option, do NOT document
  16381. OPT1 ( "--strip-path" ) g_bStripPath = true;
  16382. OPT1 ( "--vtune" ) g_bVtune = true;
  16383. OPT1 ( "--noqlog" ) bOptDebugQlog = false;
  16384. OPT1 ( "--force-preread" ) bForcedPreread = true;
  16385. OPT1 ( "--coredump" ) g_bCoreDump = true;
  16386. OPT1 ( "--new-cluster" ) bNewCluster = true;
  16387. OPT1 ( "--new-cluster-force" ) bNewClusterForce = true;
  16388. // FIXME! add opt=(csv)val handling here
  16389. OPT1 ( "--replay-flags=accept-desc-timestamp" ) uReplayFlags |= Binlog::REPLAY_ACCEPT_DESC_TIMESTAMP;
  16390. OPT1 ( "--replay-flags=ignore-open-errors" ) uReplayFlags |= Binlog::REPLAY_IGNORE_OPEN_ERROR;
  16391. OPT1 ( "--replay-flags=ignore-trx-errors" ) uReplayFlags |= Binlog::REPLAY_IGNORE_TRX_ERROR;
  16392. OPT1 ( "--replay-flags=ignore-all-errors" ) uReplayFlags |= Binlog::REPLAY_IGNORE_ALL_ERRORS;
  16393. // handle 1-arg options
  16394. else if ( (i+1)>=argc ) break;
  16395. OPT ( "-c", "--config" ) szCmdConfigFile = argv[++i];
  16396. OPT ( "-p", "--port" ) { bOptPort = true; iOptPort = atoi ( argv[++i] ); }
  16397. OPT ( "-l", "--listen" ) { bOptListen = true; sOptListen = argv[++i]; }
  16398. OPT ( "-i", "--index" ) dOptIndexes.Add ( argv[++i] );
  16399. #if _WIN32
  16400. OPT1 ( "--servicename" ) ++i; // it's valid but handled elsewhere
  16401. #endif
  16402. // handle unknown options
  16403. else
  16404. break;
  16405. }
  16406. if ( i!=argc )
  16407. sphFatal ( "malformed or unknown option near '%s'; use '-h' or '--help' to see available options.", argv[i] );
  16408. #if _WIN32
  16409. CheckWinInstall();
  16410. #endif
  16411. SetupLemmatizerBase();
  16412. g_sConfigFile = sphGetConfigFile ( szCmdConfigFile );
  16413. #if _WIN32
  16414. // init WSA on Windows
  16415. // we need to do it this early because otherwise gethostbyname() from config parser could fail
  16416. WSADATA tWSAData;
  16417. int iStartupErr = WSAStartup ( WINSOCK_VERSION, &tWSAData );
  16418. if ( iStartupErr )
  16419. sphFatal ( "failed to initialize WinSock2: %s", sphSockError ( iStartupErr ) );
  16420. if ( !LoadExFunctions () )
  16421. sphFatal ( "failed to initialize extended socket functions: %s", sphSockError ( iStartupErr ) );
  16422. // i want my windows sessions to log onto stdout
  16423. // both in Debug and Release builds
  16424. if ( !g_bService )
  16425. g_bOptNoDetach = true;
  16426. #ifndef NDEBUG
  16427. // i also want my windows debug builds to skip locking by default
  16428. // NOTE, this also skips log files!
  16429. g_bOptNoLock = true;
  16430. #endif
  16431. #endif
  16432. if ( !bOptPIDFile )
  16433. bOptPIDFile = !g_bOptNoLock;
  16434. // check port and listen arguments early
  16435. if ( !g_bOptNoDetach && ( bOptPort || bOptListen ) )
  16436. {
  16437. sphWarning ( "--listen and --port are only allowed in --console debug mode; switch ignored" );
  16438. bOptPort = bOptListen = false;
  16439. }
  16440. if ( bOptPort )
  16441. {
  16442. if ( bOptListen )
  16443. sphFatal ( "please specify either --port or --listen, not both" );
  16444. CheckPort ( iOptPort );
  16445. }
  16446. /////////////////////
  16447. // parse config file
  16448. /////////////////////
  16449. LoadAndCheckConfig ();
  16450. sphInfo( "using config file '%s' (%d chars)...", g_sConfigFile.cstr(), g_dConfig.GetLength());
  16451. // do parse
  16452. // don't aqcuire wlock, since we're in single main thread here.
  16453. FakeScopedWLock_T<> wFakeLock { g_tRotateConfigMutex };
  16454. if ( !ParseConfig ( &g_hCfg, g_sConfigFile.scstr(), g_dConfig.begin() ) )
  16455. sphFatal ( "failed to parse config file '%s': %s", g_sConfigFile.cstr (), TlsMsg::szError() );
  16456. CleanLoadedConfig();
  16457. const CSphConfig& hConf = g_hCfg;
  16458. if ( !hConf.Exists ( "searchd" ) || !hConf["searchd"].Exists ( "searchd" ) )
  16459. sphFatal ( "'searchd' config section not found in '%s'", g_sConfigFile.cstr () );
  16460. const CSphConfigSection & hSearchdpre = hConf["searchd"]["searchd"];
  16461. if ( !sphInitCharsetAliasTable ( sError ) )
  16462. sphFatal ( "failed to init charset alias table: %s", sError.cstr() );
  16463. ////////////////////////
  16464. // stop running searchd
  16465. ////////////////////////
  16466. if ( bOptStop )
  16467. {
  16468. StopOrStopWaitAnother ( hSearchdpre ( "pid_file" ), bOptStopWait );
  16469. assert ( 0 && "StopOrStopWaitAnother should not return " );
  16470. exit ( 0 );
  16471. }
  16472. ////////////////////////////////
  16473. // query running searchd status
  16474. ////////////////////////////////
  16475. if ( bOptStatus )
  16476. {
  16477. QueryStatus ( hSearchdpre("listen") );
  16478. exit ( 0 );
  16479. }
  16480. /////////////////////
  16481. // configure searchd
  16482. /////////////////////
  16483. sphInitCJson();
  16484. if ( !LoadConfigInt ( hConf, g_sConfigFile, sError ) )
  16485. sphFatal ( "%s", sError.cstr() );
  16486. ConfigureSearchd ( hConf, bOptPIDFile, bTestMode );
  16487. sphConfigureCommon ( hConf ); // this also inits plugins now
  16488. g_bWatchdog = hSearchdpre.GetInt ( "watchdog", g_bWatchdog )!=0;
  16489. if ( g_iMaxPacketSize<128*1024 || g_iMaxPacketSize>128*1024*1024 )
  16490. sphFatal ( "max_packet_size out of bounds (128K..128M)" );
  16491. if ( g_iMaxFilters<1 || g_iMaxFilters>10240 )
  16492. sphFatal ( "max_filters out of bounds (1..10240)" );
  16493. if ( g_iMaxFilterValues<1 || g_iMaxFilterValues>10485760 )
  16494. sphFatal ( "max_filter_values out of bounds (1..10485760)" );
  16495. bool bVisualLoad = true;
  16496. bool bWatched = false;
  16497. #if !_WIN32
  16498. // Let us start watchdog right now, on foreground first.
  16499. int iDevNull = open ( "/dev/null", O_RDWR );
  16500. if ( g_bWatchdog && !g_bOptNoDetach )
  16501. {
  16502. bWatched = true;
  16503. if ( !g_bOptNoLock )
  16504. OpenDaemonLog ( hConf["searchd"]["searchd"] );
  16505. bVisualLoad = SetWatchDog ( iDevNull );
  16506. OpenDaemonLog ( hConf["searchd"]["searchd"], true ); // just the 'IT Happens' magic - switch off, then on.
  16507. }
  16508. #endif
  16509. // here we either since plain startup, either being resurrected (forked) by watchdog.
  16510. // create the pid
  16511. if ( bOptPIDFile )
  16512. {
  16513. g_sPidFile = FixupFilename ( hSearchdpre["pid_file"].cstr() );
  16514. g_iPidFD = ::open ( g_sPidFile.scstr(), O_CREAT | O_WRONLY, S_IREAD | S_IWRITE );
  16515. if ( g_iPidFD<0 )
  16516. sphFatal ( "failed to create pid file '%s': %s", g_sPidFile.scstr(), strerrorm(errno) );
  16517. }
  16518. if ( bOptPIDFile && !sphLockEx ( g_iPidFD, false ) )
  16519. sphFatal ( "failed to lock pid file '%s': %s (searchd already running?)", g_sPidFile.scstr(), strerrorm(errno) );
  16520. g_bPidIsMine = true;
  16521. // Actions on resurrection
  16522. if ( bWatched && !bVisualLoad && LoadAndCheckConfig () )
  16523. {
  16524. // reparse the config file
  16525. sphInfo ( "Reloading the config (%d chars)", g_dConfig.GetLength() );
  16526. // fake lock is acquired; no warnings will be fired
  16527. if ( !ParseConfig ( &g_hCfg, g_sConfigFile.cstr (), g_dConfig.begin () ) )
  16528. sphFatal ( "failed to parse config file '%s': %s", g_sConfigFile.cstr (), TlsMsg::szError() );
  16529. sphInfo ( "Reconfigure the daemon" );
  16530. ConfigureSearchd ( hConf, bOptPIDFile, bTestMode );
  16531. }
  16532. CleanLoadedConfig();
  16533. // hSearchdpre might be dead if we reloaded the config.
  16534. CSphConfigSection & hSearchd = hConf["searchd"]["searchd"];
  16535. // handle my signals
  16536. SetSignalHandlers ( g_bOptNoDetach );
  16537. // create logs
  16538. //if ( !g_bOptNoLock )
  16539. {
  16540. // create log
  16541. OpenDaemonLog ( hSearchd, true );
  16542. // create query log if required
  16543. if ( hSearchd.Exists ( "query_log" ) )
  16544. {
  16545. CSphString sQueryLog = hSearchd["query_log"].cstr();
  16546. if ( sQueryLog=="syslog" )
  16547. g_bQuerySyslog = true;
  16548. else
  16549. {
  16550. #if _WIN32
  16551. sQueryLog = AppendWinInstallDir(sQueryLog);
  16552. #endif
  16553. g_iQueryLogFile = open ( sQueryLog.cstr(), O_CREAT | O_RDWR | O_APPEND, S_IREAD | S_IWRITE );
  16554. if ( g_iQueryLogFile<0 )
  16555. sphFatal ( "failed to open query log file '%s': %s", sQueryLog.cstr(), strerrorm(errno) );
  16556. LogChangeMode ( g_iQueryLogFile, g_iLogFileMode );
  16557. }
  16558. g_sQueryLogFile = sQueryLog.cstr();
  16559. }
  16560. }
  16561. #if !_WIN32
  16562. if ( !g_bOptNoDetach && !bWatched )
  16563. {
  16564. switch ( fork () )
  16565. {
  16566. case -1:
  16567. // error
  16568. sphFatalLog ( "fork() failed (reason: %s)", strerrorm ( errno ) );
  16569. exit ( 1 );
  16570. case 0:
  16571. // daemonized child
  16572. break;
  16573. default:
  16574. // tty-controlled parent
  16575. exit ( 0 );
  16576. }
  16577. }
  16578. #endif
  16579. // init before workpool, as last checks binlog
  16580. ModifyDaemonPaths ( hSearchd );
  16581. sphRTInit ( hSearchd, bTestMode, hConf("common") ? hConf["common"]("common") : nullptr );
  16582. // after next line executed we're in mt env, need to take rwlock accessing config.
  16583. StartGlobalWorkPool ();
  16584. // since that moment any 'fatal' will assume calling 'shutdown' function.
  16585. sphSetDieCallback ( DieOrFatalWithShutdownCb );
  16586. sphInfo( "starting daemon version '%s' ...", g_sStatusVersion.cstr() );
  16587. ////////////////////
  16588. // network startup
  16589. ////////////////////
  16590. CSphVector<ListenerDesc_t> dListenerDescs;
  16591. // command line arguments override config (but only in --console)
  16592. if ( bOptListen )
  16593. {
  16594. auto tDesc = ParseListener ( sOptListen.cstr() );
  16595. dListenerDescs.Add ( tDesc );
  16596. AddGlobalListener ( tDesc );
  16597. } else if ( bOptPort )
  16598. {
  16599. AddGlobalListener ( MakeAnyListener ( iOptPort ) );
  16600. } else
  16601. {
  16602. // listen directives in configuration file
  16603. for ( CSphVariant * v = hSearchd("listen"); v; v = v->m_pNext )
  16604. {
  16605. auto tDesc = ParseListener ( v->cstr () );
  16606. dListenerDescs.Add ( tDesc );
  16607. AddGlobalListener ( tDesc );
  16608. }
  16609. // default is to listen on our two ports
  16610. if ( g_dListeners.IsEmpty() )
  16611. {
  16612. AddGlobalListener ( MakeLocalhostListener ( SPHINXAPI_PORT, Proto_e::SPHINX ) );
  16613. AddGlobalListener ( MakeLocalhostListener ( SPHINXQL_PORT, Proto_e::MYSQL41 ) );
  16614. }
  16615. }
  16616. if ( !ValidateListenerRanges ( dListenerDescs, sError ) )
  16617. sphFatal ( "%s", sError.cstr() );
  16618. SetServerSSLKeys ( hSearchd ( "ssl_cert" ), hSearchd ( "ssl_key" ), hSearchd ( "ssl_ca" ) );
  16619. CheckSSL();
  16620. // set up ping service (if necessary) before loading indexes
  16621. // (since loading ha-mirrors of distributed already assumes ping is usable).
  16622. if ( g_iPingIntervalUs>0 )
  16623. Ping::Start();
  16624. ScheduleMallocTrim();
  16625. // initialize timeouts since hook will use them
  16626. auto iRtFlushPeriodUs = hSearchd.GetUsTime64S ( "rt_flush_period", 36000000000ll ); // 10h
  16627. SetRtFlushPeriod ( Max ( iRtFlushPeriodUs, 3 * 1000000 ) ); // min 3S
  16628. g_pLocalIndexes->SetAddOrReplaceHook ( HookSubscribeMutableFlush );
  16629. //////////////////////
  16630. // build indexes hash
  16631. //////////////////////
  16632. // configure and preload
  16633. if ( bTestMode ) // pass this flag here prior to index config
  16634. sphRTSetTestMode();
  16635. if ( bForcePseudoSharding )
  16636. SetPseudoShardingThresh(0);
  16637. StrVec_t dExactIndexes;
  16638. for ( const auto &dOptIndex : dOptIndexes )
  16639. sphSplit ( dExactIndexes, dOptIndex.cstr (), "," );
  16640. SetPercolateQueryParserFactory ( PercolateQueryParserFactory );
  16641. Threads::CallCoroutine ( [&hConf, &dExactIndexes]() REQUIRES_SHARED ( g_tRotateConfigMutex )
  16642. {
  16643. ScopedRole_c thMain ( MainThread );
  16644. ConfigureAndPreloadOnStartup ( hConf, dExactIndexes );
  16645. } );
  16646. ///////////
  16647. // startup
  16648. ///////////
  16649. DetermineNodeItemStackSize();
  16650. DetermineFilterItemStackSize();
  16651. // ModifyDaemonPaths ( hSearchd );
  16652. // sphRTInit ( hSearchd, bTestMode, hConf("common") ? hConf["common"]("common") : nullptr );
  16653. if ( hSearchd.Exists ( "snippets_file_prefix" ) )
  16654. g_sSnippetsFilePrefix = hSearchd["snippets_file_prefix"].cstr();
  16655. else
  16656. g_sSnippetsFilePrefix.SetSprintf("%s/", sphGetCwd().scstr());
  16657. {
  16658. auto sLogFormat = hSearchd.GetStr ( "query_log_format", "sphinxql" );
  16659. if ( sLogFormat=="sphinxql" )
  16660. g_eLogFormat = LOG_FORMAT_SPHINXQL;
  16661. else if ( sLogFormat=="plain" )
  16662. {
  16663. StrVec_t dParams;
  16664. sphSplit ( dParams, sLogFormat.cstr() );
  16665. for ( const auto& sParam : dParams )
  16666. {
  16667. if ( sParam=="sphinxql" )
  16668. g_eLogFormat = LOG_FORMAT_SPHINXQL;
  16669. else if ( sParam=="plain" )
  16670. g_eLogFormat = LOG_FORMAT_PLAIN;
  16671. else if ( sParam=="compact_in" )
  16672. g_bLogCompactIn = true;
  16673. }
  16674. }
  16675. }
  16676. if ( g_bLogCompactIn && g_eLogFormat==LOG_FORMAT_PLAIN )
  16677. sphWarning ( "compact_in option only supported with query_log_format=sphinxql" );
  16678. // prepare to detach
  16679. if ( !g_bOptNoDetach )
  16680. {
  16681. ReleaseTTYFlag();
  16682. #if !_WIN32
  16683. if ( !bWatched || bVisualLoad )
  16684. {
  16685. close ( STDIN_FILENO );
  16686. close ( STDOUT_FILENO );
  16687. close ( STDERR_FILENO );
  16688. dup2 ( iDevNull, STDIN_FILENO );
  16689. dup2 ( iDevNull, STDOUT_FILENO );
  16690. dup2 ( iDevNull, STDERR_FILENO );
  16691. }
  16692. #endif
  16693. }
  16694. if ( bOptPIDFile && !bWatched )
  16695. sphLockUn ( g_iPidFD );
  16696. Binlog::Configure ( hSearchd, bTestMode, uReplayFlags );
  16697. SetUidShort ( bTestMode );
  16698. InitDocstore ( g_iDocstoreCache );
  16699. InitSkipCache ( g_iSkipCache );
  16700. InitParserOption();
  16701. if ( bOptPIDFile )
  16702. {
  16703. #if !_WIN32
  16704. // re-lock pid
  16705. // FIXME! there's a potential race here
  16706. if ( !sphLockEx ( g_iPidFD, true ) )
  16707. sphFatal ( "failed to re-lock pid file '%s': %s", g_sPidFile.scstr(), strerrorm(errno) );
  16708. #endif
  16709. char sPid[16];
  16710. snprintf ( sPid, sizeof(sPid), "%d\n", (int)getpid() );
  16711. auto iPidLen = (int) strlen(sPid);
  16712. sphSeek ( g_iPidFD, 0, SEEK_SET );
  16713. if ( !sphWrite ( g_iPidFD, sPid, iPidLen ) )
  16714. sphFatal ( "failed to write to pid file '%s' (errno=%d, msg=%s)", g_sPidFile.scstr(),
  16715. errno, strerrorm(errno) );
  16716. if ( ::ftruncate ( g_iPidFD, iPidLen ) )
  16717. sphFatal ( "failed to truncate pid file '%s' (errno=%d, msg=%s)", g_sPidFile.scstr(),
  16718. errno, strerrorm(errno) );
  16719. }
  16720. #if _WIN32
  16721. SetConsoleCtrlHandler ( CtrlHandler, TRUE );
  16722. #endif
  16723. Threads::CallCoroutine( [bWatched] {
  16724. StrVec_t dFailed;
  16725. if ( !g_bOptNoDetach && !bWatched && !g_bService )
  16726. {
  16727. // re-lock indexes
  16728. ServedSnap_t hLocal = g_pLocalIndexes->GetHash();
  16729. for ( const auto& tIt : *hLocal )
  16730. {
  16731. sphLogDebug ( "Relocking %s", tIt.first.cstr () );
  16732. auto pServed = tIt.second;
  16733. // obtain exclusive lock
  16734. if ( !pServed )
  16735. dFailed.Add ( tIt.first );
  16736. RWIdx_c pIdx { pServed };
  16737. if ( !pIdx->Lock() )
  16738. {
  16739. sphWarning ( "index '%s': lock: %s; INDEX UNUSABLE", tIt.first.cstr(), pIdx->GetLastError().cstr() );
  16740. dFailed.Add ( tIt.first );
  16741. }
  16742. }
  16743. for ( const auto& sFailed : dFailed )
  16744. g_pLocalIndexes->Delete ( sFailed );
  16745. }
  16746. });
  16747. // if we're running in test console mode, dump queries to tty as well
  16748. // unless we're explicitly asked not to!
  16749. if ( hSearchd ( "query_log" ) && g_bOptNoLock && g_bOptNoDetach && bOptDebugQlog )
  16750. {
  16751. g_bQuerySyslog = false;
  16752. g_bLogSyslog = false;
  16753. g_iQueryLogFile = g_iLogFile;
  16754. }
  16755. #if USE_SYSLOG
  16756. if ( g_bLogSyslog || g_bQuerySyslog )
  16757. {
  16758. openlog ( "searchd", LOG_PID, LOG_DAEMON );
  16759. }
  16760. #else
  16761. if ( g_bQuerySyslog )
  16762. sphFatal ( "Wrong query_log file! You have to reconfigure --with-syslog and rebuild daemon if you want to use syslog there." );
  16763. #endif
  16764. /////////////////
  16765. // serve clients
  16766. /////////////////
  16767. #if _WIN32
  16768. if ( g_bService )
  16769. MySetServiceStatus ( SERVICE_RUNNING, NO_ERROR, 0 );
  16770. #endif
  16771. // replay last binlog
  16772. Threads::CallCoroutine ([]
  16773. {
  16774. auto _ = PublishSystemInfo ("replay binlog");
  16775. SmallStringHash_T<CSphIndex*> hIndexes;
  16776. ServedSnap_t hLocals = g_pLocalIndexes->GetHash();
  16777. for ( auto& tIt : *hLocals )
  16778. {
  16779. if ( tIt.second )
  16780. hIndexes.Add ( RWIdx_c ( tIt.second ), tIt.first );
  16781. }
  16782. Binlog::Replay ( hIndexes, DumpMemStat );
  16783. } );
  16784. // no need to create another cluster on restart by watchdog resurrection
  16785. if ( bWatched && !bVisualLoad )
  16786. {
  16787. bNewCluster = false;
  16788. bNewClusterForce = false;
  16789. }
  16790. StartRtBinlogFlushing();
  16791. ScheduleFlushAttrs();
  16792. gStats().m_uStarted = (DWORD)time(NULL);
  16793. {
  16794. CSphString sSQLStateDefault;
  16795. if ( IsConfigless() )
  16796. sSQLStateDefault.SetSprintf ( "%s/state.sql", GetDataDirInt().cstr() );
  16797. if ( !InitSphinxqlState ( hSearchd.GetStr ( "sphinxql_state", sSQLStateDefault.scstr() ), sError ))
  16798. sphWarning ( "sphinxql_state flush disabled: %s", sError.cstr ());
  16799. }
  16800. ServeUserVars ();
  16801. ServeAutoOptimize();
  16802. PrereadIndexes ( bForcedPreread );
  16803. // almost ready, time to start listening
  16804. g_iBacklog = hSearchd.GetInt ( "listen_backlog", g_iBacklog );
  16805. for ( const auto& dListener : g_dListeners )
  16806. if ( listen ( dListener.m_iSock, g_iBacklog )==-1 )
  16807. {
  16808. if ( sphSockGetErrno()==EADDRINUSE )
  16809. sphFatal ( "listen() failed with EADDRINUSE. A listener with other UID on same address:port?");
  16810. else
  16811. sphFatal ( "listen() failed: %s", sphSockError () );
  16812. }
  16813. // net thread needs non-blocking sockets
  16814. for ( const auto& dListener : g_dListeners )
  16815. {
  16816. if ( sphSetSockNB ( dListener.m_iSock )<0 )
  16817. {
  16818. sphWarning ( "sphSetSockNB() failed: %s", sphSockError() );
  16819. sphSockClose ( dListener.m_iSock );
  16820. }
  16821. if ( ( g_iTFO!=TFO_ABSENT ) && ( g_iTFO & TFO_LISTEN ) )
  16822. sphSetSockTFO ( dListener.m_iSock );
  16823. }
  16824. g_pTickPoolThread = Threads::MakeThreadPool ( g_iNetWorkers, "TickPool" );
  16825. WipeSchedulerOnFork ( g_pTickPoolThread );
  16826. g_dNetLoops.Resize ( g_iNetWorkers );
  16827. for ( auto & pNetLoop : g_dNetLoops )
  16828. {
  16829. pNetLoop = new CSphNetLoop;
  16830. pNetLoop->SetListeners ( g_dListeners );
  16831. if ( !GetAvailableNetLoop() )
  16832. SetAvailableNetLoop ( pNetLoop );
  16833. g_pTickPoolThread->Schedule ( [pNetLoop] { ScopedRole_c thPoll ( NetPoollingThread ); pNetLoop->LoopNetPoll (); }, false );
  16834. }
  16835. // until no threads started, schedule stopping of alone threads to very bottom
  16836. WipeGlobalSchedulerOnShutdownAndFork();
  16837. Detached::MakeAloneIteratorAvailable ();
  16838. // time for replication to sync with cluster
  16839. searchd::AddShutdownCb ( ReplicateClustersDelete );
  16840. ReplicationStart ( std::move ( dListenerDescs ), bNewCluster, bNewClusterForce );
  16841. g_bJsonConfigLoadedOk = true;
  16842. // ready, steady, go
  16843. sphInfo ( "accepting connections" );
  16844. // disable startup logging to stdout
  16845. if ( !g_bOptNoDetach )
  16846. g_bLogStdout = false;
  16847. while (true)
  16848. {
  16849. CrashLogger::SetupTimePID();
  16850. TickHead();
  16851. }
  16852. } // NOLINT ServiceMain() function length
  16853. inline int mainimpl ( int argc, char **argv )
  16854. {
  16855. // threads should be initialized before memory allocations
  16856. char cTopOfMainStack;
  16857. Threads::Init();
  16858. PrepareMainThread ( &cTopOfMainStack );
  16859. sphSetDieCallback ( DieOrFatalCb );
  16860. g_pLogger() = sphLog;
  16861. sphBacktraceSetBinaryName ( argv[0] );
  16862. GeodistInit();
  16863. #if _WIN32
  16864. int iNameIndex = -1;
  16865. for ( int i=1; i<argc; i++ )
  16866. {
  16867. if ( strcmp ( argv[i], "--ntservice" )==0 )
  16868. g_bService = true;
  16869. if ( strcmp ( argv[i], "--servicename" )==0 && (i+1)<argc )
  16870. {
  16871. iNameIndex = i+1;
  16872. g_sServiceName = argv[iNameIndex];
  16873. }
  16874. }
  16875. if ( g_bService )
  16876. {
  16877. for ( int i=0; i<argc; i++ )
  16878. g_dArgs.Add ( argv[i] );
  16879. if ( iNameIndex>=0 )
  16880. g_sServiceName = g_dArgs[iNameIndex].cstr ();
  16881. SERVICE_TABLE_ENTRY dDispatcherTable[] =
  16882. {
  16883. { (LPSTR) g_sServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain },
  16884. { NULL, NULL }
  16885. };
  16886. if ( !StartServiceCtrlDispatcher ( dDispatcherTable ) )
  16887. sphFatal ( "StartServiceCtrlDispatcher() failed: %s", WinErrorInfo() );
  16888. return 0;
  16889. } else
  16890. #endif
  16891. return ServiceMain ( argc, argv );
  16892. }
  16893. #ifndef SUPRESS_SEARCHD_MAIN
  16894. int main ( int argc, char ** argv )
  16895. {
  16896. return mainimpl ( argc, argv );
  16897. }
  16898. #endif
  16899. volatile bool& sphGetGotSighup() noexcept
  16900. {
  16901. static bool bGotSighup = false;
  16902. return bGotSighup;
  16903. }
  16904. volatile bool& sphGetGotSigusr1() noexcept
  16905. {
  16906. static bool bGotSigusr1 = false;
  16907. return bGotSigusr1;
  16908. }
  16909. volatile bool & sphGetGotSigusr2 () noexcept
  16910. {
  16911. static bool bGotSigusr2 = false;
  16912. return bGotSigusr2;
  16913. }