workunit.cpp 474 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include <string>
  14. #include <unordered_set>
  15. #include "jlib.hpp"
  16. #include "workunit.hpp"
  17. #include "jprop.hpp"
  18. #include "jmisc.hpp"
  19. #include "jexcept.hpp"
  20. #include "jiter.ipp"
  21. #include "jptree.hpp"
  22. #include "jtime.ipp"
  23. #include "jencrypt.hpp"
  24. #include "junicode.hpp"
  25. #include "jlzw.hpp"
  26. #include "jregexp.hpp"
  27. #include "eclrtl.hpp"
  28. #include "deftype.hpp"
  29. #include <time.h>
  30. #include "mpbase.hpp"
  31. #include "daclient.hpp"
  32. #include "dadfs.hpp"
  33. #include "dafdesc.hpp"
  34. #include "dasds.hpp"
  35. #include "danqs.hpp"
  36. #include "dautils.hpp"
  37. #include "dllserver.hpp"
  38. #include "thorplugin.hpp"
  39. #include "thorhelper.hpp"
  40. #include "workflow.hpp"
  41. #include "nbcd.hpp"
  42. #include "seclib.hpp"
  43. #include "wuerror.hpp"
  44. #include "wujobq.hpp"
  45. #ifndef _CONTAINERIZED
  46. #include "environment.hpp"
  47. #endif
  48. #include "daqueue.hpp"
  49. #include "workunit.ipp"
  50. #include "digisign.hpp"
  51. #include <list>
  52. #include <string>
  53. #include <algorithm>
  54. using namespace cryptohelper;
  55. static int workUnitTraceLevel = 1;
  56. static StringBuffer &getXPath(StringBuffer &wuRoot, const char *wuid)
  57. {
  58. // MORE - can fold in the date
  59. return wuRoot.append("/WorkUnits/").append(wuid);
  60. }
  61. //To be called by eclserver, but esp etc. won't know, so we need to store it.
  62. static StringBuffer & appendLibrarySuffix(StringBuffer & suffix)
  63. {
  64. #ifdef _WIN32
  65. suffix.append("W");
  66. #else
  67. suffix.append("L");
  68. #endif
  69. #ifdef __64BIT__
  70. suffix.append("64");
  71. #else
  72. suffix.append("32");
  73. #endif
  74. return suffix;
  75. }
  76. typedef MapStringTo<bool> UniqueScopes;
  77. static void wuAccessError(const char *username, const char *action, const char *wuscope, const char *wuid, bool excpt, bool log)
  78. {
  79. StringBuffer err;
  80. err.append("Workunit Access Denied - action: ").append(action).append(" user:").append(username ? username : "<Unknown>");
  81. if (wuid)
  82. err.append(" workunit:").append(wuid);
  83. if (wuscope)
  84. err.append(" scope:").append(wuscope);
  85. //MORE - we would need more information passed in from outside if we want to make the audit message format the same as from higher level ESP calls
  86. SYSLOG(AUDIT_TYPE_ACCESS_FAILURE, err.str());
  87. if (log)
  88. LOG(MCuserError, "%s", err.str());
  89. if (excpt)
  90. throw MakeStringException(WUERR_AccessError, "%s", err.str());
  91. }
  92. static bool checkWuScopeSecAccess(const char *wuscope, ISecManager *secmgr, ISecUser *secuser, int required, const char *action, bool excpt, bool log)
  93. {
  94. if (!secmgr || !secuser)
  95. return true;
  96. bool ret = secmgr->authorizeEx(RT_WORKUNIT_SCOPE, *secuser, wuscope)>=required;
  97. if (!ret && (log || excpt))
  98. wuAccessError(secuser->getName(), action, wuscope, NULL, excpt, log);
  99. return ret;
  100. }
  101. static bool checkWuScopeListSecAccess(const char *wuscope, ISecResourceList *scopes, int required, const char *action, bool excpt, bool log)
  102. {
  103. if (!scopes)
  104. return true;
  105. bool ret=true;
  106. if (wuscope)
  107. {
  108. Owned<ISecResource> res=scopes->getResource(wuscope);
  109. if (!res || res->getAccessFlags()<required)
  110. ret=false;
  111. }
  112. else
  113. {
  114. for (int seq=0; ret && seq<scopes->count(); seq++)
  115. {
  116. ISecResource *res=scopes->queryResource(seq);
  117. if (res && res->getAccessFlags()<required)
  118. return false;
  119. }
  120. }
  121. if (!ret && (log || excpt))
  122. wuAccessError(NULL, action, wuscope, NULL, excpt, log);
  123. return ret;
  124. }
  125. static bool checkWuSecAccess(IConstWorkUnit &cw, ISecManager *secmgr, ISecUser *secuser, int required, const char *action, bool excpt, bool log)
  126. {
  127. if (!secmgr || !secuser)
  128. return true;
  129. bool ret=secmgr->authorizeEx(RT_WORKUNIT_SCOPE, *secuser, cw.queryWuScope())>=required;
  130. if (!ret && (log || excpt))
  131. {
  132. wuAccessError(secuser->getName(), action, cw.queryWuScope(), cw.queryWuid(), excpt, log);
  133. }
  134. return ret;
  135. }
  136. static bool checkWuSecAccess(const char *wuid, ISecManager *secmgr, ISecUser *secuser, int required, const char *action, bool excpt, bool log)
  137. {
  138. if (!secmgr || !secuser)
  139. return true;
  140. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  141. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid);
  142. bool ret=secmgr->authorizeEx(RT_WORKUNIT_SCOPE, *secuser, cw->queryWuScope())>=required;
  143. if (!ret && (log || excpt))
  144. {
  145. wuAccessError(secuser->getName(), action, cw->queryWuScope(), cw->queryWuid(), excpt, log);
  146. }
  147. return ret;
  148. }
  149. void doDescheduleWorkkunit(char const * wuid)
  150. {
  151. StringBuffer xpath;
  152. xpath.append("*/*/*/");
  153. ncnameEscape(wuid, xpath);
  154. Owned<IRemoteConnection> conn = querySDS().connect("/Schedule", myProcessSession(), RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  155. if(!conn) return;
  156. Owned<IPropertyTree> root = conn->getRoot();
  157. bool more;
  158. do more = root->removeProp(xpath.str()); while(more);
  159. }
  160. //======================================================
  161. /*
  162. * Graph progress support
  163. */
  164. CWuGraphStats::CWuGraphStats(StatisticCreatorType _creatorType, const char * _creator, unsigned wfid, const char * _rootScope, unsigned _id, bool _merge)
  165. : creatorType(_creatorType), creator(_creator), id(_id), merge(_merge)
  166. {
  167. StatsScopeId graphScopeId;
  168. verifyex(graphScopeId.setScopeText(_rootScope));
  169. StatsScopeId rootScopeId(SSTworkflow,wfid);
  170. collector.setown(createStatisticsGatherer(_creatorType, _creator, rootScopeId));
  171. collector->beginScope(graphScopeId);
  172. }
  173. void CWuGraphStats::beforeDispose()
  174. {
  175. collector->endScope();
  176. StringBuffer tag;
  177. tag.append("sg").append(id);
  178. IPropertyTree &progress = queryProgressTree();
  179. if (merge && progress.hasProp(tag))
  180. {
  181. VStringBuffer statsPath("%s/Stats", tag.str());
  182. MemoryBuffer compressed;
  183. progress.getPropBin(statsPath, compressed);
  184. if (compressed.length())
  185. {
  186. MemoryBuffer serialized;
  187. decompressToBuffer(serialized, compressed);
  188. Owned<IStatisticCollection> prevCollection = createStatisticCollection(serialized);
  189. prevCollection->mergeInto(*collector);
  190. }
  191. }
  192. Owned<IStatisticCollection> stats = collector->getResult();
  193. MemoryBuffer compressed;
  194. {
  195. MemoryBuffer serialized;
  196. serializeStatisticCollection(serialized, stats);
  197. compressToBuffer(compressed, serialized.length(), serialized.toByteArray());
  198. }
  199. unsigned minActivity = 0;
  200. unsigned maxActivity = 0;
  201. stats->getMinMaxActivity(minActivity, maxActivity);
  202. //Replace the particular subgraph statistics added by this creator
  203. IPropertyTree * subgraph = progress.setPropTree(tag);
  204. subgraph->setProp("@c", queryCreatorTypeName(creatorType));
  205. subgraph->setProp("@creator", creator);
  206. subgraph->setPropInt("@minActivity", minActivity);
  207. subgraph->setPropInt("@maxActivity", maxActivity);
  208. subgraph->setPropBin("Stats", compressed.length(), compressed.toByteArray());
  209. if (!progress.getPropBool("@stats", false))
  210. progress.setPropBool("@stats", true);
  211. }
  212. IStatisticGatherer & CWuGraphStats::queryStatsBuilder()
  213. {
  214. return *collector;
  215. }
  216. class CConstGraphProgress : public CInterface, implements IConstWUGraphProgress
  217. {
  218. public:
  219. IMPLEMENT_IINTERFACE;
  220. CConstGraphProgress(const char *_wuid, const char *_graphName, IPropertyTree *_progress) : wuid(_wuid), graphName(_graphName), progress(_progress)
  221. {
  222. if (!progress)
  223. progress.setown(createPTree());
  224. formatVersion = progress->getPropInt("@format", PROGRESS_FORMAT_V);
  225. }
  226. virtual IPropertyTree * getProgressTree(bool doFormat) override
  227. {
  228. if (progress->getPropBool("@stats"))
  229. return createProcessTreeFromStats(doFormat); // Should we cache that?
  230. return LINK(progress);
  231. }
  232. virtual unsigned queryFormatVersion()
  233. {
  234. return formatVersion;
  235. }
  236. protected:
  237. CConstGraphProgress(const char *_wuid, const char *_graphName) : wuid(_wuid), graphName(_graphName)
  238. {
  239. formatVersion = PROGRESS_FORMAT_V;
  240. }
  241. static void expandStats(IPropertyTree * target, IStatisticCollection & collection, bool doFormat)
  242. {
  243. StringBuffer formattedValue;
  244. unsigned numStats = collection.getNumStatistics();
  245. for (unsigned i=0; i < numStats; i++)
  246. {
  247. StatisticKind kind;
  248. unsigned __int64 value;
  249. collection.getStatistic(kind, value, i);
  250. if (doFormat)
  251. {
  252. formatStatistic(formattedValue.clear(), value, kind);
  253. target->setProp(queryTreeTag(kind), formattedValue);
  254. }
  255. else
  256. {
  257. target->setPropInt64(queryTreeTag(kind), value);
  258. }
  259. }
  260. }
  261. void expandProcessTreeFromStats(IPropertyTree * rootTarget, IPropertyTree * target, IStatisticCollection * collection, bool doFormat)
  262. {
  263. expandStats(target, *collection, doFormat);
  264. StringBuffer scopeName;
  265. Owned<IStatisticCollectionIterator> activityIter = &collection->getScopes(NULL, false);
  266. ForEach(*activityIter)
  267. {
  268. IStatisticCollection & cur = activityIter->query();
  269. cur.getScope(scopeName.clear());
  270. const char * id = scopeName.str();
  271. const char * tag;
  272. IPropertyTree * curTarget = target;
  273. switch (cur.queryScopeType())
  274. {
  275. case SSTedge:
  276. tag = "edge";
  277. id += strlen(EdgeScopePrefix);
  278. break;
  279. case SSTactivity:
  280. tag = "node";
  281. id += strlen(ActivityScopePrefix);
  282. break;
  283. case SSTsubgraph:
  284. //All subgraphs are added a root elements in the progress tree
  285. curTarget = rootTarget;
  286. tag = "node";
  287. id += strlen(SubGraphScopePrefix);
  288. break;
  289. case SSTchildgraph:
  290. case SSTworkflow:
  291. case SSTgraph:
  292. // SSTworkflow and SSTgraph may be safely ignored. They are not required to produce the statistics.
  293. expandProcessTreeFromStats(rootTarget, target, &cur, doFormat);
  294. continue;
  295. case SSTfunction:
  296. case SSTchannel:
  297. case SSTglobal:
  298. //MORE:Should function scopes be included in the graph scope somehow, and if so how?
  299. continue;
  300. default:
  301. throwUnexpected();
  302. }
  303. IPropertyTree * next = curTarget->addPropTree(tag);
  304. next->setProp("@id", id);
  305. expandProcessTreeFromStats(rootTarget, next, &cur, doFormat);
  306. }
  307. }
  308. IPropertyTree * createProcessTreeFromStats(bool doFormat)
  309. {
  310. MemoryBuffer compressed;
  311. MemoryBuffer serialized;
  312. Owned<IPropertyTree> progressTree = createPTree();
  313. Owned<IPropertyTreeIterator> iter = progress->getElements("sg*");
  314. ForEach(*iter)
  315. {
  316. IPropertyTree & curSubGraph = iter->query();
  317. curSubGraph.getPropBin("Stats", compressed.clear());
  318. //Protect against updates that delete the stats while we are iterating
  319. if (compressed.length())
  320. {
  321. decompressToBuffer(serialized.clear(), compressed);
  322. Owned<IStatisticCollection> collection = createStatisticCollection(serialized);
  323. expandProcessTreeFromStats(progressTree, progressTree, collection, doFormat);
  324. }
  325. }
  326. return progressTree.getClear();
  327. }
  328. protected:
  329. Linked<IPropertyTree> progress;
  330. StringAttr wuid, graphName;
  331. unsigned formatVersion;
  332. };
  333. extern WORKUNIT_API IConstWUGraphProgress *createConstGraphProgress(const char *_wuid, const char *_graphName, IPropertyTree *_progress)
  334. {
  335. return new CConstGraphProgress(_wuid, _graphName, _progress);
  336. }
  337. //--------------------------------------------------------------------------------------------------------------------
  338. /*
  339. * Create a user friendly description a scope/stats combination. Only currently used for elapsed time for root subgraphs
  340. */
  341. static void createDefaultDescription(StringBuffer & description, StatisticKind kind, StatisticScopeType scopeType, const char * scope)
  342. {
  343. switch (kind)
  344. {
  345. case StTimeElapsed:
  346. {
  347. if (scopeType != SSTsubgraph)
  348. break;
  349. //Create a default description for a root subgraph
  350. const char * colon = strchr(scope, ':');
  351. if (!colon)
  352. break;
  353. const char * subgraph = colon+1;
  354. //Check for nested subgraph
  355. if (strchr(subgraph, ':'))
  356. break;
  357. assertex(strncmp(subgraph, SubGraphScopePrefix, strlen(SubGraphScopePrefix)) == 0);
  358. StringAttr graphname;
  359. graphname.set(scope, colon - scope);
  360. unsigned subId = atoi(subgraph + strlen(SubGraphScopePrefix));
  361. formatGraphTimerLabel(description, graphname, 0, subId);
  362. return;
  363. }
  364. }
  365. describeScope(description, scope);
  366. }
  367. /* Represents a single statistic */
  368. class ExtractedStatistic : public CInterfaceOf<IConstWUStatistic>
  369. {
  370. public:
  371. virtual IStringVal & getDescription(IStringVal & str, bool createDefault) const
  372. {
  373. if (!description && createDefault)
  374. {
  375. StringBuffer desc;
  376. createDefaultDescription(desc, kind, scopeType, scope);
  377. str.set(desc);
  378. return str;
  379. }
  380. str.set(description);
  381. return str;
  382. }
  383. virtual IStringVal & getCreator(IStringVal & str) const
  384. {
  385. str.set(creator);
  386. return str;
  387. }
  388. virtual const char * queryScope() const
  389. {
  390. return scope ? scope : "";
  391. }
  392. virtual IStringVal & getFormattedValue(IStringVal & str) const
  393. {
  394. StringBuffer formatted;
  395. formatStatistic(formatted, value, measure);
  396. str.set(formatted);
  397. return str;
  398. }
  399. virtual StatisticMeasure getMeasure() const
  400. {
  401. return measure;
  402. }
  403. virtual StatisticKind getKind() const
  404. {
  405. return kind;
  406. }
  407. virtual StatisticCreatorType getCreatorType() const
  408. {
  409. return creatorType;
  410. }
  411. virtual StatisticScopeType getScopeType() const
  412. {
  413. return scopeType;
  414. }
  415. virtual unsigned __int64 getValue() const
  416. {
  417. return value;
  418. }
  419. virtual unsigned __int64 getCount() const
  420. {
  421. return count;
  422. }
  423. virtual unsigned __int64 getMax() const
  424. {
  425. return max;
  426. }
  427. virtual unsigned __int64 getTimestamp() const
  428. {
  429. return timeStamp;
  430. }
  431. public:
  432. StringBuffer creator;
  433. StringBuffer description;
  434. StringBuffer scope;
  435. StatisticMeasure measure;
  436. StatisticKind kind;
  437. StatisticCreatorType creatorType;
  438. StatisticScopeType scopeType;
  439. unsigned __int64 value;
  440. unsigned __int64 count;
  441. unsigned __int64 max;
  442. unsigned __int64 timeStamp;
  443. };
  444. //---------------------------------------------------------------------------------------------------------------------
  445. /*
  446. * The following compare functions are used to ensure that comparison are consistent with
  447. * compareScopeName() in jstats. This ensures that the scope iterators from different sources
  448. * are processed in a consistent order
  449. */
  450. static int compareGraphNode(IInterface * const *ll, IInterface * const *rr)
  451. {
  452. IPropertyTree *l = (IPropertyTree *) *ll;
  453. IPropertyTree *r = (IPropertyTree *) *rr;
  454. unsigned lwfid = l->getPropInt("@wfid");
  455. unsigned rwfid = r->getPropInt("@wfid");
  456. if (lwfid != rwfid)
  457. return lwfid > rwfid ? +1 : -1;
  458. const char * lname = l->queryName();
  459. const char * rname = r->queryName();
  460. return compareScopeName(lname, rname);
  461. }
  462. static int compareSubGraphStatsNode(IInterface * const *ll, IInterface * const *rr)
  463. {
  464. IPropertyTree *l = (IPropertyTree *) *ll;
  465. IPropertyTree *r = (IPropertyTree *) *rr;
  466. return compareScopeName(l->queryName(), r->queryName());
  467. }
  468. static int compareSubGraphNode(IInterface * const *ll, IInterface * const *rr)
  469. {
  470. IPropertyTree *l = (IPropertyTree *) *ll;
  471. IPropertyTree *r = (IPropertyTree *) *rr;
  472. return l->getPropInt("@id") - r->getPropInt("@id");
  473. }
  474. static int compareActivityNode(IInterface * const *ll, IInterface * const *rr)
  475. {
  476. IPropertyTree *l = (IPropertyTree *) *ll;
  477. IPropertyTree *r = (IPropertyTree *) *rr;
  478. return l->getPropInt("@id") - r->getPropInt("@id");
  479. }
  480. static int compareEdgeNode(IInterface * const *ll, IInterface * const *rr)
  481. {
  482. IPropertyTree *l = (IPropertyTree *) *ll;
  483. IPropertyTree *r = (IPropertyTree *) *rr;
  484. //MORE: Edge needs more work
  485. const char * leftId = l->queryProp("@id");
  486. const char * rightId = r->queryProp("@id");
  487. unsigned leftAc = atoi(leftId);
  488. unsigned rightAc = atoi(rightId);
  489. if (leftAc != rightAc)
  490. return (int)(leftAc - rightAc);
  491. const char * leftSep = strchr(leftId, '_');
  492. const char * rightSep = strchr(rightId, '_');
  493. assertex(leftSep && rightSep);
  494. return atoi(leftSep+1) - atoi(rightSep+1);
  495. }
  496. //---------------------------------------------------------------------------------------------------------------------
  497. /*
  498. * A class for implementing a scope iterator that walks through graph progress information
  499. */
  500. class CConstGraphProgressScopeIterator : public CInterfaceOf<IConstWUScopeIterator>
  501. {
  502. public:
  503. CConstGraphProgressScopeIterator(const char * wuid, const ScopeFilter & _filter, __uint64 _minVersion) : filter(_filter), minVersion(_minVersion)
  504. {
  505. //Examine the filter, and determine if we only need to look at a single graph/subgraph
  506. StringAttr singleGraph;
  507. const StringArray & scopesToMatch = filter.queryScopes();
  508. if (scopesToMatch)
  509. {
  510. bool seenGraph = false;
  511. bool seenSubGraph = false;
  512. ForEachItemIn(iScope, scopesToMatch)
  513. {
  514. StringArray ids;
  515. ids.appendList(scopesToMatch.item(iScope), ":");
  516. ForEachItemIn(i, ids)
  517. {
  518. const char * curId = ids.item(i);
  519. StatsScopeId id(curId);
  520. switch (id.queryScopeType())
  521. {
  522. case SSTgraph:
  523. if (seenGraph)
  524. {
  525. if (singleGraph && !streq(singleGraph, curId))
  526. singleGraph.clear();
  527. }
  528. else
  529. {
  530. if (!id.isWildcard())
  531. singleGraph.set(curId);
  532. seenGraph = true;
  533. }
  534. break;
  535. case SSTsubgraph:
  536. if (seenSubGraph)
  537. {
  538. if (singleSubGraph && !streq(singleSubGraph, curId))
  539. singleSubGraph.clear();
  540. }
  541. else
  542. {
  543. if (!id.isWildcard())
  544. singleSubGraph.set(curId);
  545. seenSubGraph = true;
  546. }
  547. break;
  548. }
  549. }
  550. }
  551. }
  552. rootPath.append("/GraphProgress/").append(wuid).append('/');
  553. if (singleGraph)
  554. rootPath.append(singleGraph).append("/");
  555. //Don't lock the statistics while we iterate - any partial updates must not cause problems
  556. if (daliClientActive())
  557. conn.setown(querySDS().connect(rootPath.str(), myProcessSession(), RTM_NONE, SDS_LOCK_TIMEOUT));
  558. if (conn && !singleGraph)
  559. {
  560. graphIter.setown(conn->queryRoot()->getElements("*"));
  561. graphIter.setown(createSortedIterator(*graphIter, compareGraphNode));
  562. }
  563. valid = false;
  564. }
  565. IMPLEMENT_IINTERFACE_USING(CInterfaceOf<IConstWUScopeIterator>)
  566. virtual bool first()
  567. {
  568. valid = false;
  569. if (!conn)
  570. return false;
  571. if (graphIter && !graphIter->first())
  572. return false;
  573. if (!firstSubGraph())
  574. {
  575. if (!nextGraph())
  576. return false;
  577. }
  578. valid = true;
  579. return true;
  580. }
  581. virtual bool next()
  582. {
  583. if (!nextChildScope())
  584. {
  585. if (!nextSubGraph())
  586. {
  587. if (!nextGraph())
  588. {
  589. valid = false;
  590. return false;
  591. }
  592. }
  593. }
  594. return true;
  595. }
  596. virtual bool nextSibling() override
  597. {
  598. if (collections.ordinality() == 0)
  599. return false;
  600. assertex(childIterators.ordinality() < collections.ordinality());
  601. collections.pop();
  602. //next will call childIterator.next() - walking the next sibling
  603. return next();
  604. }
  605. virtual bool nextParent() override
  606. {
  607. if (collections.ordinality() == 0)
  608. return false;
  609. assertex(childIterators.ordinality() < collections.ordinality());
  610. collections.pop();
  611. if (collections.ordinality() == 0)
  612. return false;
  613. //Finish with this node - so next will move onto the sibling of the parent node.
  614. finishCollection();
  615. return next();
  616. }
  617. virtual bool isValid()
  618. {
  619. return valid;
  620. }
  621. protected:
  622. bool firstSubGraph()
  623. {
  624. IPropertyTree & graphNode = graphIter ? graphIter->query() : *conn->queryRoot();
  625. const char * xpath = "sg*";
  626. StringBuffer childXpath;
  627. if (singleSubGraph)
  628. {
  629. childXpath.append(singleSubGraph);
  630. xpath = childXpath.str();
  631. }
  632. subgraphIter.setown(graphNode.getElements(xpath));
  633. if (subgraphIter)
  634. {
  635. if (!singleSubGraph)
  636. subgraphIter.setown(createSortedIterator(*subgraphIter, compareSubGraphStatsNode));
  637. }
  638. else
  639. subgraphIter.setown(graphNode.getElements("sg0"));
  640. if (!subgraphIter->first())
  641. return false;
  642. if (firstStat())
  643. return true;
  644. return nextSubGraph();
  645. }
  646. bool nextSubGraph()
  647. {
  648. for (;;)
  649. {
  650. if (!subgraphIter->next())
  651. return false;
  652. if (firstStat())
  653. return true;
  654. }
  655. }
  656. bool nextGraph()
  657. {
  658. if (!graphIter)
  659. return false;
  660. for (;;)
  661. {
  662. if (!graphIter->next())
  663. return false;
  664. if (firstSubGraph())
  665. return true;
  666. }
  667. }
  668. bool firstStat()
  669. {
  670. IPropertyTree & curSubGraph = subgraphIter->query();
  671. if (!checkSubGraph())
  672. return false;
  673. curSubGraph.getPropBin("Stats", compressed.clear());
  674. //Don't crash on old format progress...
  675. if (compressed.length() == 0)
  676. return false;
  677. decompressToBuffer(serialized.clear(), compressed);
  678. Owned<IStatisticCollection> collection = createStatisticCollection(serialized);
  679. statsIterator.timeStamp = collection->queryWhenCreated();
  680. if (!beginCollection(*collection))
  681. return false;
  682. // When workflow is root element, it is just a container. Ignore the workflow element here
  683. // as WorkUnitStatisticsScopeIterator will produce workflow scope - don't want duplicates.
  684. // (Note: workflow element never contains stats).
  685. if (collections.tos().queryScopeType() == SSTworkflow)
  686. {
  687. if (!next())
  688. return false;
  689. }
  690. //The root element of a collection is a graph - but it is only there to nest the subgraphs in.
  691. //Do not iterate it as a separate element - unless it has some stats.
  692. IStatisticCollection & curCollection = collections.tos();
  693. if ((curCollection.queryScopeType() != SSTgraph) || (curCollection.getNumStatistics() != 0))
  694. return true;
  695. return next();
  696. }
  697. bool beginCollection(IStatisticCollection & collection)
  698. {
  699. collections.append(OLINK(collection));
  700. curScopeType = collection.queryScopeType();
  701. collection.getFullScope(curScopeName.clear());
  702. ScopeCompare result = filter.compare(curScopeName);
  703. if (result & SCequal)
  704. return true;
  705. //If this scope cannot be the parent of a match then discard it.
  706. if (!(result & SCparent))
  707. collections.pop();
  708. //walk the next element
  709. return nextChildScope();
  710. }
  711. bool nextChildScope()
  712. {
  713. for (;;)
  714. {
  715. if (collections.ordinality() == 0)
  716. return false;
  717. IStatisticCollection * curCollection = &collections.tos();
  718. if (childIterators.ordinality() < collections.ordinality())
  719. {
  720. ScopeCompare result = filter.compare(curScopeName);
  721. //Do not walk children scopes if it is unrelated
  722. if (result & SCparent)
  723. {
  724. //Start iterating the children for the current collection
  725. childIterators.append(curCollection->getScopes(NULL, true));
  726. if (!childIterators.tos().first())
  727. {
  728. finishCollection();
  729. continue;
  730. }
  731. }
  732. else
  733. {
  734. //Don't walk the child scopes
  735. collections.pop();
  736. continue;
  737. }
  738. }
  739. else if (!childIterators.tos().next())
  740. {
  741. finishCollection();
  742. continue;
  743. }
  744. if (beginCollection(childIterators.tos().query()))
  745. return true;
  746. }
  747. }
  748. void finishCollection()
  749. {
  750. collections.pop();
  751. childIterators.pop();
  752. }
  753. bool checkSubGraph()
  754. {
  755. IPropertyTree & curSubGraph = subgraphIter->query();
  756. StatisticCreatorType creatorType = queryCreatorType(curSubGraph.queryProp("@c"), SCTnone);
  757. const char * creator = curSubGraph.queryProp("@creator");
  758. //MORE: Check minVersion and allow early filtering
  759. //MORE: Potentially filter by creator type??
  760. // if (!filter->matches(creatorType, creator, SSTall, NULL, SMeasureAll, StKindAll, AnyStatisticValue))
  761. // return false;
  762. statsIterator.creatorType = creatorType;
  763. statsIterator.creator.set(creator);
  764. return true;
  765. }
  766. virtual void playProperties(IWuScopeVisitor & visitor, WuPropertyTypes whichProperties) override
  767. {
  768. if ((whichProperties & PTstatistics))
  769. {
  770. /*
  771. MORE: Code from the statsIterator class should be inlined here - code will be much simpler
  772. but it currently needs an implementation of the IConstWUStatistic 3rd parameter
  773. IStatisticCollection & collection = collections.tos();
  774. ForEachItemIn(i, collection)
  775. {
  776. StatisticKind kind;
  777. unsigned __int64 value;
  778. collection->getStatistic(kind, value, i);
  779. visitor.noteStatistic(kind, value, *this);
  780. }
  781. */
  782. statsIterator.reset(curScopeName, curScopeType, collections.tos());
  783. ForEach(statsIterator)
  784. statsIterator.play(visitor);
  785. }
  786. }
  787. virtual bool getStat(StatisticKind kind, unsigned __int64 & value) const override
  788. {
  789. return collections.tos().getStatistic(kind, value);
  790. }
  791. virtual const char * queryAttribute(WuAttr attr, StringBuffer & scratchpad) const override
  792. {
  793. return nullptr;
  794. }
  795. virtual const char * queryHint(const char * kind) const override
  796. {
  797. return nullptr;
  798. }
  799. virtual const char * queryScope() const override
  800. {
  801. return curScopeName;
  802. }
  803. virtual StatisticScopeType getScopeType() const override
  804. {
  805. return curScopeType;
  806. }
  807. private:
  808. class ScopeStatisticsIterator : public CInterfaceOf<IConstWUStatistic>
  809. {
  810. friend class CConstGraphProgressScopeIterator; // cleaner if this was removed + setScope() functions added.
  811. public:
  812. void reset(const char * _scopeName, StatisticScopeType _scopeType, IStatisticCollection & _collection)
  813. {
  814. scope.set(_scopeName);
  815. scopeType = _scopeType;
  816. collection = &_collection;
  817. numStats = collection->getNumStatistics();
  818. }
  819. // interface IConstWUStatisticIterator
  820. IConstWUStatistic & query()
  821. {
  822. return *this;
  823. }
  824. bool first()
  825. {
  826. curStatIndex = 0;
  827. if (curStatIndex >= numStats)
  828. return false;
  829. collection->getStatistic(kind, value, curStatIndex);
  830. //MORE: Allow filtering:
  831. /*
  832. * if (filter && !filter->matches(SCTall, NULL, SSTall, NULL, queryMeasure(kind), kind, value))
  833. continue;
  834. */
  835. return true;
  836. }
  837. bool next()
  838. {
  839. for (;;)
  840. {
  841. ++curStatIndex;
  842. if (curStatIndex >= numStats)
  843. return false;
  844. collection->getStatistic(kind, value, curStatIndex);
  845. //MORE: Allow stats filtering
  846. return true;
  847. }
  848. }
  849. bool isValid()
  850. {
  851. return curStatIndex < numStats;
  852. }
  853. //interface IConstWUStatistic
  854. virtual IStringVal & getDescription(IStringVal & str, bool createDefault) const override
  855. {
  856. if (createDefault)
  857. {
  858. StringBuffer description;
  859. createDefaultDescription(description, kind, scopeType, scope);
  860. str.set(description);
  861. }
  862. return str;
  863. }
  864. virtual IStringVal & getCreator(IStringVal & str) const override
  865. {
  866. str.set(creator);
  867. return str;
  868. }
  869. virtual const char * queryScope() const override
  870. {
  871. return scope;
  872. }
  873. virtual IStringVal & getFormattedValue(IStringVal & str) const override
  874. {
  875. StringBuffer formatted;
  876. formatStatistic(formatted, value, kind);
  877. str.set(formatted);
  878. return str;
  879. }
  880. virtual StatisticMeasure getMeasure() const override
  881. {
  882. return queryMeasure(kind);
  883. }
  884. virtual StatisticKind getKind() const override
  885. {
  886. return kind;
  887. }
  888. virtual StatisticCreatorType getCreatorType() const override
  889. {
  890. return creatorType;
  891. }
  892. virtual StatisticScopeType getScopeType() const override
  893. {
  894. return scopeType;
  895. }
  896. virtual unsigned __int64 getValue() const override
  897. {
  898. return value;
  899. }
  900. virtual unsigned __int64 getCount() const override
  901. {
  902. return 1;
  903. }
  904. virtual unsigned __int64 getMax() const override
  905. {
  906. return 0;
  907. }
  908. virtual unsigned __int64 getTimestamp() const override
  909. {
  910. return timeStamp;
  911. }
  912. void play(IWuScopeVisitor & visitor)
  913. {
  914. visitor.noteStatistic(kind, value, *this);
  915. }
  916. protected:
  917. IStatisticCollection * collection = nullptr;
  918. StringBuffer creator;
  919. StringBuffer scope;
  920. StatisticKind kind;
  921. StatisticCreatorType creatorType;
  922. StatisticScopeType scopeType;
  923. unsigned __int64 value = 0;
  924. unsigned __int64 timeStamp;
  925. unsigned curStatIndex = 0;
  926. unsigned numStats = 0;
  927. } statsIterator;
  928. Owned<IRemoteConnection> conn;
  929. StringBuffer curScopeName;
  930. StatisticScopeType curScopeType = SSTnone;
  931. __uint64 minVersion;
  932. const ScopeFilter & filter;
  933. StringBuffer rootPath;
  934. StringAttr singleSubGraph;
  935. Owned<IPropertyTreeIterator> graphIter;
  936. Owned<IPropertyTreeIterator> subgraphIter;
  937. IArrayOf<IStatisticCollection> collections;
  938. IArrayOf<IStatisticCollectionIterator> childIterators; // Iterator(n) through collections(n) - created once iterating children
  939. MemoryBuffer compressed;
  940. MemoryBuffer serialized;
  941. bool valid;
  942. };
  943. int compareNoteScopeOrder(IConstWUException & left, IConstWUException & right)
  944. {
  945. return compareScopeName(left.queryScope(), right.queryScope());
  946. }
  947. int compareNoteScopeOrder(IInterface * const * left, IInterface * const * right)
  948. {
  949. return compareNoteScopeOrder(*static_cast<IConstWUException *>(*left), *static_cast<IConstWUException *>(*right));
  950. }
  951. class NotesIterator : public CInterfaceOf<IConstWUScopeIterator>
  952. {
  953. public:
  954. NotesIterator(const IConstWorkUnit * wu, const ScopeFilter & _filter)
  955. {
  956. Owned<IConstWUExceptionIterator> exceptions = &wu->getExceptions();
  957. ForEach(*exceptions)
  958. {
  959. IConstWUException & exception = exceptions->query();
  960. if (exception.queryScope()!=nullptr)
  961. notes.append(OLINK(exception));
  962. }
  963. notes.sort(compareNoteScopeOrder);
  964. }
  965. virtual bool first() override
  966. {
  967. baseIndex = 0;
  968. return updateCurrent();
  969. }
  970. virtual bool next() override
  971. {
  972. baseIndex += numCurrentScope;
  973. return updateCurrent();
  974. }
  975. virtual bool isValid() override
  976. {
  977. return notes.isItem(baseIndex);
  978. }
  979. virtual bool nextSibling() override
  980. {
  981. //Search until the current scope is not a child of the previous scope
  982. StringBuffer savedScope(queryScope());
  983. for (;;)
  984. {
  985. if (!next())
  986. return false;
  987. if (compareScopes(queryScope(), savedScope) != SCchild)
  988. return true;
  989. }
  990. }
  991. virtual bool nextParent() override
  992. {
  993. //Search until the current scope is not a child of the previous parent scope
  994. StringBuffer parentScope;
  995. if (getParentScope(parentScope, queryScope()))
  996. {
  997. for (;;)
  998. {
  999. if (!next())
  1000. return false;
  1001. if (compareScopes(queryScope(), parentScope) != SCchild)
  1002. return true;
  1003. }
  1004. }
  1005. else
  1006. {
  1007. finish();
  1008. return false;
  1009. }
  1010. }
  1011. virtual const char * queryScope() const override
  1012. {
  1013. const char * scope = notes.item(baseIndex).queryScope();
  1014. return scope ? scope : "";
  1015. }
  1016. virtual StatisticScopeType getScopeType() const override
  1017. {
  1018. const char * tail = queryScopeTail(queryScope());
  1019. StatsScopeId id(tail);
  1020. return id.queryScopeType();
  1021. }
  1022. virtual void playProperties(IWuScopeVisitor & visitor, WuPropertyTypes whichProperties = PTall) override
  1023. {
  1024. if (whichProperties & PTnotes)
  1025. {
  1026. for (unsigned i=0; i < numCurrentScope; i++)
  1027. {
  1028. IConstWUException & cur = notes.item(baseIndex + i);
  1029. visitor.noteException(cur);
  1030. }
  1031. }
  1032. }
  1033. virtual bool getStat(StatisticKind kind, unsigned __int64 & value) const override
  1034. {
  1035. return false;
  1036. }
  1037. virtual const char * queryAttribute(WuAttr attr, StringBuffer & scratchpad) const override
  1038. {
  1039. return nullptr;
  1040. }
  1041. virtual const char * queryHint(const char * kind) const override
  1042. {
  1043. return nullptr;
  1044. }
  1045. private:
  1046. bool updateCurrent()
  1047. {
  1048. if (!notes.isItem(baseIndex))
  1049. return false;
  1050. // set numCurrentScope to number of notes in the current scope
  1051. unsigned next = baseIndex+1;
  1052. while (next < notes.ordinality())
  1053. {
  1054. if (compareNoteScopeOrder(notes.item(baseIndex), notes.item(next)) != 0)
  1055. break;
  1056. next++;
  1057. }
  1058. numCurrentScope = (next - baseIndex);
  1059. return true;
  1060. }
  1061. void finish()
  1062. {
  1063. baseIndex = notes.ordinality();
  1064. }
  1065. unsigned baseIndex = 0;
  1066. unsigned numCurrentScope = 0;
  1067. IArrayOf<IConstWUException> notes;
  1068. };
  1069. int compareStatisticScopes(IConstWUStatistic & left, IConstWUStatistic & right)
  1070. {
  1071. return compareScopeName(left.queryScope(), right.queryScope());
  1072. }
  1073. int compareStatisticScopes(IInterface * const * left, IInterface * const * right)
  1074. {
  1075. return compareStatisticScopes(*static_cast<IConstWUStatistic *>(*left), *static_cast<IConstWUStatistic *>(*right));
  1076. }
  1077. /*
  1078. * An implementation of IConstWUScopeIterator for global workunit statistics.
  1079. */
  1080. class WorkUnitStatisticsScopeIterator : public CInterfaceOf<IConstWUScopeIterator>
  1081. {
  1082. public:
  1083. WorkUnitStatisticsScopeIterator(const IArrayOf<IConstWUStatistic> & _statistics, const ScopeFilter & _filter)
  1084. {
  1085. ForEachItemIn(i, _statistics)
  1086. {
  1087. IConstWUStatistic & cur = _statistics.item(i);
  1088. if (_filter.compare(cur.queryScope()) & SCequal)
  1089. statistics.append(OLINK(cur));
  1090. }
  1091. statistics.sort(compareStatisticScopes);
  1092. }
  1093. virtual bool first() override
  1094. {
  1095. curIndex = 0;
  1096. return initScope();
  1097. }
  1098. virtual bool next() override
  1099. {
  1100. curIndex += numStatistics;
  1101. return initScope();
  1102. }
  1103. virtual bool nextSibling() override
  1104. {
  1105. //Search until the current scope is not a child of the previous scope
  1106. StringBuffer savedScope(queryScope());
  1107. for (;;)
  1108. {
  1109. if (!next())
  1110. return false;
  1111. if (compareScopes(queryScope(), savedScope) != SCchild)
  1112. return true;
  1113. }
  1114. }
  1115. virtual bool nextParent() override
  1116. {
  1117. //Search until the current scope is not a child of the previous parent scope
  1118. StringBuffer parentScope;
  1119. if (getParentScope(parentScope, queryScope()))
  1120. {
  1121. for (;;)
  1122. {
  1123. if (!next())
  1124. return false;
  1125. if (compareScopes(queryScope(), parentScope) != SCchild)
  1126. return true;
  1127. }
  1128. }
  1129. else
  1130. {
  1131. finish();
  1132. return false;
  1133. }
  1134. }
  1135. virtual bool isValid() override
  1136. {
  1137. return statistics.isItem(curIndex);
  1138. }
  1139. virtual const char * queryScope() const override
  1140. {
  1141. return statistics.item(curIndex).queryScope();
  1142. }
  1143. virtual StatisticScopeType getScopeType() const override
  1144. {
  1145. return statistics.item(curIndex).getScopeType();
  1146. }
  1147. virtual void playProperties(IWuScopeVisitor & visitor, WuPropertyTypes whichProperties) override
  1148. {
  1149. if (whichProperties & PTstatistics)
  1150. {
  1151. for (unsigned i=0; i < numStatistics; i++)
  1152. {
  1153. IConstWUStatistic & cur = statistics.item(curIndex + i);
  1154. visitor.noteStatistic(cur.getKind(), cur.getValue(), cur);
  1155. }
  1156. }
  1157. }
  1158. virtual bool getStat(StatisticKind kind, unsigned __int64 & value) const override
  1159. {
  1160. for (unsigned i=0; i < numStatistics; i++)
  1161. {
  1162. IConstWUStatistic & cur = statistics.item(curIndex + i);
  1163. if (cur.getKind() == kind)
  1164. {
  1165. value = cur.getValue();
  1166. return true;
  1167. }
  1168. }
  1169. return false;
  1170. }
  1171. virtual const char * queryAttribute(WuAttr attr, StringBuffer & scratchpad) const override
  1172. {
  1173. return nullptr;
  1174. }
  1175. virtual const char * queryHint(const char * kind) const
  1176. {
  1177. return nullptr;
  1178. }
  1179. protected:
  1180. inline IConstWUStatistic & queryStatistic(unsigned i)
  1181. {
  1182. return statistics.item(curIndex + i);
  1183. }
  1184. bool initScope()
  1185. {
  1186. if (!statistics.isItem(curIndex))
  1187. return false;
  1188. unsigned next = curIndex+1;
  1189. while (next < statistics.ordinality())
  1190. {
  1191. if (compareStatisticScopes(statistics.item(curIndex), statistics.item(next)) != 0)
  1192. break;
  1193. next++;
  1194. }
  1195. numStatistics = (next - curIndex);
  1196. return true;
  1197. }
  1198. void finish()
  1199. {
  1200. curIndex = statistics.ordinality();
  1201. }
  1202. protected:
  1203. IArrayOf<IConstWUStatistic> statistics;
  1204. unsigned curIndex = 0;
  1205. unsigned numStatistics = 1;
  1206. };
  1207. /*
  1208. * An implementation of IConstWUScopeIterator for the query graphs.
  1209. */
  1210. class GraphScopeIterator : public CInterfaceOf<IConstWUScopeIterator>
  1211. {
  1212. private:
  1213. //This uses a state machine - this enumeration contains the different states.
  1214. enum State
  1215. {
  1216. //Following are the states which represent valid scopes
  1217. SGraph,
  1218. SChildGraph,
  1219. SSubGraph,
  1220. SEdge,
  1221. SActivity,
  1222. //The following are internal states.
  1223. SGraphFirstEdge,
  1224. SGraphFirstSubGraph,
  1225. SGraphFirst,
  1226. SGraphEnd,
  1227. SGraphNext,
  1228. SSubGraphFirstEdge,
  1229. SSubGraphFirstActivity,
  1230. SSubGraphEnd,
  1231. SSubGraphNext,
  1232. SEdgeNext,
  1233. SEdgeEnd,
  1234. SActivityNext,
  1235. SActivityEnd,
  1236. SChildGraphFirstEdge,
  1237. SChildGraphFirstSubGraph,
  1238. SChildGraphFirst,
  1239. SChildGraphNext,
  1240. SChildGraphEnd,
  1241. SDone
  1242. };
  1243. State state = SDone;
  1244. State nextState = SDone;
  1245. public:
  1246. GraphScopeIterator(const IConstWorkUnit * wu, const ScopeFilter & _filter) : graphIter(&wu->getGraphs(GraphTypeAny)), filter(_filter)
  1247. {
  1248. }
  1249. virtual bool first() override
  1250. {
  1251. state = SGraphFirst;
  1252. return nextScope();
  1253. }
  1254. virtual bool next() override
  1255. {
  1256. if (!selectNext())
  1257. return false;
  1258. return nextScope();
  1259. }
  1260. virtual bool nextSibling() override
  1261. {
  1262. if (!selectNextSibling())
  1263. return false;
  1264. return nextScope();
  1265. }
  1266. virtual bool nextParent() override
  1267. {
  1268. if (!selectNextParent())
  1269. return false;
  1270. return nextScope();
  1271. }
  1272. virtual bool isValid() override
  1273. {
  1274. return graphIter->isValid();
  1275. }
  1276. virtual const char * queryScope() const override
  1277. {
  1278. return curScopeName.str();
  1279. }
  1280. virtual StatisticScopeType getScopeType() const override
  1281. {
  1282. return scopeType;
  1283. }
  1284. virtual void playProperties(IWuScopeVisitor & visitor, WuPropertyTypes whichProperties) override
  1285. {
  1286. switch (scopeType)
  1287. {
  1288. case SSTgraph:
  1289. return;
  1290. }
  1291. IPropertyTree & cur = treeIters.tos().query();
  1292. switch (scopeType)
  1293. {
  1294. case SSTgraph:
  1295. break;
  1296. case SSTsubgraph:
  1297. break;
  1298. case SSTactivity:
  1299. {
  1300. if (whichProperties & PTattributes)
  1301. {
  1302. playAttribute(visitor, WaLabel);
  1303. Owned<IPropertyTreeIterator> attrs = cur.getElements("att");
  1304. ForEach(*attrs)
  1305. {
  1306. IPropertyTree & cur = attrs->query();
  1307. WuAttr attr = queryGraphChildAttToWuAttr(cur.queryProp("@name"));
  1308. if (attr != WaNone)
  1309. visitor.noteAttribute(attr, cur.queryProp("@value"));
  1310. }
  1311. }
  1312. if (whichProperties & PThints)
  1313. {
  1314. Owned<IPropertyTreeIterator> hints = cur.getElements("hint");
  1315. ForEach(*hints)
  1316. {
  1317. IPropertyTree & cur = hints->query();
  1318. visitor.noteHint(cur.queryProp("@name"), cur.queryProp("@value"));
  1319. }
  1320. }
  1321. break;
  1322. }
  1323. case SSTedge:
  1324. if (whichProperties & PTattributes)
  1325. {
  1326. //MORE This will eventually need to walk the attributes and map the names.
  1327. //Need to be careful if they need to be mapped differently depending on the context.
  1328. playAttribute(visitor, WaLabel);
  1329. playAttribute(visitor, WaIdSource);
  1330. playAttribute(visitor, WaIdTarget);
  1331. playAttribute(visitor, WaSourceIndex);
  1332. playAttribute(visitor, WaTargetIndex);
  1333. playAttribute(visitor, WaIsDependency);
  1334. }
  1335. break;
  1336. }
  1337. }
  1338. virtual bool getStat(StatisticKind kind, unsigned __int64 & value) const override
  1339. {
  1340. return false;
  1341. }
  1342. virtual const char * queryAttribute(WuAttr attr, StringBuffer & scratchpad) const override
  1343. {
  1344. if (!treeIters.ordinality())
  1345. return nullptr;
  1346. //MORE - check that the attribute is value for the current scope type (to prevent defaults being returned)
  1347. return queryAttributeValue(treeIters.tos().query(), attr, scratchpad);
  1348. }
  1349. virtual const char * queryHint(const char * kind) const override
  1350. {
  1351. //MORE: Needs to be implemented!
  1352. return nullptr;
  1353. }
  1354. private:
  1355. void playAttribute(IWuScopeVisitor & visitor, WuAttr kind)
  1356. {
  1357. StringBuffer scratchpad;
  1358. const char * value = queryAttributeValue(treeIters.tos().query(), kind, scratchpad);
  1359. if (value)
  1360. visitor.noteAttribute(kind, value);
  1361. }
  1362. void pushIterator(IPropertyTreeIterator * iter, State state)
  1363. {
  1364. treeIters.append(*LINK(iter));
  1365. stateStack.append(state);
  1366. }
  1367. State popIterator()
  1368. {
  1369. treeIters.pop();
  1370. return (State)stateStack.popGet();
  1371. }
  1372. void pushScope(const char * id)
  1373. {
  1374. scopeLengths.append(curScopeName.length());
  1375. curScopeName.append(":").append(id);
  1376. }
  1377. void popScope()
  1378. {
  1379. curScopeName.setLength(scopeLengths.popGet());
  1380. }
  1381. bool doNextScope()
  1382. {
  1383. for(;;)
  1384. {
  1385. switch (state)
  1386. {
  1387. case SGraph:
  1388. {
  1389. IConstWUGraph & graph = graphIter->query();
  1390. unsigned wfid = graph.getWfid();
  1391. curScopeName.clear();
  1392. if (wfid != 0)
  1393. curScopeName.append(WorkflowScopePrefix).append(wfid).append(':');
  1394. graph.getName(StringBufferAdaptor(curScopeName));
  1395. scopeType = SSTgraph;
  1396. return true;
  1397. }
  1398. case SSubGraph:
  1399. scopeId.set(SubGraphScopePrefix).append(treeIters.tos().query().getPropInt("@id"));
  1400. pushScope(scopeId);
  1401. scopeType = SSTsubgraph;
  1402. return true;
  1403. case SEdge:
  1404. scopeId.set(EdgeScopePrefix).append(treeIters.tos().query().queryProp("@id"));
  1405. pushScope(scopeId);
  1406. scopeType = SSTedge;
  1407. return true;
  1408. case SActivity:
  1409. if (treeIters.tos().query().getPropInt("att[@name='_kind']/@value") == TAKsubgraph)
  1410. {
  1411. state = SActivityNext;
  1412. break;
  1413. }
  1414. scopeId.set(ActivityScopePrefix).append(treeIters.tos().query().getPropInt("@id"));
  1415. pushScope(scopeId);
  1416. scopeType = SSTactivity;
  1417. return true;
  1418. case SChildGraph:
  1419. {
  1420. #ifdef _DEBUG
  1421. assertex(treeIters.tos().query().getPropInt("att[@name='_kind']/@value") == TAKsubgraph);
  1422. unsigned numIters = treeIters.ordinality();
  1423. unsigned parentActivityId = treeIters.item(numIters-2).query().getPropInt("@id");
  1424. unsigned parentId = treeIters.tos().query().getPropInt("att[@name='_parentActivity']/@value");
  1425. assertex(parentId == parentActivityId);
  1426. #endif
  1427. scopeId.set(ChildGraphScopePrefix).append(treeIters.tos().query().getPropInt("@id"));
  1428. pushScope(scopeId);
  1429. scopeType = SSTchildgraph;
  1430. return true;
  1431. }
  1432. //Graph iteration
  1433. case SGraphFirst:
  1434. if (!graphIter->first())
  1435. state = SDone;
  1436. else
  1437. state = SGraph;
  1438. break;
  1439. case SGraphEnd:
  1440. state = SGraphNext;
  1441. break;
  1442. case SGraphNext:
  1443. if (!graphIter->next())
  1444. state = SDone;
  1445. else
  1446. state = SGraph;
  1447. break;
  1448. //Edge iteration
  1449. case SGraphFirstEdge:
  1450. {
  1451. //Walk dependencies - should possibly have a different SST e.g., SSTdependency since they do not
  1452. //share many characteristics with edges - e.g. no flowing records => few/no stats.
  1453. curGraph.setown(graphIter->query().getXGMMLTree(false, false));
  1454. Owned<IPropertyTreeIterator> treeIter = curGraph->getElements("edge");
  1455. if (treeIter && treeIter->first())
  1456. {
  1457. treeIter.setown(createSortedIterator(*treeIter, compareEdgeNode));
  1458. treeIter->first();
  1459. pushIterator(treeIter, SGraphFirstSubGraph);
  1460. state = SEdge;
  1461. }
  1462. else
  1463. state = SGraphFirstSubGraph;
  1464. break;
  1465. }
  1466. case SChildGraphFirstEdge:
  1467. {
  1468. Owned<IPropertyTreeIterator> treeIter = treeIters.tos().query().getElements("att/graph/edge");
  1469. if (treeIter && treeIter->first())
  1470. {
  1471. treeIter.setown(createSortedIterator(*treeIter, compareEdgeNode));
  1472. pushIterator(treeIter, SChildGraphFirstSubGraph);
  1473. state = SEdge;
  1474. }
  1475. else
  1476. state = SChildGraphFirstSubGraph;
  1477. break;
  1478. }
  1479. case SEdgeEnd:
  1480. popScope();
  1481. state = SEdgeNext;
  1482. break;
  1483. case SEdgeNext:
  1484. if (treeIters.tos().next())
  1485. state = SEdge;
  1486. else
  1487. state = popIterator();
  1488. break;
  1489. //Subgraph iteration
  1490. case SGraphFirstSubGraph:
  1491. {
  1492. Owned<IPropertyTreeIterator> treeIter = curGraph->getElements("node");
  1493. if (treeIter && treeIter->first())
  1494. {
  1495. treeIter.setown(createSortedIterator(*treeIter, compareSubGraphNode));
  1496. treeIter->first();
  1497. pushIterator(treeIter, SGraphNext);
  1498. state = SSubGraph;
  1499. }
  1500. else
  1501. state = SGraphNext;
  1502. break;
  1503. }
  1504. case SChildGraphFirstSubGraph:
  1505. {
  1506. Owned<IPropertyTreeIterator> treeIter = treeIters.tos().query().getElements("att/graph/node");
  1507. if (treeIter && treeIter->first())
  1508. {
  1509. treeIter.setown(createSortedIterator(*treeIter, compareSubGraphNode));
  1510. pushIterator(treeIter, SChildGraphEnd);
  1511. state = SSubGraph;
  1512. }
  1513. else
  1514. state = SChildGraphEnd;
  1515. break;
  1516. }
  1517. case SSubGraphFirstEdge:
  1518. {
  1519. Owned<IPropertyTreeIterator> treeIter = treeIters.tos().query().getElements("att/graph/edge");
  1520. if (treeIter && treeIter->first())
  1521. {
  1522. treeIter.setown(createSortedIterator(*treeIter, compareEdgeNode));
  1523. pushIterator(treeIter, SSubGraphFirstActivity);
  1524. state = SEdge;
  1525. }
  1526. else
  1527. state = SSubGraphFirstActivity;
  1528. break;
  1529. }
  1530. case SSubGraphFirstActivity:
  1531. {
  1532. //Walk the contents of each subgraph once, splitting the entries into activities and child graphs
  1533. IArrayOf<IPropertyTree> activities;
  1534. IArrayOf<IPropertyTree> childGraphs;
  1535. Owned<IPropertyTreeIterator> treeIter = treeIters.tos().query().getElements("att/graph/node");
  1536. ForEach(*treeIter)
  1537. {
  1538. IPropertyTree & cur = treeIter->get();
  1539. if (cur.getPropInt("att[@name='_kind']/@value") != TAKsubgraph)
  1540. activities.append(cur);
  1541. else
  1542. childGraphs.append(cur);
  1543. }
  1544. if (activities.ordinality())
  1545. {
  1546. //Create an iterator for the child graphs in this subgraph which is iterated in order as the activities are processed
  1547. Owned<IPropertyTreeIterator> graphIter = createSortedIterator(childGraphs, compareSubGraphNode);
  1548. graphIter->first();
  1549. childGraphIters.append(*graphIter.getClear());
  1550. Owned<IPropertyTreeIterator> activityIter = createSortedIterator(activities, compareActivityNode);
  1551. pushIterator(activityIter, SSubGraphEnd);
  1552. state = SActivity;
  1553. }
  1554. else
  1555. state = SSubGraphEnd;
  1556. break;
  1557. }
  1558. case SSubGraphEnd:
  1559. popScope();
  1560. state = SSubGraphNext;
  1561. break;
  1562. case SChildGraphEnd:
  1563. popScope();
  1564. state = SChildGraphNext;
  1565. break;
  1566. case SSubGraphNext:
  1567. if (treeIters.tos().next())
  1568. state = SSubGraph;
  1569. else
  1570. state = popIterator();
  1571. break;
  1572. case SActivityEnd:
  1573. popScope();
  1574. state = SActivityNext;
  1575. break;
  1576. case SActivityNext:
  1577. if (treeIters.tos().next())
  1578. state = SActivity;
  1579. else
  1580. {
  1581. assertex(!childGraphIters.tos().isValid());
  1582. childGraphIters.pop();
  1583. state = popIterator();
  1584. }
  1585. break;
  1586. case SChildGraphNext:
  1587. if (treeIters.tos().next())
  1588. state = SChildGraph;
  1589. else
  1590. state = popIterator();
  1591. break;
  1592. case SChildGraphFirst:
  1593. {
  1594. unsigned parentActivityId = treeIters.tos().query().getPropInt("@id");
  1595. IArrayOf<IPropertyTree> childGraphs;
  1596. IPropertyTreeIterator & allGraphs = childGraphIters.tos();
  1597. while (allGraphs.isValid())
  1598. {
  1599. IPropertyTree & cur = allGraphs.query();
  1600. unsigned parentId = cur.getPropInt("att[@name='_parentActivity']/@value");
  1601. if (parentId != parentActivityId)
  1602. break;
  1603. childGraphs.append(OLINK(cur));
  1604. allGraphs.next();
  1605. }
  1606. if (childGraphs.ordinality())
  1607. {
  1608. Owned<IPropertyTreeIterator> treeIter = createSortedIterator(childGraphs, compareSubGraphNode);
  1609. pushIterator(treeIter, SActivityEnd);
  1610. state = SChildGraph;
  1611. }
  1612. else
  1613. state = SActivityEnd;
  1614. break;
  1615. }
  1616. case SDone:
  1617. return false;
  1618. default:
  1619. throwUnexpected();
  1620. }
  1621. }
  1622. }
  1623. bool nextScope()
  1624. {
  1625. for(;;)
  1626. {
  1627. if (!doNextScope())
  1628. return false;
  1629. ScopeCompare cmp = filter.compare(curScopeName);
  1630. if (cmp & SCequal)
  1631. return true;
  1632. //MORE: Optimize next based on result of compare
  1633. if (!selectNext())
  1634. return false;
  1635. }
  1636. }
  1637. bool selectNext()
  1638. {
  1639. switch (state)
  1640. {
  1641. case SGraph:
  1642. state = SGraphFirstEdge;
  1643. break;
  1644. case SChildGraph:
  1645. state = SChildGraphFirstEdge;
  1646. break;
  1647. case SSubGraph:
  1648. state = SSubGraphFirstEdge;
  1649. break;
  1650. case SEdge:
  1651. state = SEdgeEnd;
  1652. break;
  1653. case SActivity:
  1654. state = SChildGraphFirst;
  1655. break;
  1656. case SDone:
  1657. return false;
  1658. default:
  1659. throwUnexpected();
  1660. }
  1661. return true;
  1662. }
  1663. bool selectNextSibling()
  1664. {
  1665. switch (state)
  1666. {
  1667. case SGraph:
  1668. state = SGraphEnd;
  1669. break;
  1670. case SChildGraph:
  1671. state = SChildGraphEnd;
  1672. break;
  1673. case SSubGraph:
  1674. state = SSubGraphEnd;
  1675. break;
  1676. case SEdge:
  1677. state = SEdgeEnd;
  1678. break;
  1679. case SActivity:
  1680. state = SActivityEnd;
  1681. break;
  1682. case SDone:
  1683. return false;
  1684. default:
  1685. throwUnexpected();
  1686. }
  1687. return true;
  1688. }
  1689. bool selectNextParent()
  1690. {
  1691. switch (state)
  1692. {
  1693. case SGraph:
  1694. state = SDone;
  1695. break;
  1696. case SActivity:
  1697. childGraphIters.pop();
  1698. //fall through
  1699. case SChildGraph:
  1700. case SSubGraph:
  1701. case SEdge:
  1702. popScope();
  1703. state = popIterator();
  1704. break;
  1705. case SDone:
  1706. return false;
  1707. default:
  1708. throwUnexpected();
  1709. }
  1710. return true;
  1711. }
  1712. protected:
  1713. const ScopeFilter & filter;
  1714. Owned<IConstWUGraphIterator> graphIter;
  1715. Owned<IPropertyTree> curGraph;
  1716. IArrayOf<IPropertyTreeIterator> treeIters;
  1717. IArrayOf<IPropertyTreeIterator> childGraphIters;
  1718. UnsignedArray scopeLengths;
  1719. UnsignedArray stateStack;
  1720. StringBuffer curScopeName;
  1721. StringBuffer scopeId;
  1722. StatisticScopeType scopeType = SSTnone;
  1723. };
  1724. static int compareWorkflow(IInterface * const * pLeft, IInterface * const * pRight)
  1725. {
  1726. IConstWorkflowItem * left = static_cast<IConstWorkflowItem *>(*pLeft);
  1727. IConstWorkflowItem * right = static_cast<IConstWorkflowItem *>(*pRight);
  1728. return left->queryWfid() - right->queryWfid();
  1729. }
  1730. static const char * trueToStr(bool value) { return value ? "true" : nullptr; }
  1731. /*
  1732. * An implementation of IConstWUScopeIterator for the workflow information.
  1733. */
  1734. class WorkflowStatisticsScopeIterator : public CInterfaceOf<IConstWUScopeIterator>
  1735. {
  1736. public:
  1737. WorkflowStatisticsScopeIterator(IConstWorkflowItemIterator * wfIter)
  1738. {
  1739. ForEach(*wfIter)
  1740. workflow.append(*LINK(wfIter->query()));
  1741. workflow.sort(compareWorkflow);
  1742. }
  1743. virtual bool first() override
  1744. {
  1745. if (workflow.empty())
  1746. return false;
  1747. curWorkflow = 0;
  1748. return initWorkflowItem();
  1749. }
  1750. virtual bool next() override
  1751. {
  1752. if (!workflow.isItem(++curWorkflow))
  1753. return false;
  1754. return initWorkflowItem();
  1755. }
  1756. virtual bool nextSibling() override
  1757. {
  1758. return next();
  1759. }
  1760. virtual bool nextParent() override
  1761. {
  1762. curWorkflow = NotFound;
  1763. return false;
  1764. }
  1765. virtual bool isValid() override
  1766. {
  1767. return workflow.isItem(curWorkflow);
  1768. }
  1769. virtual const char * queryScope() const override
  1770. {
  1771. return curScope.str();
  1772. }
  1773. virtual StatisticScopeType getScopeType() const override
  1774. {
  1775. return SSTworkflow;
  1776. }
  1777. virtual void playProperties(IWuScopeVisitor & visitor, WuPropertyTypes whichProperties) override
  1778. {
  1779. if (whichProperties & PTattributes)
  1780. {
  1781. {
  1782. StringBuffer scratchpad;
  1783. Owned<IWorkflowDependencyIterator> depends = workflow.item(curWorkflow).getDependencies();
  1784. ForEach(*depends)
  1785. visitor.noteAttribute(WaIdDependency, getValueText(depends->query(), scratchpad, WorkflowScopePrefix));
  1786. }
  1787. play(visitor, { WaIsScheduled, WaIdSuccess, WaIdFailure, WaIdRecovery, WaIdPersist, WaIdScheduled,
  1788. WaPersistName, WaLabel, WaMode, WaType, WaState, WaCluster, WaCriticalSection });
  1789. }
  1790. }
  1791. virtual bool getStat(StatisticKind kind, unsigned __int64 & value) const override
  1792. {
  1793. return false;
  1794. }
  1795. virtual const char * queryAttribute(WuAttr attr, StringBuffer & scratchpad) const override
  1796. {
  1797. auto wf = &workflow.item(curWorkflow);
  1798. StringBufferAdaptor adaptor(scratchpad);
  1799. switch (attr)
  1800. {
  1801. case WaIdDependencyList:
  1802. {
  1803. bool first = true;
  1804. Owned<IWorkflowDependencyIterator> depends = workflow.item(curWorkflow).getDependencies();
  1805. ForEach(*depends)
  1806. {
  1807. if (first)
  1808. scratchpad.append("[");
  1809. else
  1810. scratchpad.append(",");
  1811. scratchpad.append('"').append(WorkflowScopePrefix).append(depends->query()).append('"');
  1812. first = false;
  1813. }
  1814. if (first)
  1815. return nullptr;
  1816. scratchpad.append("]");
  1817. return scratchpad.str();
  1818. }
  1819. case WaIsScheduled: return trueToStr(wf->isScheduled());
  1820. case WaIdSuccess: return getWfidText(wf->querySuccess(), scratchpad);
  1821. case WaIdFailure: return getWfidText(wf->queryFailure(), scratchpad);
  1822. case WaIdRecovery: return getWfidText(wf->queryRecovery(), scratchpad);
  1823. case WaIdPersist: return getWfidText(wf->queryPersistWfid(), scratchpad);
  1824. case WaIdScheduled: return getWfidText(wf->queryScheduledWfid(), scratchpad);
  1825. case WaPersistName: return queryOptString(wf->getPersistName(adaptor));
  1826. case WaLabel: return queryOptString(wf->getLabel(adaptor));
  1827. case WaMode: return wf->queryMode() != WFModeNormal ? queryWorkflowModeText(wf->queryMode()) : nullptr;
  1828. case WaType: return wf->queryType() != WFTypeNormal ? queryWorkflowTypeText(wf->queryType()) : nullptr;
  1829. case WaState: return queryWorkflowStateText(wf->queryState());
  1830. case WaCluster: return queryOptString(wf->queryCluster(adaptor));
  1831. case WaCriticalSection: return queryOptString(wf->getCriticalName(adaptor));
  1832. /*
  1833. The followng attributes are not generated - I'm not convinced they are very useful, but they could be added later.
  1834. virtual bool isScheduledNow() const = 0;
  1835. virtual IWorkflowEvent * getScheduleEvent() const = 0;
  1836. virtual unsigned querySchedulePriority() const = 0;
  1837. virtual bool hasScheduleCount() const = 0;
  1838. virtual unsigned queryScheduleCount() const = 0;
  1839. virtual unsigned queryRetriesAllowed() const = 0;
  1840. virtual int queryPersistCopies() const = 0; // 0 - unmangled name, < 0 - use default, > 0 - max number
  1841. virtual bool queryPersistRefresh() const = 0;
  1842. virtual unsigned queryScheduleCountRemaining() const = 0;
  1843. virtual unsigned queryRetriesRemaining() const = 0;
  1844. virtual int queryFailCode() const = 0;
  1845. virtual const char * queryFailMessage() const = 0;
  1846. virtual const char * queryEventName() const = 0;
  1847. virtual const char * queryEventExtra() const = 0;
  1848. */
  1849. }
  1850. return nullptr;
  1851. }
  1852. virtual const char * queryHint(const char * kind) const
  1853. {
  1854. return nullptr;
  1855. }
  1856. protected:
  1857. bool initWorkflowItem()
  1858. {
  1859. curScope.clear().append(WorkflowScopePrefix).append(workflow.item(curWorkflow).queryWfid());
  1860. return true;
  1861. }
  1862. const char * getValueText(unsigned value, StringBuffer & scratchpad, const char * prefix = nullptr) const
  1863. {
  1864. return scratchpad.clear().append(prefix).append(value).str();
  1865. }
  1866. const char * queryOptString(IStringVal & value) const
  1867. {
  1868. const char * text = value.str();
  1869. if (!text || !*text)
  1870. return nullptr;
  1871. return text;
  1872. }
  1873. const char * getWfidText(unsigned value, StringBuffer & scratchpad) const
  1874. {
  1875. if (!value)
  1876. return nullptr;
  1877. return scratchpad.clear().append(WorkflowScopePrefix).append(value).str();
  1878. }
  1879. void play(IWuScopeVisitor & visitor, const std::initializer_list<WuAttr> & attrs) const
  1880. {
  1881. StringBuffer scratchpad;
  1882. for (auto attr : attrs)
  1883. {
  1884. const char * value = queryAttribute(attr, scratchpad.clear());
  1885. if (value)
  1886. visitor.noteAttribute(attr, value);
  1887. }
  1888. }
  1889. protected:
  1890. IArrayOf<IConstWorkflowItem> workflow;
  1891. unsigned curWorkflow = 0;
  1892. StringBuffer curScope;
  1893. };
  1894. /*
  1895. * An implementation of IConstWUScopeIterator that combines results from multiple sources.
  1896. */
  1897. class CompoundStatisticsScopeIterator : public CInterfaceOf<IConstWUScopeIterator>
  1898. {
  1899. class VisitorMapper : implements IWuScopeVisitor
  1900. {
  1901. public:
  1902. VisitorMapper(const WuScopeFilter & _filter, IWuScopeVisitor & _visitor) :
  1903. filter(_filter), visitor(_visitor)
  1904. {}
  1905. virtual void noteStatistic(StatisticKind kind, unsigned __int64 value, IConstWUStatistic & extra) override
  1906. {
  1907. if (filter.includeStatistic(kind))
  1908. visitor.noteStatistic(kind, value, extra);
  1909. }
  1910. virtual void noteAttribute(WuAttr attr, const char * value) override
  1911. {
  1912. if (filter.includeAttribute(attr))
  1913. visitor.noteAttribute(attr, value);
  1914. }
  1915. virtual void noteHint(const char * kind, const char * value) override
  1916. {
  1917. if (filter.includeHint(kind))
  1918. visitor.noteHint(kind, value);
  1919. }
  1920. virtual void noteException(IConstWUException & exception) override
  1921. {
  1922. visitor.noteException(exception);
  1923. }
  1924. protected:
  1925. const WuScopeFilter & filter;
  1926. IWuScopeVisitor & visitor;
  1927. };
  1928. public:
  1929. CompoundStatisticsScopeIterator(const WuScopeFilter & _filter) : filter(_filter)
  1930. {
  1931. }
  1932. void addIter(IConstWUScopeIterator * iter)
  1933. {
  1934. if (iter)
  1935. {
  1936. iters.append(OLINK(*iter));
  1937. assertex(iters.ordinality() <= sizeof(activeIterMask)*8);
  1938. }
  1939. }
  1940. virtual bool first() override
  1941. {
  1942. activeIterMask = 0;
  1943. ForEachItemIn(i, iters)
  1944. {
  1945. if (iters.item(i).first())
  1946. activeIterMask |= (1U << i);
  1947. }
  1948. return findNextScope();
  1949. }
  1950. virtual bool next() override
  1951. {
  1952. selectNext();
  1953. return findNextScope();
  1954. }
  1955. virtual bool nextSibling() override
  1956. {
  1957. selectNextSibling();
  1958. return findNextScope();
  1959. }
  1960. virtual bool nextParent() override
  1961. {
  1962. selectNextParent();
  1963. return findNextScope();
  1964. }
  1965. virtual bool isValid() override
  1966. {
  1967. return (activeIterMask != 0);
  1968. }
  1969. virtual const char * queryScope() const override
  1970. {
  1971. return iters.item(firstMatchIter).queryScope();
  1972. }
  1973. virtual StatisticScopeType getScopeType() const override
  1974. {
  1975. return iters.item(firstMatchIter).getScopeType();
  1976. }
  1977. virtual void playProperties(IWuScopeVisitor & visitor, WuPropertyTypes whichProperties) override
  1978. {
  1979. VisitorMapper mappedVisitor(filter, visitor);
  1980. whichProperties &= filter.properties;
  1981. ForEachItemIn(i, iters)
  1982. {
  1983. if (iterMatchesCurrentScope(i))
  1984. iters.item(i).playProperties(mappedVisitor, whichProperties);
  1985. }
  1986. }
  1987. virtual bool getStat(StatisticKind kind, unsigned __int64 & value) const override
  1988. {
  1989. ForEachItemIn(i, iters)
  1990. {
  1991. if (iterMatchesCurrentScope(i))
  1992. {
  1993. if (iters.item(i).getStat(kind, value))
  1994. return true;
  1995. }
  1996. }
  1997. return false;
  1998. }
  1999. virtual const char * queryAttribute(WuAttr attr, StringBuffer & scratchpad) const override
  2000. {
  2001. ForEachItemIn(i, iters)
  2002. {
  2003. if (iterMatchesCurrentScope(i))
  2004. {
  2005. const char * value = iters.item(i).queryAttribute(attr, scratchpad);
  2006. if (value)
  2007. return value;
  2008. }
  2009. }
  2010. return nullptr;
  2011. }
  2012. virtual const char * queryHint(const char * kind) const override
  2013. {
  2014. ForEachItemIn(i, iters)
  2015. {
  2016. if (iterMatchesCurrentScope(i))
  2017. {
  2018. const char * value = iters.item(i).queryHint(kind);
  2019. if (value)
  2020. return value;
  2021. }
  2022. }
  2023. return nullptr;
  2024. }
  2025. inline bool iterMatchesCurrentScope(unsigned i) const { return ((1U << i) & matchIterMask) != 0; }
  2026. inline bool isAlive(unsigned i) const { return ((1U << i) & activeIterMask) != 0; }
  2027. protected:
  2028. //Calculate which iterators contain the scope that should come next
  2029. bool findNextScope()
  2030. {
  2031. for(;;)
  2032. {
  2033. if (activeIterMask == 0)
  2034. return false;
  2035. unsigned mask = 0;
  2036. const char * scope = nullptr;
  2037. ForEachItemIn(i, iters)
  2038. {
  2039. if (isAlive(i))
  2040. {
  2041. const char * iterScope = iters.item(i).queryScope();
  2042. if (mask)
  2043. {
  2044. int compare = compareScopeName(scope, iterScope);
  2045. if (compare == 0)
  2046. {
  2047. mask |= (1U << i);
  2048. }
  2049. else if (compare > 0)
  2050. {
  2051. scope = iterScope;
  2052. mask = (1U << i);
  2053. firstMatchIter = i;
  2054. }
  2055. }
  2056. else
  2057. {
  2058. scope = iterScope;
  2059. mask = (1U << i);
  2060. firstMatchIter = i;
  2061. }
  2062. }
  2063. }
  2064. matchIterMask = mask;
  2065. while (activeScopes)
  2066. {
  2067. const char * activeScope = activeScopes.tos();
  2068. //If the next scope if not a child of one of the active scopes then that active scope will no longer match.
  2069. if (compareScopes(scope, activeScope) & SCchild)
  2070. break;
  2071. activeScopes.pop();
  2072. }
  2073. //The top most scope will be the deepest. Check if the current scope is close enough to return as a match.
  2074. bool include = false;
  2075. if (activeScopes)
  2076. {
  2077. const char * activeScope = activeScopes.tos();
  2078. //code above has ensured that this scope must be a child of (and therefore deeper) than activeScope
  2079. unsigned nesting = queryScopeDepth(scope) - queryScopeDepth(activeScope);
  2080. if (nesting <= filter.include.nestedDepth)
  2081. include = true;
  2082. }
  2083. //Check to see if this is a new match
  2084. if (filter.compareMatchScopes(scope) & SCequal)
  2085. {
  2086. if (!include)
  2087. include = filter.include.matchedScope;
  2088. //Only add it to the list of active scopes if it can match child elements
  2089. if (filter.include.nestedDepth != 0)
  2090. activeScopes.append(scope);
  2091. }
  2092. if (include && !filter.includeScope(scope))
  2093. include = false;
  2094. if (include && filter.requiredStats.size())
  2095. {
  2096. //MORE: This would be cleaner as a member of filter - but it needs access to the stats.
  2097. for (unsigned iReq=0; iReq < filter.requiredStats.size(); iReq++)
  2098. {
  2099. const StatisticValueFilter & cur = filter.requiredStats[iReq];
  2100. unsigned __int64 value;
  2101. if (getStat(cur.queryKind(), value))
  2102. {
  2103. if (!cur.matches(value))
  2104. include = false;
  2105. }
  2106. else
  2107. include = false;
  2108. }
  2109. }
  2110. if (include && filter.requiredAttrs.size())
  2111. {
  2112. //MORE: This would be cleaner as a member of filter - but it needs access to the stats.
  2113. StringBuffer temp;
  2114. for (unsigned iReq=0; iReq < filter.requiredAttrs.size(); iReq++)
  2115. {
  2116. const AttributeValueFilter & cur = filter.requiredAttrs[iReq];
  2117. const char * value = queryAttribute(cur.queryKind(), temp.clear());
  2118. if (!value || !cur.matches(value))
  2119. {
  2120. include = false;
  2121. break;
  2122. }
  2123. }
  2124. }
  2125. if (include)
  2126. return true;
  2127. //MORE: Optimize based on filter.compareScope()
  2128. selectNext();
  2129. }
  2130. }
  2131. void checkScopeOrder(unsigned input, const char * prevScope)
  2132. {
  2133. if (isAlive(input))
  2134. {
  2135. const char * curScope = iters.item(input).queryScope();
  2136. int compare = compareScopeName(prevScope, curScope);
  2137. if (compare >= 0)
  2138. throw MakeStringException(0, "Out of order (%u) scopes %s,%s = %d", input, prevScope, curScope, compare);
  2139. }
  2140. }
  2141. void selectNext()
  2142. {
  2143. ForEachItemIn(i, iters)
  2144. {
  2145. if (iterMatchesCurrentScope(i))
  2146. {
  2147. #ifdef _DEBUG
  2148. StringBuffer prevScope(iters.item(i).queryScope());
  2149. #endif
  2150. if (!iters.item(i).next())
  2151. activeIterMask &= ~(1U << i);
  2152. #ifdef _DEBUG
  2153. checkScopeOrder(i, prevScope);
  2154. #endif
  2155. }
  2156. }
  2157. }
  2158. void selectNextSibling()
  2159. {
  2160. ForEachItemIn(i, iters)
  2161. {
  2162. if (iterMatchesCurrentScope(i))
  2163. {
  2164. #ifdef _DEBUG
  2165. StringBuffer prevScope(iters.item(i).queryScope());
  2166. #endif
  2167. if (!iters.item(i).nextSibling())
  2168. activeIterMask &= ~(1U << i);
  2169. #ifdef _DEBUG
  2170. checkScopeOrder(i, prevScope);
  2171. #endif
  2172. }
  2173. }
  2174. }
  2175. void selectNextParent()
  2176. {
  2177. ForEachItemIn(i, iters)
  2178. {
  2179. if (iterMatchesCurrentScope(i))
  2180. {
  2181. #ifdef _DEBUG
  2182. StringBuffer prevScope(iters.item(i).queryScope());
  2183. #endif
  2184. if (!iters.item(i).nextParent())
  2185. activeIterMask &= ~(1U << i);
  2186. #ifdef _DEBUG
  2187. checkScopeOrder(i, prevScope);
  2188. #endif
  2189. }
  2190. }
  2191. }
  2192. protected:
  2193. const WuScopeFilter & filter;
  2194. IArrayOf<IConstWUScopeIterator> iters;
  2195. unsigned curIter = 0;
  2196. unsigned firstMatchIter = 0;
  2197. unsigned matchIterMask = 0; // bit set of iterators which have a valid entry for the current scope
  2198. unsigned activeIterMask = 0; // bit set of iterators which are still active
  2199. StringArray activeScopes;
  2200. };
  2201. //---------------------------------------------------------------------------------------------------------------------
  2202. class AggregateFilter
  2203. {
  2204. public:
  2205. AggregateFilter(StatisticKind _search) : search(_search) {}
  2206. inline bool matches(StatisticKind kind, unsigned __int64 value, IConstWUStatistic & extra) const
  2207. {
  2208. return (kind == search);
  2209. }
  2210. protected:
  2211. StatisticKind search;
  2212. //MORE: Allow filtering by scope?
  2213. };
  2214. class StatisticAggregator : public CInterfaceOf<IWuScopeVisitor>
  2215. {
  2216. public:
  2217. StatisticAggregator(StatisticKind _search) : filter(_search) {}
  2218. virtual void noteAttribute(WuAttr attr, const char * value) override { throwUnexpected(); }
  2219. virtual void noteHint(const char * kind, const char * value) override { throwUnexpected(); }
  2220. virtual void noteException(IConstWUException & exception) { throwUnexpected();}
  2221. protected:
  2222. AggregateFilter filter;
  2223. };
  2224. class SimpleAggregator : public StatisticAggregator
  2225. {
  2226. public:
  2227. SimpleAggregator(StatisticKind _search) : StatisticAggregator(_search) {}
  2228. virtual void noteStatistic(StatisticKind kind, unsigned __int64 value, IConstWUStatistic & extra) override
  2229. {
  2230. if (filter.matches(kind, value, extra))
  2231. summary.noteValue(value);
  2232. }
  2233. //How should these be reported? Should there be a playAggregates(IWuAggregatedScopeVisitor)
  2234. //with a noteAggregate(value, variant, value, grouping)?
  2235. protected:
  2236. StatsAggregation summary;
  2237. };
  2238. class SimpleReferenceAggregator : public StatisticAggregator
  2239. {
  2240. public:
  2241. SimpleReferenceAggregator(StatisticKind _search, StatsAggregation & _summary) : StatisticAggregator(_search), summary(_summary) {}
  2242. virtual void noteStatistic(StatisticKind kind, unsigned __int64 value, IConstWUStatistic & extra) override
  2243. {
  2244. if (filter.matches(kind, value, extra))
  2245. summary.noteValue(value);
  2246. }
  2247. //How should these be reported? Should there be a playAggregates(IWuAggregatedScopeVisitor)
  2248. //with a noteAggregate(value, variant, value, grouping)?
  2249. protected:
  2250. StatsAggregation & summary;
  2251. };
  2252. class GroupedAggregator : public StatisticAggregator
  2253. {
  2254. public:
  2255. virtual void noteStatistic(StatisticKind kind, unsigned __int64 value, IConstWUStatistic & extra) override
  2256. {
  2257. if (filter.matches(kind, value, extra))
  2258. {
  2259. StatsAggregation & match = summary; // look up in hash table.
  2260. match.noteValue(value);
  2261. }
  2262. }
  2263. protected:
  2264. //HashTable of (possible groupings->summary)
  2265. StatsAggregation summary;
  2266. };
  2267. class CompoundAggregator : implements StatisticAggregator
  2268. {
  2269. public:
  2270. virtual void noteStatistic(StatisticKind kind, unsigned __int64 value, IConstWUStatistic & extra)
  2271. {
  2272. ForEachItemIn(i, aggregators)
  2273. aggregators.item(i).noteStatistic(kind, value, extra);
  2274. }
  2275. protected:
  2276. IArrayOf<StatisticAggregator> aggregators;
  2277. };
  2278. // Aggregate costs excluding all hThor costs
  2279. // Note: the only reason that this class is required is that it is not possible to determine the creator
  2280. // of a scope when iterating with IConstWUScopeIterator. (When this functionlity becomes available,
  2281. // consider filtering within aggregateCost and eliminating this class.)
  2282. class CostAggregatorExcludeHThor : public CInterfaceOf<IWuScopeVisitor>
  2283. {
  2284. public:
  2285. virtual void noteStatistic(StatisticKind kind, unsigned __int64 value, IConstWUStatistic & extra) override
  2286. {
  2287. if (extra.getCreatorType() != SCThthor)
  2288. totalCost += value;
  2289. }
  2290. virtual void noteAttribute(WuAttr attr, const char * value) override { throwUnexpected(); }
  2291. virtual void noteHint(const char * kind, const char * value) override { throwUnexpected(); }
  2292. virtual void noteException(IConstWUException & exception) { throwUnexpected(); }
  2293. virtual cost_type getTotalCost() const { return totalCost; }
  2294. protected:
  2295. cost_type totalCost = 0;
  2296. };
  2297. // Aggregrate Thor or hThor costs
  2298. cost_type aggregateCost(const IConstWorkUnit * wu, const char *scope, bool excludeHThor)
  2299. {
  2300. // depth 1: workflow, depth 2: graph, depth 3: subgraph
  2301. WuScopeFilter filter;
  2302. if (scope && *scope) // All costs under specified scope (used to calculate all costs for a workflow)
  2303. {
  2304. filter.addScope(scope);
  2305. filter.setIncludeNesting(3);
  2306. filter.addOutputStatistic(StCostExecute);
  2307. filter.addRequiredStat(StCostExecute);
  2308. }
  2309. else
  2310. filter.addFilter("stat[CostExecute],depth[1..3],nested[0],where[CostExecute]");
  2311. filter.addSource("global");
  2312. filter.finishedFilter();
  2313. Owned<IConstWUScopeIterator> it = &wu->getScopeIterator(filter);
  2314. // Note: use of CostAggregatorExcludeHThor will be a slower, so use only when necessary
  2315. if (excludeHThor)
  2316. {
  2317. CostAggregatorExcludeHThor aggregator;
  2318. for (it->first(); it->isValid(); )
  2319. {
  2320. it->playProperties(aggregator);
  2321. stat_type value;
  2322. if (it->getStat(StCostExecute, value))
  2323. it->nextSibling();
  2324. else
  2325. it->next();
  2326. }
  2327. return aggregator.getTotalCost();
  2328. }
  2329. else
  2330. {
  2331. cost_type totalCost = 0;
  2332. for (it->first(); it->isValid(); )
  2333. {
  2334. stat_type value = 0;
  2335. if (it->getStat(StCostExecute, value))
  2336. {
  2337. totalCost += value;
  2338. it->nextSibling();
  2339. }
  2340. else
  2341. it->next();
  2342. }
  2343. return totalCost;
  2344. }
  2345. }
  2346. //aggregate disk costs from top-level subgraphs (when scope specified) or workflows (scope not specified)
  2347. cost_type aggregateDiskAccessCost(const IConstWorkUnit * wu, const char *scope)
  2348. {
  2349. WuScopeFilter filter;
  2350. if (!isEmptyString(scope))
  2351. filter.addScope(scope);
  2352. else
  2353. filter.addScope(""); // Needed to match scope
  2354. // when scope is a workflow, sum graph costs (or subgraph cost when no graph cost) to get workflow cost
  2355. // (Costs from child graphs and activities should have been summed up to graph/subgraph level already)
  2356. // when isEmptyString(scope), sum workflow costs (or graph cost when no workflow cost) to get global cost
  2357. // (Costs from all levels below graph should be summed upto at least graph level already)
  2358. // i.e. need 2 levels of nesting
  2359. filter.setIncludeNesting(2);
  2360. // includeNesting(2) needs just source "global". However, WuScopeFilter is incorrectly inferring the source as "global,stats",
  2361. // causing too many of the stats to be pulled in and inefficiency. Here, explicitly set source to "global"
  2362. filter.addSource("global");
  2363. filter.addOutputStatistic(StCostFileAccess);
  2364. filter.addRequiredStat(StCostFileAccess);
  2365. filter.finishedFilter();
  2366. Owned<IConstWUScopeIterator> it = &wu->getScopeIterator(filter);
  2367. cost_type totalCost = 0;
  2368. for (it->first(); it->isValid(); )
  2369. {
  2370. cost_type value = 0;
  2371. if (it->getStat(StCostFileAccess, value))
  2372. {
  2373. totalCost += value;
  2374. it->nextSibling();
  2375. }
  2376. else
  2377. {
  2378. it->next();
  2379. }
  2380. }
  2381. return totalCost;
  2382. }
  2383. //---------------------------------------------------------------------------------------------------------------------
  2384. //Extract an argument of the format abc[def[ghi...,]],... as (abc,def[...])
  2385. static bool extractOption(const char * & finger, StringBuffer & option, StringBuffer & arg)
  2386. {
  2387. const char * start = finger;
  2388. if (!*start)
  2389. return false;
  2390. const char * cur = start;
  2391. const char * bra = nullptr;
  2392. const char * end = nullptr;
  2393. unsigned braDepth = 0;
  2394. char next;
  2395. arg.clear();
  2396. for (;;)
  2397. {
  2398. next = *cur;
  2399. if (!next || ((next == ',') && (braDepth == 0)))
  2400. break;
  2401. switch (next)
  2402. {
  2403. case '[':
  2404. if (braDepth == 0)
  2405. {
  2406. if (bra)
  2407. throw makeStringExceptionV(0, "Multiple [ in filter : %s", bra);
  2408. bra = cur;
  2409. }
  2410. braDepth++;
  2411. break;
  2412. case ']':
  2413. if (braDepth > 0)
  2414. {
  2415. if (--braDepth == 0)
  2416. {
  2417. end = cur;
  2418. arg.append(cur - (bra+1), bra+1);
  2419. }
  2420. }
  2421. break;
  2422. }
  2423. cur++;
  2424. }
  2425. if (braDepth != 0)
  2426. throw makeStringExceptionV(0, "Mismatched ] in filter : %s", start);
  2427. option.clear();
  2428. if (bra)
  2429. {
  2430. if (cur != end+1)
  2431. throw makeStringExceptionV(0, "Text follows closing bracket: %s", end);
  2432. option.append(bra-start, start);
  2433. }
  2434. else
  2435. option.append(cur-start, start);
  2436. if (next)
  2437. finger = cur+1;
  2438. else
  2439. finger = cur;
  2440. return true;
  2441. }
  2442. static unsigned readOptValue(const char * start, const char * end, unsigned dft, const char * type)
  2443. {
  2444. if (start == end)
  2445. return dft;
  2446. char * next;
  2447. unsigned value = (unsigned)strtoll(start, &next, 10);
  2448. if (next != end)
  2449. throw makeStringExceptionV(0, "Unexpected characters in %s option '%s'", type, next);
  2450. return value;
  2451. }
  2452. static unsigned readValue(const char * start, const char * type)
  2453. {
  2454. if (*start == '\0')
  2455. throw makeStringExceptionV(0, "Expected a value for the %s option", type);
  2456. char * next;
  2457. unsigned value = (unsigned)strtoll(start, &next, 10);
  2458. if (*next != '\0')
  2459. throw makeStringExceptionV(0, "Unexpected characters in %s option '%s'", type, next);
  2460. return value;
  2461. }
  2462. StringBuffer & AttributeValueFilter::describe(StringBuffer & out) const
  2463. {
  2464. out.append(queryWuAttributeName(attr));
  2465. if (value)
  2466. out.append("=").append(value);
  2467. return out;
  2468. }
  2469. /*
  2470. Scope service matching Syntax: * indicates
  2471. Which items are matched:
  2472. scope[<scope-id>]* | stype[<scope-type>]* | id[<id>]* - which scopes should be matched?
  2473. depth[n | low..high] - range of depths to search for a match
  2474. source[global|stats|graph|all]* - which sources to search within the workunit
  2475. where[<statistickind> | <statistickind> (=|<|<=|>|>=) value | <statistickind>=low..high] - a statistic filter
  2476. Which items are include in the results:
  2477. matched[true|false] - are the matched scopes returned?
  2478. nested[<depth>|all] - how deep within a scope should be matched (default = 0 if matched[true], all if matched[false])
  2479. includetype[<scope-type>] - which scope types should be included?
  2480. Which properties of the items are returned:
  2481. properties[statistics|hints|attributes|scope|all]
  2482. statistic[<statistic-kind>|none|all] - include statistic
  2483. attribute[<attribute-name>|none|all] - include attribute
  2484. hint[<hint-name>] - include hint
  2485. property[<statistic-kind>|<attribute-name>|<hint-name>] - include property
  2486. measure[<measure>] - all statistics with a particular measure
  2487. version[<version>] - minimum version to return
  2488. */
  2489. enum { FOscope, FOstype, FOid, FOdepth, FOsource, FOwhere, FOmatched, FOnested, FOinclude, FOproperties, FOstatistic, FOattribute, FOhint, FOproperty, FOmeasure, FOversion, FOunknown };
  2490. //Some of the following contains aliases for the same option e.g. stat and statistic
  2491. static constexpr EnumMapping filterOptions[] = {
  2492. { FOscope, "scope" }, { FOstype, "stype" }, { FOid, "id" },
  2493. { FOdepth, "depth" }, { FOsource, "source" }, { FOwhere, "where" },
  2494. { FOmatched, "matched" }, { FOnested, "nested" },
  2495. { FOinclude, "include" }, { FOinclude, "includetype" },
  2496. { FOproperties, "props" }, { FOproperties, "properties" }, // some aliases
  2497. { FOstatistic, "stat" }, { FOstatistic, "statistic" }, { FOattribute, "attr" }, { FOattribute, "attribute" }, { FOhint, "hint" },
  2498. { FOproperty, "prop" }, { FOproperty, "property" },
  2499. { FOmeasure, "measure" }, { FOversion, "version" }, { 0, nullptr} };
  2500. static constexpr EnumMapping sourceMappings[] = {
  2501. { SSFsearchGlobalStats, "global" }, { SSFsearchGraphStats, "stats" }, { SSFsearchGraphStats, "statistics" }, { SSFsearchGraph, "graph" }, { SSFsearchExceptions, "exception" }, { SSFsearchWorkflow, "workflow" },
  2502. { (int)SSFsearchAll, "all" }, { 0, nullptr } };
  2503. static constexpr EnumMapping propertyMappings[] = {
  2504. { PTstatistics, "stat" }, { PTstatistics, "statistic" }, { PTattributes, "attr" }, { PTattributes, "attribute" }, { PThints, "hint" },{ PTnotes, "note" },
  2505. { PTstatistics, "stats" }, { PTstatistics, "statistics" }, { PTattributes, "attrs" }, { PTattributes, "attributes" }, { PThints, "hints" }, { PTnotes, "notes" },
  2506. { PTnone, "none" }, { PTscope, "scope" }, { PTall, "all" }, { 0, nullptr } };
  2507. WuScopeSourceFlags querySource(const char * source) { return (WuScopeSourceFlags)getEnum(source, sourceMappings, SSFunknown); }
  2508. const char * querySourceText(WuScopeSourceFlags source) { return getEnumText(source, sourceMappings, nullptr); }
  2509. WuScopeFilter::WuScopeFilter(const char * filter)
  2510. {
  2511. addFilter(filter);
  2512. finishedFilter();
  2513. }
  2514. WuScopeFilter & WuScopeFilter::addFilter(const char * filter)
  2515. {
  2516. checkModifiable();
  2517. if (!filter)
  2518. return *this;
  2519. StringBuffer option;
  2520. StringBuffer arg;
  2521. while (extractOption(filter, option, arg))
  2522. {
  2523. switch (getEnum(option, filterOptions, FOunknown))
  2524. {
  2525. case FOscope:
  2526. addScope(arg);
  2527. break;
  2528. case FOstype:
  2529. addScopeType(arg);
  2530. break;
  2531. case FOid:
  2532. addId(arg);
  2533. break;
  2534. case FOdepth:
  2535. {
  2536. //Allow depth[n], depth[a,b] or depth[a..b]
  2537. const char * comma = strchr(arg, ',');
  2538. const char * dotdot = strstr(arg, "..");
  2539. if (comma)
  2540. {
  2541. unsigned low = readOptValue(arg, comma, 0, "depth");
  2542. if (comma[1])
  2543. {
  2544. scopeFilter.setDepth(low, readValue(comma+1, "depth"));
  2545. }
  2546. else
  2547. scopeFilter.setDepth(low, UINT_MAX);
  2548. }
  2549. else if (dotdot)
  2550. {
  2551. unsigned low = readOptValue(arg, dotdot, 0, "depth");
  2552. if (dotdot[2])
  2553. scopeFilter.setDepth(low, readValue(dotdot+2, "depth"));
  2554. else
  2555. scopeFilter.setDepth(low, UINT_MAX);
  2556. }
  2557. else
  2558. {
  2559. scopeFilter.setDepth(readValue(arg, "depth"));
  2560. }
  2561. break;
  2562. }
  2563. case FOsource:
  2564. addSource(arg);
  2565. break;
  2566. case FOwhere: // where[stat<op>value]
  2567. addRequiredStat(arg);
  2568. break;
  2569. case FOmatched:
  2570. setIncludeMatch(strToBool(arg));
  2571. break;
  2572. case FOnested:
  2573. if (strieq(arg, "all"))
  2574. setIncludeNesting(UINT_MAX);
  2575. else if (isdigit(*arg))
  2576. setIncludeNesting(atoi(arg));
  2577. else
  2578. throw makeStringExceptionV(0, "Expected a value for the nesting depth: %s", arg.str());
  2579. break;
  2580. case FOinclude:
  2581. setIncludeScopeType(arg);
  2582. break;
  2583. case FOproperties:
  2584. {
  2585. WuPropertyTypes prop = (WuPropertyTypes)getEnum(arg, propertyMappings, PTunknown);
  2586. if (prop == PTunknown)
  2587. throw makeStringExceptionV(0, "Unexpected properties '%s'", arg.str());
  2588. addOutputProperties(prop);
  2589. break;
  2590. }
  2591. case FOstatistic:
  2592. addOutputStatistic(arg);
  2593. break;
  2594. case FOattribute:
  2595. addOutputAttribute(arg);
  2596. break;
  2597. case FOhint:
  2598. addOutputHint(arg);
  2599. break;
  2600. case FOproperty:
  2601. addOutput(arg);
  2602. break;
  2603. case FOmeasure:
  2604. setMeasure(arg);
  2605. break;
  2606. case FOversion:
  2607. if (isdigit(*arg))
  2608. minVersion = atoi64(arg);
  2609. else
  2610. throw makeStringExceptionV(0, "Expected a value for the version: %s", arg.str());
  2611. break;
  2612. default:
  2613. throw makeStringExceptionV(0, "Unrecognised filter option: %s", option.str());
  2614. }
  2615. }
  2616. return *this;
  2617. }
  2618. WuScopeFilter & WuScopeFilter::addScope(const char * scope)
  2619. {
  2620. checkModifiable();
  2621. if (scope)
  2622. {
  2623. validateScope(scope);
  2624. scopeFilter.addScope(scope);
  2625. }
  2626. return *this;
  2627. }
  2628. WuScopeFilter & WuScopeFilter::addScopeType(StatisticScopeType scopeType)
  2629. {
  2630. checkModifiable();
  2631. scopeFilter.addScopeType(scopeType);
  2632. return *this;
  2633. }
  2634. WuScopeFilter & WuScopeFilter::addScopeType(const char * scopeType)
  2635. {
  2636. checkModifiable();
  2637. if (scopeType)
  2638. {
  2639. StatisticScopeType sst = queryScopeType(scopeType, SSTmax);
  2640. if (sst == SSTmax)
  2641. throw makeStringExceptionV(0, "Unrecognised scope type '%s'", scopeType);
  2642. scopeFilter.addScopeType(sst);
  2643. }
  2644. return *this;
  2645. }
  2646. WuScopeFilter & WuScopeFilter::addId(const char * id)
  2647. {
  2648. checkModifiable();
  2649. validateScopeId(id);
  2650. scopeFilter.addId(id);
  2651. return *this;
  2652. }
  2653. WuScopeFilter & WuScopeFilter::addOutput(const char * prop)
  2654. {
  2655. checkModifiable();
  2656. WuAttr attr = queryWuAttribute(prop, WaNone);
  2657. if (attr != WaNone)
  2658. {
  2659. WuAttr singleKindAttr = getSingleKindOfListAttribute(attr);
  2660. if (singleKindAttr != WaNone)
  2661. addOutputAttribute(singleKindAttr);
  2662. else
  2663. addOutputAttribute(attr);
  2664. } else if (queryStatisticKind(prop, StMax) != StMax)
  2665. addOutputStatistic(prop);
  2666. else
  2667. addOutputHint(prop);
  2668. return *this;
  2669. }
  2670. WuScopeFilter & WuScopeFilter::addOutputStatistic(const char * prop)
  2671. {
  2672. checkModifiable();
  2673. if (!prop)
  2674. return *this;
  2675. StatisticKind kind = queryStatisticKind(prop, StMax);
  2676. if (kind == StMax)
  2677. throw makeStringExceptionV(0, "Unrecognised statistic '%s'", prop);
  2678. return addOutputStatistic(kind);
  2679. }
  2680. WuScopeFilter & WuScopeFilter::addOutputStatistic(StatisticKind stat)
  2681. {
  2682. checkModifiable();
  2683. if (stat != StKindNone)
  2684. {
  2685. if (stat != StKindAll)
  2686. desiredStats.append(stat);
  2687. else
  2688. desiredStats.kill();
  2689. properties |= PTstatistics;
  2690. }
  2691. else
  2692. properties &= ~PTstatistics;
  2693. return *this;
  2694. }
  2695. WuScopeFilter & WuScopeFilter::addOutputAttribute(const char * prop)
  2696. {
  2697. checkModifiable();
  2698. if (!prop)
  2699. return *this;
  2700. WuAttr attr = queryWuAttribute(prop, WaMax);
  2701. if (attr == WaMax)
  2702. throw makeStringExceptionV(0, "Unrecognised attribute '%s'", prop);
  2703. return addOutputAttribute(attr);
  2704. }
  2705. WuScopeFilter & WuScopeFilter::addOutputAttribute(WuAttr attr)
  2706. {
  2707. checkModifiable();
  2708. if (attr != WaNone)
  2709. {
  2710. if (attr != WaAll)
  2711. desiredAttrs.append(attr);
  2712. else
  2713. desiredAttrs.kill();
  2714. properties |= PTattributes;
  2715. }
  2716. else
  2717. properties &= ~PTattributes;
  2718. return *this;
  2719. }
  2720. WuScopeFilter & WuScopeFilter::addOutputHint(const char * prop)
  2721. {
  2722. checkModifiable();
  2723. if (strieq(prop, "none"))
  2724. {
  2725. desiredHints.kill();
  2726. properties &= ~PThints;
  2727. }
  2728. else
  2729. {
  2730. if (!strieq(prop, "all"))
  2731. desiredHints.append(prop);
  2732. else
  2733. desiredHints.kill();
  2734. properties |= PThints;
  2735. }
  2736. return *this;
  2737. }
  2738. WuScopeFilter & WuScopeFilter::setIncludeMatch(bool value)
  2739. {
  2740. checkModifiable();
  2741. include.matchedScope = value;
  2742. return *this;
  2743. }
  2744. WuScopeFilter & WuScopeFilter::setIncludeNesting(unsigned depth)
  2745. {
  2746. checkModifiable();
  2747. include.nestedDepth = depth;
  2748. return *this;
  2749. }
  2750. WuScopeFilter & WuScopeFilter::setIncludeScopeType(const char * scopeType)
  2751. {
  2752. checkModifiable();
  2753. if (scopeType)
  2754. {
  2755. StatisticScopeType sst = queryScopeType(scopeType, SSTmax);
  2756. if (sst == SSTmax)
  2757. throw makeStringExceptionV(0, "Unrecognised scope type '%s'", scopeType);
  2758. include.scopeTypes.append(sst);
  2759. }
  2760. return *this;
  2761. }
  2762. WuScopeFilter & WuScopeFilter::setMeasure(const char * measure)
  2763. {
  2764. checkModifiable();
  2765. if (measure)
  2766. {
  2767. desiredMeasure = queryMeasure(measure, SMeasureNone);
  2768. if (desiredMeasure == SMeasureNone)
  2769. throw makeStringExceptionV(0, "Unrecognised measure '%s'", measure);
  2770. properties |= PTstatistics;
  2771. }
  2772. return *this;
  2773. }
  2774. WuScopeFilter & WuScopeFilter::addOutputProperties(WuPropertyTypes mask)
  2775. {
  2776. checkModifiable();
  2777. if (properties == PTnone)
  2778. properties = mask;
  2779. else
  2780. properties |= mask;
  2781. return *this;
  2782. }
  2783. WuScopeFilter & WuScopeFilter::addRequiredStat(StatisticKind statKind, stat_type lowValue, stat_type highValue)
  2784. {
  2785. checkModifiable();
  2786. requiredStats.emplace_back(statKind, lowValue, highValue);
  2787. return *this;
  2788. }
  2789. WuScopeFilter & WuScopeFilter::addRequiredStat(StatisticKind statKind)
  2790. {
  2791. checkModifiable();
  2792. requiredStats.emplace_back(statKind, 0, MaxStatisticValue);
  2793. return *this;
  2794. }
  2795. WuScopeFilter & WuScopeFilter::addRequiredAttr(WuAttr attr, const char * value)
  2796. {
  2797. checkModifiable();
  2798. requiredAttrs.emplace_back(attr, value);
  2799. return *this;
  2800. }
  2801. //process a filter in one of the following forms:
  2802. // <statistic-name>
  2803. // <statistic-name> (=|<|<=|>|>=) <value>
  2804. // <statistic-name>=[<low>]..[<high>]
  2805. void WuScopeFilter::addRequiredStat(const char * filter)
  2806. {
  2807. checkModifiable();
  2808. const char * stat = filter;
  2809. const char * cur = stat;
  2810. while (isalpha(*cur))
  2811. cur++;
  2812. StringBuffer statisticName(cur-stat, stat);
  2813. //Skip any spaces before a comparison operator.
  2814. while (*cur && isspace(*cur))
  2815. cur++;
  2816. StatisticKind statKind = queryStatisticKind(statisticName, StKindNone);
  2817. if (statKind == StKindNone)
  2818. {
  2819. WuAttr attr = queryWuAttribute(statisticName, WaNone);
  2820. if (attr == WaNone)
  2821. throw makeStringExceptionV(0, "Unknown property name '%s'", statisticName.str());
  2822. if (*cur == '=')
  2823. requiredAttrs.emplace_back(attr, cur+1);
  2824. else if (*cur == '\0')
  2825. requiredAttrs.emplace_back(attr, nullptr);
  2826. else
  2827. throw makeStringExceptionV(0, "Unknown attribute comparison '%s'", cur);
  2828. return;
  2829. }
  2830. //Save the operator, and skip over any non digits.
  2831. const char * op = cur;
  2832. switch (*op)
  2833. {
  2834. case '=':
  2835. cur++;
  2836. break;
  2837. case '<':
  2838. case '>':
  2839. if (op[1] == '=')
  2840. cur += 2;
  2841. else
  2842. cur++;
  2843. break;
  2844. case '\0':
  2845. break;
  2846. default:
  2847. throw makeStringExceptionV(0, "Unknown comparison '%s'", op);
  2848. }
  2849. const char * next;
  2850. stat_type value = readStatisticValue(cur, &next, queryMeasure(statKind));
  2851. stat_type lowValue = 0;
  2852. stat_type highValue = MaxStatisticValue;
  2853. switch (op[0])
  2854. {
  2855. case '=':
  2856. {
  2857. //Allow a,b or a..b to specify a range - either bound may be omitted.
  2858. if (next[0] == ',')
  2859. {
  2860. lowValue = value;
  2861. next++;
  2862. if (*next != '\0')
  2863. highValue = readStatisticValue(next, &next, queryMeasure(statKind));
  2864. }
  2865. else if (strncmp(next, "..", 2) == 0)
  2866. {
  2867. lowValue = value;
  2868. next += 2;
  2869. if (*next != '\0')
  2870. highValue = readStatisticValue(next, &next, queryMeasure(statKind));
  2871. }
  2872. else
  2873. {
  2874. lowValue = value;
  2875. highValue = value;
  2876. }
  2877. break;
  2878. }
  2879. case '<':
  2880. if (op[1] == '=')
  2881. highValue = value;
  2882. else
  2883. highValue = value-1;
  2884. break;
  2885. case '>':
  2886. if (op[1] == '=')
  2887. lowValue = value;
  2888. else
  2889. lowValue = value+1;
  2890. break;
  2891. }
  2892. if (*next)
  2893. throw makeStringExceptionV(0, "Trailing characters in where '%s'", next);
  2894. requiredStats.emplace_back(statKind, lowValue, highValue);
  2895. }
  2896. WuScopeFilter & WuScopeFilter::addSource(const char * source)
  2897. {
  2898. checkModifiable();
  2899. WuScopeSourceFlags mask = querySource(source);
  2900. if (mask == SSFunknown)
  2901. throw makeStringExceptionV(0, "Unexpected source '%s'", source);
  2902. if (!mask)
  2903. sourceFlags = mask;
  2904. else
  2905. sourceFlags |= mask;
  2906. return *this;
  2907. }
  2908. WuScopeFilter & WuScopeFilter::setDepth(unsigned low, unsigned high)
  2909. {
  2910. checkModifiable();
  2911. scopeFilter.setDepth(low, high);
  2912. return *this;
  2913. }
  2914. bool WuScopeFilter::matchOnly(StatisticScopeType scopeType) const
  2915. {
  2916. //If there is a post filter on the scopeType, then check if it matches
  2917. if (include.scopeTypes.ordinality() == 1)
  2918. return (include.scopeTypes.item(0) == scopeType);
  2919. //If filter doesn't match nested items, then check if the scope filter matches.
  2920. if (include.nestedDepth == 0)
  2921. {
  2922. if (scopeFilter.matchOnly(scopeType))
  2923. return true;
  2924. }
  2925. return false;
  2926. }
  2927. void WuScopeFilter::reportModifyTooLate()
  2928. {
  2929. throw makeStringException(WUERR_ModifyFilterAfterFinalize, "Attempting to modify WuScopefilter after finish() has been called");
  2930. }
  2931. //Called once the filter has been updated to optimize the filter
  2932. void WuScopeFilter::finishedFilter()
  2933. {
  2934. if (optimized)
  2935. throw makeStringException(WUERR_FinalizeAfterFinalize, "Calling finishedFilter() more than once");
  2936. scopeFilter.finishedFilter();
  2937. if ((include.nestedDepth == 0) && !include.matchedScope)
  2938. include.nestedDepth = UINT_MAX;
  2939. preFilterScope = include.matchedScope && (include.nestedDepth == 0);
  2940. if (scopeFilter.canAlwaysPreFilter())
  2941. preFilterScope = true;
  2942. //If the source flags have not been explicitly set then calculate which sources will provide the results
  2943. if (!sourceFlags)
  2944. {
  2945. sourceFlags = SSFsearchAll;
  2946. //Use the other options to reduce the number of elements that are searched.
  2947. //If not interested in scopes on their own then...
  2948. if (!(properties & PTscope))
  2949. {
  2950. //Global stats and graph stats only contain stats => remove if not interested in them
  2951. if (!(properties & PTstatistics) && (requiredStats.size() == 0))
  2952. sourceFlags &= ~(SSFsearchGlobalStats|SSFsearchGraphStats);
  2953. //graph, workflow only contains attributes and hints => remove if not interested
  2954. if (!(properties & (PTattributes|PThints)) && (requiredAttrs.size() == 0))
  2955. sourceFlags &= ~(SSFsearchGraph|SSFsearchWorkflow);
  2956. }
  2957. if (!(properties & PTnotes))
  2958. sourceFlags &= ~SSFsearchExceptions;
  2959. }
  2960. //Optimize sources if they haven't been explicitly specified
  2961. //Most of the following are dependent on internal knowledge of the way that information is represented
  2962. if (matchOnly(SSTworkflow))
  2963. {
  2964. //Workflow is not nested within a graph
  2965. sourceFlags &= ~(SSFsearchGraph|SSFsearchGraphStats);
  2966. setDepth(1, 1);
  2967. }
  2968. else if (matchOnly(SSTgraph))
  2969. {
  2970. //Graph starts are stored globally, not in the graph stats.
  2971. sourceFlags &= ~(SSFsearchGraphStats|SSFsearchWorkflow);
  2972. if (!(properties & (PTattributes|PThints)))
  2973. sourceFlags &= ~(SSFsearchGraph);
  2974. //This should really be setDepth(2,2) but workunits prior to 7.4 did not have graph ids prefixed by the wfid
  2975. //Remove once 7.2 is a distant memory (see HPCC-22887)
  2976. setDepth(1, 2);
  2977. }
  2978. else if (matchOnly(SSTsubgraph))
  2979. {
  2980. sourceFlags &= ~(SSFsearchWorkflow);
  2981. }
  2982. else if (matchOnly(SSTcompilestage))
  2983. {
  2984. //compile stages are not stored in the graph
  2985. sourceFlags &= ~(SSFsearchGraphStats|SSFsearchGraph|SSFsearchWorkflow);
  2986. }
  2987. else if (matchOnly(SSTactivity))
  2988. {
  2989. //information about activities is not stored globally
  2990. sourceFlags &= ~(SSFsearchGlobalStats|SSFsearchWorkflow);
  2991. }
  2992. // Everything stored in the graphs stats has a depth of 3 or more - so ignore if there are not matches in that depth
  2993. if (include.nestedDepth == 0)
  2994. {
  2995. if (scopeFilter.compareDepth(3) > 0) // 3 is larger than any scope in the filter
  2996. sourceFlags &= ~(SSFsearchGraphStats);
  2997. else if (scopeFilter.compareDepth(3) == 0) // 3 matches the maximum scope in the filter
  2998. {
  2999. //Subgraph WhenStarted and TimeElapsed are stored globally. If that is all that is required
  3000. //then do not include the subgraph timings
  3001. if (desiredStats.ordinality())
  3002. {
  3003. bool needGraphStats = false;
  3004. ForEachItemIn(i, desiredStats)
  3005. {
  3006. unsigned stat = desiredStats.item(i);
  3007. if ((stat != StWhenStarted) && (stat != StTimeElapsed))
  3008. needGraphStats = true;
  3009. }
  3010. if (!needGraphStats)
  3011. {
  3012. sourceFlags &= ~(SSFsearchGraphStats);
  3013. if (matchOnly(SSTsubgraph))
  3014. sourceFlags &= SSFsearchGlobalStats;
  3015. }
  3016. }
  3017. }
  3018. }
  3019. if (scopeFilter.compareDepth(1) < 0)
  3020. {
  3021. //If minimum match depth is > 1 then it will never match workflow
  3022. sourceFlags &= ~(SSFsearchWorkflow);
  3023. }
  3024. //The xml graph is never updated, so do not check it if minVersion != 0
  3025. if (minVersion != 0)
  3026. {
  3027. sourceFlags &= ~(SSFsearchGraph|SSFsearchWorkflow);
  3028. }
  3029. optimized = true;
  3030. }
  3031. bool WuScopeFilter::includeStatistic(StatisticKind kind) const
  3032. {
  3033. if (!(properties & PTstatistics))
  3034. return false;
  3035. if ((desiredMeasure != SMeasureAll) && (queryMeasure(kind) != desiredMeasure))
  3036. return false;
  3037. if (desiredStats.empty())
  3038. return true;
  3039. return desiredStats.contains(kind);
  3040. }
  3041. bool WuScopeFilter::includeAttribute(WuAttr attr) const
  3042. {
  3043. if (!(properties & PTattributes))
  3044. return false;
  3045. if (desiredAttrs.empty())
  3046. return true;
  3047. return desiredAttrs.contains(attr);
  3048. }
  3049. bool WuScopeFilter::includeHint(const char * kind) const
  3050. {
  3051. if (!(properties & PThints))
  3052. return false;
  3053. if (desiredHints.empty())
  3054. return true;
  3055. return desiredHints.contains(kind);
  3056. }
  3057. bool WuScopeFilter::includeScope(const char * scope) const
  3058. {
  3059. if (include.scopeTypes)
  3060. {
  3061. const char * tail = queryScopeTail(scope);
  3062. StatsScopeId id(tail);
  3063. if (!include.scopeTypes.contains(id.queryScopeType()))
  3064. return false;
  3065. }
  3066. return true;
  3067. }
  3068. const static ScopeFilter nullScopeFilter {};
  3069. const ScopeFilter & WuScopeFilter::queryIterFilter() const
  3070. {
  3071. if (preFilterScope)
  3072. return scopeFilter;
  3073. else
  3074. return nullScopeFilter;
  3075. }
  3076. ScopeCompare WuScopeFilter::compareMatchScopes(const char * scope) const
  3077. {
  3078. return scopeFilter.compare(scope);
  3079. }
  3080. StringBuffer & WuScopeFilter::describe(StringBuffer & out) const
  3081. {
  3082. scopeFilter.describe(out);
  3083. if (requiredStats.size() || requiredAttrs.size())
  3084. {
  3085. out.append(",where[");
  3086. bool first = false;
  3087. for (const auto & stat : requiredStats)
  3088. {
  3089. if (!first)
  3090. out.append(",");
  3091. stat.describe(out);
  3092. first = false;
  3093. }
  3094. for (const auto & attr : requiredAttrs)
  3095. {
  3096. if (!first)
  3097. out.append(",");
  3098. attr.describe(out);
  3099. first = false;
  3100. }
  3101. out.append("]");
  3102. }
  3103. {
  3104. StringBuffer sources;
  3105. for (unsigned mask=1; mask; mask *= 2)
  3106. {
  3107. if (sourceFlags & mask)
  3108. {
  3109. const char * source = querySourceText((WuScopeSourceFlags)mask);
  3110. if (source)
  3111. sources.append(",").append(source);
  3112. }
  3113. }
  3114. if (sources)
  3115. out.append(",source[").append(sources.str()+1).append("]");
  3116. }
  3117. if (include.nestedDepth != UINT_MAX)
  3118. out.appendf(",nested[%u]", include.nestedDepth);
  3119. if (include.scopeTypes)
  3120. {
  3121. out.append(",include[");
  3122. ForEachItemIn(i, include.scopeTypes)
  3123. {
  3124. if (i)
  3125. out.append(",");
  3126. out.append(queryScopeTypeName((StatisticScopeType)include.scopeTypes.item(i)));
  3127. }
  3128. out.append("]");
  3129. }
  3130. {
  3131. StringBuffer props;
  3132. if (properties == PTnone)
  3133. props.append(",none");
  3134. else if (properties == PTall)
  3135. props.append(",all");
  3136. else
  3137. {
  3138. if (properties & PTstatistics)
  3139. props.append(",stat");
  3140. if (properties & PTattributes)
  3141. props.append(",attr");
  3142. if (properties & PThints)
  3143. props.append(",hint");
  3144. if (properties & PTscope)
  3145. props.append(",scope");
  3146. if (properties & PTnotes)
  3147. props.append(",note");
  3148. }
  3149. out.append(",properties[").append(props.str()+1).append("]");
  3150. }
  3151. if (desiredStats)
  3152. {
  3153. out.append(",stat[");
  3154. ForEachItemIn(i, desiredStats)
  3155. {
  3156. if (i)
  3157. out.append(",");
  3158. out.append(queryStatisticName((StatisticKind)desiredStats.item(i)));
  3159. }
  3160. out.append("]");
  3161. }
  3162. if (desiredAttrs)
  3163. {
  3164. out.append(",attr[");
  3165. ForEachItemIn(i, desiredAttrs)
  3166. {
  3167. if (i)
  3168. out.append(",");
  3169. out.append(queryWuAttributeName((WuAttr)desiredAttrs.item(i)));
  3170. }
  3171. out.append("]");
  3172. }
  3173. if (desiredHints)
  3174. {
  3175. out.append(",hint[");
  3176. ForEachItemIn(i, desiredHints)
  3177. {
  3178. if (i)
  3179. out.append(",");
  3180. out.append(desiredHints.item(i));
  3181. }
  3182. out.append("]");
  3183. }
  3184. if (desiredMeasure != SMeasureAll)
  3185. out.append(",measure[").append(queryMeasureName(desiredMeasure)).append("]");
  3186. if (minVersion != 0)
  3187. out.appendf(",version(%" I64F "u)", minVersion);
  3188. return out;
  3189. }
  3190. //--------------------------------------------------------------------------------------------------------------------
  3191. EnumMapping states[] = {
  3192. { WUStateUnknown, "unknown" },
  3193. { WUStateCompiled, "compiled" },
  3194. { WUStateRunning, "running" },
  3195. { WUStateCompleted, "completed" },
  3196. { WUStateFailed, "failed" },
  3197. { WUStateArchived, "archived" },
  3198. { WUStateAborting, "aborting" },
  3199. { WUStateAborted, "aborted" },
  3200. { WUStateBlocked, "blocked" },
  3201. { WUStateSubmitted, "submitted" },
  3202. { WUStateScheduled, "scheduled" },
  3203. { WUStateCompiling, "compiling" },
  3204. { WUStateWait, "wait" },
  3205. { WUStateUploadingFiles, "uploading_files" },
  3206. { WUStateDebugPaused, "debugging" },
  3207. { WUStateDebugRunning, "debug_running" },
  3208. { WUStatePaused, "paused" },
  3209. { WUStateSize, NULL }
  3210. };
  3211. EnumMapping actions[] = {
  3212. { WUActionUnknown, "unknown" },
  3213. { WUActionCompile, "compile" },
  3214. { WUActionCheck, "check" },
  3215. { WUActionRun, "run" },
  3216. { WUActionExecuteExisting, "execute" },
  3217. { WUActionPause, "pause" },
  3218. { WUActionPauseNow, "pausenow" },
  3219. { WUActionResume, "resume" },
  3220. { WUActionSize, NULL },
  3221. };
  3222. EnumMapping priorityClasses[] = {
  3223. { PriorityClassUnknown, "unknown" },
  3224. { PriorityClassLow, "low" },
  3225. { PriorityClassNormal, "normal" },
  3226. { PriorityClassHigh, "high" },
  3227. { PriorityClassSize, NULL },
  3228. };
  3229. const char * getWorkunitStateStr(WUState state)
  3230. {
  3231. dbgassertex(state < WUStateSize);
  3232. return states[state].str; // MORE - should be using getEnumText, or need to take steps to ensure values remain contiguous and in order.
  3233. }
  3234. void setEnum(IPropertyTree *p, const char *propname, int value, const EnumMapping *map)
  3235. {
  3236. const char *defval = map->str;
  3237. while (map->str)
  3238. {
  3239. if (value==map->val)
  3240. {
  3241. p->setProp(propname, map->str);
  3242. return;
  3243. }
  3244. map++;
  3245. }
  3246. assertex(!"Unexpected value in setEnum");
  3247. p->setProp(propname, defval);
  3248. }
  3249. static int getEnum(const IPropertyTree *p, const char *propname, const EnumMapping *map)
  3250. {
  3251. return getEnum(p->queryProp(propname),map);
  3252. }
  3253. const char * getWorkunitActionStr(WUAction action)
  3254. {
  3255. return getEnumText(action, actions);
  3256. }
  3257. WUAction getWorkunitAction(const char *actionStr)
  3258. {
  3259. return (WUAction) getEnum(actionStr, actions);
  3260. }
  3261. //==========================================================================================
  3262. class CLightweightWorkunitInfo : public CInterfaceOf<IConstWorkUnitInfo>
  3263. {
  3264. public:
  3265. CLightweightWorkunitInfo(IPropertyTree &p)
  3266. {
  3267. wuid.set(p.queryName());
  3268. user.set(p.queryProp("@submitID"));
  3269. jobName.set(p.queryProp("@jobName"));
  3270. clusterName.set(p.queryProp("@clusterName"));
  3271. timeScheduled.set(p.queryProp("@timeScheduled"));
  3272. state = (WUState) getEnum(&p, "@state", states);
  3273. action = (WUAction) getEnum(&p, "Action", actions);
  3274. priority = (WUPriorityClass) getEnum(&p, "@priorityClass", priorityClasses);
  3275. priorityLevel = calcPriorityValue(&p);
  3276. wuscope.set(p.queryProp("@scope"));
  3277. appvalues.loadBranch(&p,"Application");
  3278. totalThorTime = (unsigned)nanoToMilli(extractTimeCollatable(p.queryProp("@totalThorTime"), nullptr));
  3279. _isProtected = p.getPropBool("@protected", false);
  3280. costExecute = p.getPropInt64("@costExecute");
  3281. costFileAccess = p.getPropInt64("@costFileAccess");
  3282. costCompile = p.getPropInt64("@costCompile");
  3283. }
  3284. virtual const char *queryWuid() const { return wuid.str(); }
  3285. virtual const char *queryUser() const { return user.str(); }
  3286. virtual const char *queryJobName() const { return jobName.str(); }
  3287. virtual const char *queryClusterName() const { return clusterName.str(); }
  3288. virtual const char *queryWuScope() const { return wuscope.str(); }
  3289. virtual WUState getState() const { return state; }
  3290. virtual const char *queryStateDesc() const { return getEnumText(state, states); }
  3291. virtual WUAction getAction() const { return action; }
  3292. virtual const char *queryActionDesc() const { return getEnumText(action, actions); }
  3293. virtual WUPriorityClass getPriority() const { return priority; }
  3294. virtual const char *queryPriorityDesc() const { return getEnumText(priority, priorityClasses); }
  3295. virtual int getPriorityLevel() const { return priorityLevel; }
  3296. virtual bool isProtected() const { return _isProtected; }
  3297. virtual cost_type getExecuteCost() const { return costExecute; }
  3298. virtual cost_type getFileAccessCost() const { return costFileAccess; }
  3299. virtual cost_type getCompileCost() const { return costCompile; }
  3300. virtual IJlibDateTime & getTimeScheduled(IJlibDateTime & val) const
  3301. {
  3302. if (timeScheduled.length())
  3303. val.setGmtString(timeScheduled.str());
  3304. return val;
  3305. }
  3306. virtual unsigned getTotalThorTime() const { return totalThorTime; };
  3307. virtual IConstWUAppValueIterator & getApplicationValues() const { return *new CArrayIteratorOf<IConstWUAppValue,IConstWUAppValueIterator> (appvalues, 0, (IConstWorkUnitInfo *) this); };
  3308. protected:
  3309. StringAttr wuid, user, jobName, clusterName, timeScheduled, wuscope;
  3310. mutable CachedWUAppValues appvalues;
  3311. unsigned totalThorTime;
  3312. WUState state;
  3313. WUAction action;
  3314. WUPriorityClass priority;
  3315. int priorityLevel;
  3316. bool _isProtected;
  3317. unsigned __int64 costExecute;
  3318. unsigned __int64 costFileAccess;
  3319. unsigned __int64 costCompile;
  3320. };
  3321. extern IConstWorkUnitInfo *createConstWorkUnitInfo(IPropertyTree &p)
  3322. {
  3323. return new CLightweightWorkunitInfo(p);
  3324. }
  3325. class CDaliWorkUnit;
  3326. class CDaliWuGraphStats : public CWuGraphStats
  3327. {
  3328. public:
  3329. CDaliWuGraphStats(const CDaliWorkUnit* _owner, StatisticCreatorType _creatorType, const char * _creator, unsigned _wfid, const char * _rootScope, unsigned _id, bool _merge)
  3330. : CWuGraphStats(_creatorType, _creator, _wfid, _rootScope, _id, _merge), owner(_owner), graphName(_rootScope), wfid(_wfid)
  3331. {
  3332. }
  3333. protected:
  3334. virtual IPropertyTree &queryProgressTree() override;
  3335. const CDaliWorkUnit *owner;
  3336. Owned<IRemoteConnection> conn;
  3337. StringAttr graphName;
  3338. unsigned wfid;
  3339. };
  3340. class CLocalWuGraphStats : public CWuGraphStats
  3341. {
  3342. public:
  3343. CLocalWuGraphStats(IPropertyTree *_p, StatisticCreatorType _creatorType, const char * _creator, unsigned _wfid, const char * _rootScope, unsigned _id, bool _merge)
  3344. : CWuGraphStats(_creatorType, _creator, _wfid, _rootScope, _id, _merge), graphName(_rootScope), p(_p)
  3345. {
  3346. }
  3347. protected:
  3348. virtual IPropertyTree &queryProgressTree() override
  3349. {
  3350. IPropertyTree *progress = p->queryPropTree("GraphProgress");
  3351. if (!progress)
  3352. progress = p->addPropTree("GraphProgress");
  3353. IPropertyTree *graph = progress->queryPropTree(graphName);
  3354. if (!graph)
  3355. graph = progress->addPropTree(graphName);
  3356. return *graph;
  3357. }
  3358. StringAttr graphName;
  3359. Owned<IPropertyTree> p;
  3360. };
  3361. CWorkUnitWatcher::CWorkUnitWatcher(IWorkUnitSubscriber *_subscriber, WUSubscribeOptions flags, const char *wuid) : subscriber(_subscriber)
  3362. {
  3363. abortId = 0;
  3364. stateId = 0;
  3365. actionId = 0;
  3366. assertex((flags & ~SubscribeOptionAbort) == 0);
  3367. if (flags & SubscribeOptionAbort)
  3368. {
  3369. VStringBuffer xpath("/WorkUnitAborts/%s", wuid);
  3370. abortId = querySDS().subscribe(xpath.str(), *this, false, true);
  3371. }
  3372. }
  3373. CWorkUnitWatcher::~CWorkUnitWatcher()
  3374. {
  3375. assertex(abortId==0 && stateId==0 && actionId==0);
  3376. }
  3377. void CWorkUnitWatcher::unsubscribe()
  3378. {
  3379. CriticalBlock b(crit);
  3380. if (abortId)
  3381. querySDS().unsubscribe(abortId);
  3382. if (stateId)
  3383. querySDS().unsubscribe(stateId);
  3384. if (actionId)
  3385. querySDS().unsubscribe(actionId);
  3386. abortId = 0;
  3387. stateId = 0;
  3388. actionId = 0;
  3389. }
  3390. void CWorkUnitWatcher::notify(SubscriptionId id, const char *xpath, SDSNotifyFlags flags, unsigned valueLen, const void *valueData)
  3391. {
  3392. CriticalBlock b(crit);
  3393. if (id==stateId)
  3394. subscriber->notify(SubscribeOptionState, valueLen, valueData);
  3395. else if (id==actionId)
  3396. subscriber->notify(SubscribeOptionAction, valueLen, valueData);
  3397. else if (id==abortId)
  3398. subscriber->notify(SubscribeOptionAbort, valueLen, valueData);
  3399. }
  3400. class CDaliWorkUnitWatcher : public CWorkUnitWatcher
  3401. {
  3402. public:
  3403. CDaliWorkUnitWatcher(IWorkUnitSubscriber *_subscriber, WUSubscribeOptions flags, const char *wuid)
  3404. : CWorkUnitWatcher(_subscriber, (WUSubscribeOptions) (flags & SubscribeOptionAbort), wuid)
  3405. {
  3406. if (flags & SubscribeOptionState)
  3407. {
  3408. VStringBuffer xpath("/WorkUnits/%s/State", wuid);
  3409. stateId = querySDS().subscribe(xpath.str(), *this);
  3410. }
  3411. if (flags & SubscribeOptionAction)
  3412. {
  3413. VStringBuffer xpath("/WorkUnits/%s/Action", wuid);
  3414. actionId = querySDS().subscribe(xpath.str(), *this);
  3415. }
  3416. }
  3417. };
  3418. void CPersistedWorkUnit::subscribe(WUSubscribeOptions options)
  3419. {
  3420. CriticalBlock block(crit);
  3421. assertex(options==SubscribeOptionAbort);
  3422. if (!abortWatcher)
  3423. {
  3424. abortWatcher.setown(new CWorkUnitWatcher(this, SubscribeOptionAbort, p->queryName()));
  3425. abortDirty = true;
  3426. }
  3427. }
  3428. void CPersistedWorkUnit::unsubscribe()
  3429. {
  3430. CriticalBlock block(crit);
  3431. if (abortWatcher)
  3432. {
  3433. abortWatcher->unsubscribe();
  3434. abortWatcher.clear();
  3435. }
  3436. }
  3437. bool CPersistedWorkUnit::aborting() const
  3438. {
  3439. CriticalBlock block(crit);
  3440. if (abortDirty)
  3441. {
  3442. StringBuffer apath;
  3443. apath.append("/WorkUnitAborts/").append(p->queryName());
  3444. Owned<IRemoteConnection> acon = querySDS().connect(apath.str(), myProcessSession(), 0, SDS_LOCK_TIMEOUT);
  3445. if (acon)
  3446. abortState = acon->queryRoot()->getPropInt(NULL) != 0;
  3447. else
  3448. abortState = false;
  3449. abortDirty = false;
  3450. }
  3451. return abortState;
  3452. }
  3453. class CDaliWorkUnit : public CPersistedWorkUnit
  3454. {
  3455. friend class CDaliWuGraphStats;
  3456. public:
  3457. CDaliWorkUnit(IRemoteConnection *_conn, ISecManager *secmgr, ISecUser *secuser)
  3458. : connection(_conn), CPersistedWorkUnit(secmgr, secuser)
  3459. {
  3460. loadPTree(connection->getRoot());
  3461. }
  3462. ~CDaliWorkUnit()
  3463. {
  3464. // NOTE - order is important - we need to construct connection before p and (especially) destroy after p
  3465. // We use the beforeDispose() in base class to help ensure this
  3466. p.clear();
  3467. }
  3468. IConstWUGraphProgress *getGraphProgress(const char *graphName) const
  3469. {
  3470. Owned<IRemoteConnection> conn = getProgressConnection();
  3471. if (conn)
  3472. {
  3473. IPTree *progress = conn->queryRoot()->queryPropTree(graphName);
  3474. if (progress)
  3475. return new CConstGraphProgress(p->queryName(), graphName, progress);
  3476. }
  3477. return NULL;
  3478. }
  3479. virtual WUGraphState queryGraphState(const char *graphName) const
  3480. {
  3481. Owned<IRemoteConnection> conn = getProgressConnection();
  3482. if (conn)
  3483. {
  3484. IPTree *progress = conn->queryRoot()->queryPropTree(graphName);
  3485. if (progress)
  3486. return (WUGraphState) progress->getPropInt("@_state", (unsigned) WUGraphUnknown);
  3487. }
  3488. return WUGraphUnknown;
  3489. }
  3490. virtual WUGraphState queryNodeState(const char *graphName, WUGraphIDType nodeId) const
  3491. {
  3492. Owned<IRemoteConnection> conn = getProgressConnection();
  3493. if (conn)
  3494. {
  3495. IPTree *progress = conn->queryRoot()->queryPropTree(graphName);
  3496. if (progress)
  3497. {
  3498. StringBuffer path;
  3499. // NOTE - the node state info still uses the old graph layout, even when the stats are using the new...
  3500. path.append("node[@id=\"").append(nodeId).append("\"]/@_state");
  3501. return (WUGraphState) progress->getPropInt(path, (unsigned) WUGraphUnknown);
  3502. }
  3503. }
  3504. return WUGraphUnknown;
  3505. }
  3506. virtual void clearGraphProgress() const
  3507. {
  3508. CriticalBlock block(crit);
  3509. progressConnection.clear(); // Make sure nothing is locking for read or we won't be able to lock for write
  3510. StringBuffer path("/GraphProgress/");
  3511. path.append(p->queryName());
  3512. Owned<IRemoteConnection> delconn = querySDS().connect(path.str(), myProcessSession(), RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  3513. if (delconn)
  3514. delconn->close(true);
  3515. }
  3516. virtual bool getRunningGraph(IStringVal &graphName, WUGraphIDType &subId) const
  3517. {
  3518. Owned<IRemoteConnection> conn = getProgressConnection();
  3519. if (!conn)
  3520. return false;
  3521. const char *name = conn->queryRoot()->queryProp("Running/@graph");
  3522. if (name)
  3523. {
  3524. graphName.set(name);
  3525. subId = conn->queryRoot()->getPropInt64("Running/@subId");
  3526. return true;
  3527. }
  3528. else
  3529. return false;
  3530. }
  3531. virtual void forceReload()
  3532. {
  3533. synchronized sync(locked); // protect locked workunits (uncommitted writes) from reload
  3534. CriticalBlock block(crit);
  3535. clearCached(true);
  3536. connection->reload();
  3537. progressConnection.clear();
  3538. abortDirty = true;
  3539. p.setown(connection->getRoot());
  3540. }
  3541. virtual void cleanupAndDelete(bool deldll, bool deleteOwned, const StringArray *deleteExclusions)
  3542. {
  3543. CPersistedWorkUnit::cleanupAndDelete(deldll, deleteOwned, deleteExclusions);
  3544. clearGraphProgress();
  3545. connection->close(true);
  3546. connection.clear();
  3547. }
  3548. virtual void commit()
  3549. {
  3550. CPersistedWorkUnit::commit();
  3551. if (connection)
  3552. connection->commit();
  3553. }
  3554. virtual void _lockRemote()
  3555. {
  3556. StringBuffer wuRoot;
  3557. getXPath(wuRoot, p->queryName());
  3558. if (connection)
  3559. connection->changeMode(RTM_LOCK_WRITE,SDS_LOCK_TIMEOUT);
  3560. else
  3561. connection.setown(querySDS().connect(wuRoot.str(), myProcessSession(), RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT));
  3562. if (!connection)
  3563. throw MakeStringException(WUERR_LockFailed, "Failed to get connection for xpath %s", wuRoot.str());
  3564. clearCached(true);
  3565. p.setown(connection->getRoot());
  3566. }
  3567. virtual void _unlockRemote()
  3568. {
  3569. if (connection)
  3570. {
  3571. try
  3572. {
  3573. try
  3574. {
  3575. connection->commit();
  3576. }
  3577. catch (IException *e)
  3578. {
  3579. EXCLOG(e, "Error during workunit commit");
  3580. connection->rollback();
  3581. connection->changeMode(0, SDS_LOCK_TIMEOUT);
  3582. throw;
  3583. }
  3584. connection->changeMode(0, SDS_LOCK_TIMEOUT);
  3585. }
  3586. catch (IException *E)
  3587. {
  3588. StringBuffer s;
  3589. IERRLOG("Failed to release write lock on workunit: %s", E->errorMessage(s).str());
  3590. throw;
  3591. }
  3592. }
  3593. }
  3594. virtual void setGraphState(const char *graphName, unsigned wfid, WUGraphState state) const
  3595. {
  3596. Owned<IRemoteConnection> conn = getWritableProgressConnection(graphName, wfid);
  3597. conn->queryRoot()->setPropInt("@_state", state);
  3598. }
  3599. virtual void setNodeState(const char *graphName, WUGraphIDType nodeId, WUGraphState state) const
  3600. {
  3601. CriticalBlock block(crit);
  3602. progressConnection.clear(); // Make sure nothing is locking for read or we won't be able to lock for write
  3603. VStringBuffer path("/GraphProgress/%s", queryWuid());
  3604. Owned<IRemoteConnection> conn = querySDS().connect(path, myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  3605. IPTree *progress = ensurePTree(conn->queryRoot(), graphName);
  3606. // NOTE - the node state info still uses the old graph layout, even when the stats are using the new...
  3607. path.clear().append("node[@id=\"").append(nodeId).append("\"]");
  3608. IPropertyTree *node = progress->queryPropTree(path.str());
  3609. if (!node)
  3610. {
  3611. node = progress->addPropTree("node");
  3612. node->setPropInt64("@id", nodeId);
  3613. }
  3614. node->setPropInt("@_state", (unsigned)state);
  3615. switch (state)
  3616. {
  3617. case WUGraphRunning:
  3618. {
  3619. IPropertyTree *running = conn->queryRoot()->setPropTree("Running");
  3620. running->setProp("@graph", graphName);
  3621. running->setPropInt64("@subId", nodeId);
  3622. break;
  3623. }
  3624. case WUGraphComplete:
  3625. {
  3626. conn->queryRoot()->removeProp("Running"); // only one thing running at any given time and one thing with lockWrite access
  3627. break;
  3628. }
  3629. }
  3630. }
  3631. virtual IWUGraphStats *updateStats(const char *graphName, StatisticCreatorType creatorType, const char * creator, unsigned _wfid, unsigned subgraph, bool merge) const override
  3632. {
  3633. return new CDaliWuGraphStats(this, creatorType, creator, _wfid, graphName, subgraph, merge);
  3634. }
  3635. virtual void import(IPropertyTree *wuTree, IPropertyTree *graphProgressTree)
  3636. {
  3637. connection->queryRoot()->setPropTree(nullptr, LINK(wuTree));
  3638. loadPTree(connection->getRoot());
  3639. if (!graphProgressTree)
  3640. return;
  3641. VStringBuffer xpath("/GraphProgress/%s", queryWuid());
  3642. Owned<IRemoteConnection> progressConn = querySDS().connect(xpath, myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE, SDS_LOCK_TIMEOUT);
  3643. if (!progressConn)
  3644. throw MakeStringException(0, "Failed to access %s.", xpath.str());
  3645. progressConn->queryRoot()->setPropTree(nullptr, LINK(graphProgressTree));
  3646. }
  3647. protected:
  3648. IRemoteConnection *getProgressConnection() const
  3649. {
  3650. CriticalBlock block(crit);
  3651. if (!progressConnection)
  3652. {
  3653. VStringBuffer path("/GraphProgress/%s", queryWuid());
  3654. progressConnection.setown(querySDS().connect(path, myProcessSession(), 0, SDS_LOCK_TIMEOUT)); // Note - we don't lock. The writes are atomic.
  3655. }
  3656. return progressConnection.getLink();
  3657. }
  3658. IRemoteConnection *getWritableProgressConnection(const char *graphName, unsigned wfid) const
  3659. {
  3660. CriticalBlock block(crit);
  3661. progressConnection.clear(); // Make sure subsequent reads from this workunit get the changes I am making
  3662. VStringBuffer path("/GraphProgress/%s/%s", queryWuid(), graphName);
  3663. Owned<IRemoteConnection> conn = querySDS().connect(path, myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  3664. IPropertyTree * root = conn->queryRoot();
  3665. assertex(wfid);
  3666. if (!root->hasProp("@wfid"))
  3667. {
  3668. root->setPropInt("@wfid", wfid);
  3669. }
  3670. else
  3671. {
  3672. //Ideally the following code would check that the wfids are passed consistently.
  3673. //However there is an obscure problem with out of line functions being called from multiple workflow
  3674. //ids, and possibly library graphs.
  3675. //Stats for library graphs should be nested below the library call activity
  3676. //assertex(root->getPropInt("@wfid", 0) == wfid); // check that wfid is passed consistently
  3677. }
  3678. return conn.getClear();
  3679. }
  3680. IPropertyTree *getGraphProgressTree() const
  3681. {
  3682. Owned<IRemoteConnection> conn = getProgressConnection();
  3683. if (conn)
  3684. {
  3685. Owned<IPropertyTree> tmp = createPTree("GraphProgress");
  3686. mergePTree(tmp,conn->queryRoot());
  3687. return tmp.getClear();
  3688. }
  3689. return NULL;
  3690. }
  3691. Owned<IRemoteConnection> connection;
  3692. mutable Owned<IRemoteConnection> progressConnection;
  3693. };
  3694. class CLockedWorkUnit : implements ILocalWorkUnit, implements IExtendedWUInterface, public CInterface
  3695. {
  3696. public:
  3697. Owned<CLocalWorkUnit> c;
  3698. IMPLEMENT_IINTERFACE;
  3699. CLockedWorkUnit(CLocalWorkUnit *_c) : c(_c) {}
  3700. ~CLockedWorkUnit()
  3701. {
  3702. if (workUnitTraceLevel > 1)
  3703. DBGLOG("Releasing locked workunit %s", queryWuid());
  3704. if (c)
  3705. {
  3706. try
  3707. {
  3708. c->unlockRemote();
  3709. }
  3710. catch (IException *E)
  3711. {
  3712. // Exceptions here should be very uncommon - but there's also not a lot we can do if we get one
  3713. // Allowing them to be thrown out of the destructor is going to terminate the program which is NOT what we want.
  3714. EXCLOG(E);
  3715. ::Release(E);
  3716. }
  3717. }
  3718. }
  3719. virtual IConstWorkUnit * unlock() override
  3720. {
  3721. c->unlockRemote();
  3722. return c.getClear();
  3723. }
  3724. virtual bool aborting() const
  3725. { return c->aborting(); }
  3726. virtual void forceReload()
  3727. { }
  3728. virtual WUAction getAction() const
  3729. { return c->getAction(); }
  3730. virtual const char *queryActionDesc() const
  3731. { return c->queryActionDesc(); }
  3732. virtual IStringVal & getApplicationValue(const char * application, const char * propname, IStringVal & str) const
  3733. { return c->getApplicationValue(application, propname, str); }
  3734. virtual int getApplicationValueInt(const char * application, const char * propname, int defVal) const
  3735. { return c->getApplicationValueInt(application, propname, defVal); }
  3736. virtual IConstWUAppValueIterator & getApplicationValues() const
  3737. { return c->getApplicationValues(); }
  3738. virtual bool hasWorkflow() const
  3739. { return c->hasWorkflow(); }
  3740. virtual unsigned queryEventScheduledCount() const
  3741. { return c->queryEventScheduledCount(); }
  3742. virtual IPropertyTree * queryWorkflowTree() const
  3743. { return c->queryWorkflowTree(); }
  3744. virtual IConstWorkflowItemIterator * getWorkflowItems() const
  3745. { return c->getWorkflowItems(); }
  3746. virtual IWorkflowItemArray * getWorkflowClone() const
  3747. { return c->getWorkflowClone(); }
  3748. virtual bool requiresLocalFileUpload() const
  3749. { return c->requiresLocalFileUpload(); }
  3750. virtual IConstLocalFileUploadIterator * getLocalFileUploads() const
  3751. { return c->getLocalFileUploads(); }
  3752. virtual bool getIsQueryService() const
  3753. { return c->getIsQueryService(); }
  3754. virtual bool getCloneable() const
  3755. { return c->getCloneable(); }
  3756. virtual IUserDescriptor * queryUserDescriptor() const
  3757. { return c->queryUserDescriptor(); }
  3758. virtual const char *queryClusterName() const
  3759. { return c->queryClusterName(); }
  3760. virtual unsigned getCodeVersion() const
  3761. { return c->getCodeVersion(); }
  3762. virtual unsigned getWuidVersion() const
  3763. { return c->getWuidVersion(); }
  3764. virtual void getBuildVersion(IStringVal & buildVersion, IStringVal & eclVersion) const
  3765. { c->getBuildVersion(buildVersion, eclVersion); }
  3766. virtual bool hasDebugValue(const char * propname) const
  3767. { return c->hasDebugValue(propname); }
  3768. virtual IStringVal & getDebugValue(const char * propname, IStringVal & str) const
  3769. { return c->getDebugValue(propname, str); }
  3770. virtual int getDebugValueInt(const char * propname, int defVal) const
  3771. { return c->getDebugValueInt(propname, defVal); }
  3772. virtual __int64 getDebugValueInt64(const char * propname, __int64 defVal) const
  3773. { return c->getDebugValueInt64(propname, defVal); }
  3774. virtual double getDebugValueReal(const char * propname, double defVal) const
  3775. { return c->getDebugValueReal(propname, defVal); }
  3776. virtual bool getDebugValueBool(const char * propname, bool defVal) const
  3777. { return c->getDebugValueBool(propname, defVal); }
  3778. virtual IStringIterator & getDebugValues() const
  3779. { return c->getDebugValues(NULL); }
  3780. virtual IStringIterator & getDebugValues(const char *prop) const
  3781. { return c->getDebugValues(prop); }
  3782. virtual unsigned getExceptionCount() const
  3783. { return c->getExceptionCount(); }
  3784. virtual IConstWUExceptionIterator & getExceptions() const
  3785. { return c->getExceptions(); }
  3786. virtual unsigned getGraphCount() const
  3787. { return c->getGraphCount(); }
  3788. virtual unsigned getSourceFileCount() const
  3789. { return c->getSourceFileCount(); }
  3790. virtual unsigned getResultCount() const
  3791. { return c->getResultCount(); }
  3792. virtual unsigned getVariableCount() const
  3793. { return c->getVariableCount(); }
  3794. virtual unsigned getApplicationValueCount() const
  3795. { return c->getApplicationValueCount(); }
  3796. virtual IConstWUGraphIterator & getGraphs(WUGraphType type) const
  3797. { return c->getGraphs(type); }
  3798. virtual IConstWUGraphMetaIterator & getGraphsMeta(WUGraphType type) const
  3799. { return c->getGraphsMeta(type); }
  3800. virtual IConstWUGraph * getGraph(const char *name) const
  3801. { return c->getGraph(name); }
  3802. virtual IConstWUGraphProgress * getGraphProgress(const char * name) const
  3803. { return c->getGraphProgress(name); }
  3804. virtual const char *queryJobName() const
  3805. { return c->queryJobName(); }
  3806. virtual IConstWUPlugin * getPluginByName(const char * name) const
  3807. { return c->getPluginByName(name); }
  3808. virtual IConstWUPluginIterator & getPlugins() const
  3809. { return c->getPlugins(); }
  3810. virtual IConstWULibrary* getLibraryByName(const char *name) const
  3811. { return c->getLibraryByName(name); }
  3812. virtual IConstWULibraryIterator & getLibraries() const
  3813. { return c->getLibraries(); }
  3814. virtual WUPriorityClass getPriority() const
  3815. { return c->getPriority(); }
  3816. virtual const char *queryPriorityDesc() const
  3817. { return c->queryPriorityDesc(); }
  3818. virtual int getPriorityLevel() const
  3819. { return c->getPriorityLevel(); }
  3820. virtual int getPriorityValue() const
  3821. { return c->getPriorityValue(); }
  3822. virtual IConstWUQuery * getQuery() const
  3823. { return c->getQuery(); }
  3824. virtual IConstWUWebServicesInfo * getWebServicesInfo() const
  3825. { return c->getWebServicesInfo(); }
  3826. virtual bool getRescheduleFlag() const
  3827. { return c->getRescheduleFlag(); }
  3828. virtual IConstWUResult * getResultByName(const char * name) const
  3829. { return c->getResultByName(name); }
  3830. virtual IConstWUResult * getResultBySequence(unsigned seq) const
  3831. { return c->getResultBySequence(seq); }
  3832. virtual IConstWUResult * getQueryResultByName(const char * name) const
  3833. { return c->getQueryResultByName(name); }
  3834. virtual unsigned getResultLimit() const
  3835. { return c->getResultLimit(); }
  3836. virtual IConstWUResultIterator & getResults() const
  3837. { return c->getResults(); }
  3838. virtual IStringVal & getScope(IStringVal & str) const
  3839. { return c->getScope(str); }
  3840. virtual IStringVal & getWorkunitDistributedAccessToken(IStringVal & str) const
  3841. { return c->getWorkunitDistributedAccessToken(str); }
  3842. virtual WUState getState() const
  3843. { return c->getState(); }
  3844. virtual IStringVal & getStateEx(IStringVal & str) const
  3845. { return c->getStateEx(str); }
  3846. virtual __int64 getAgentSession() const
  3847. { return c->getAgentSession(); }
  3848. virtual unsigned getAgentPID() const
  3849. { return c->getAgentPID(); }
  3850. virtual const char *queryStateDesc() const
  3851. { return c->queryStateDesc(); }
  3852. virtual bool getRunningGraph(IStringVal & graphName, WUGraphIDType & subId) const
  3853. { return c->getRunningGraph(graphName, subId); }
  3854. virtual IConstWUScopeIterator & getScopeIterator(const WuScopeFilter & filter) const override
  3855. { return c->getScopeIterator(filter); }
  3856. virtual bool getStatistic(stat_type & value, const char * scope, StatisticKind kind) const override
  3857. { return c->getStatistic(value, scope, kind); }
  3858. virtual IStringVal & getSnapshot(IStringVal & str) const
  3859. { return c->getSnapshot(str); }
  3860. virtual const char *queryUser() const
  3861. { return c->queryUser(); }
  3862. virtual ErrorSeverity getWarningSeverity(unsigned code, ErrorSeverity defaultSeverity) const
  3863. { return c->getWarningSeverity(code, defaultSeverity); }
  3864. virtual const char *queryWuScope() const
  3865. { return c->queryWuScope(); }
  3866. virtual const char *queryWuid() const
  3867. { return c->queryWuid(); }
  3868. virtual IConstWUResult * getGlobalByName(const char * name) const
  3869. { return c->getGlobalByName(name); }
  3870. virtual IConstWUResult * getTemporaryByName(const char * name) const
  3871. { return c->getTemporaryByName(name); }
  3872. virtual IConstWUResultIterator & getTemporaries() const
  3873. { return c->getTemporaries(); }
  3874. virtual IConstWUResult * getVariableByName(const char * name) const
  3875. { return c->getVariableByName(name); }
  3876. virtual IConstWUResultIterator & getVariables() const
  3877. { return c->getVariables(); }
  3878. virtual bool isProtected() const
  3879. { return c->isProtected(); }
  3880. virtual bool isPausing() const
  3881. { return c->isPausing(); }
  3882. virtual IWorkUnit & lock()
  3883. { ((CInterface *)this)->Link(); return (IWorkUnit &) *this; }
  3884. virtual bool reload()
  3885. { UNIMPLEMENTED; }
  3886. virtual void subscribe(WUSubscribeOptions options)
  3887. { c->subscribe(options); }
  3888. virtual void requestAbort()
  3889. { c->requestAbort(); }
  3890. virtual unsigned calculateHash(unsigned prevHash)
  3891. { return queryExtendedWU(c)->calculateHash(prevHash); }
  3892. virtual void copyWorkUnit(IConstWorkUnit *cached, bool copyStats, bool all)
  3893. { queryExtendedWU(c)->copyWorkUnit(cached, copyStats, all); }
  3894. virtual IPropertyTree *queryPTree() const
  3895. { return queryExtendedWU(c)->queryPTree(); }
  3896. virtual IPropertyTree *getUnpackedTree(bool includeProgress) const
  3897. { return queryExtendedWU(c)->getUnpackedTree(includeProgress); }
  3898. virtual bool archiveWorkUnit(const char *base,bool del,bool deldll,bool deleteOwned,bool exportAssociatedFiles)
  3899. { return queryExtendedWU(c)->archiveWorkUnit(base,del,deldll,deleteOwned,exportAssociatedFiles); }
  3900. virtual unsigned queryFileUsage(const char *filename) const
  3901. { return c->queryFileUsage(filename); }
  3902. virtual IConstWUFileUsageIterator * getFieldUsage() const
  3903. { return c->getFieldUsage(); }
  3904. virtual bool getFieldUsageArray(StringArray & filenames, StringArray & columnnames, const char * clusterName) const
  3905. { return c->getFieldUsageArray(filenames, columnnames, clusterName); }
  3906. virtual IJlibDateTime & getTimeScheduled(IJlibDateTime &val) const
  3907. { return c->getTimeScheduled(val); }
  3908. virtual unsigned getDebugAgentListenerPort() const
  3909. { return c->getDebugAgentListenerPort(); }
  3910. virtual IStringVal & getDebugAgentListenerIP(IStringVal &ip) const
  3911. { return c->getDebugAgentListenerIP(ip); }
  3912. virtual IStringVal & getXmlParams(IStringVal & params, bool hidePasswords) const
  3913. { return c->getXmlParams(params, hidePasswords); }
  3914. virtual const IPropertyTree *getXmlParams() const
  3915. { return c->getXmlParams(); }
  3916. virtual unsigned __int64 getHash() const
  3917. { return c->getHash(); }
  3918. virtual IStringIterator *getLogs(const char *type, const char *instance) const
  3919. { return c->getLogs(type, instance); }
  3920. virtual IStringIterator *getProcesses(const char *type) const
  3921. { return c->getProcesses(type); }
  3922. virtual IPropertyTreeIterator* getProcesses(const char *type, const char *instance) const
  3923. { return c->getProcesses(type, instance); }
  3924. virtual unsigned getTotalThorTime() const
  3925. { return c->getTotalThorTime(); }
  3926. virtual WUGraphState queryGraphState(const char *graphName) const
  3927. { return c->queryGraphState(graphName); }
  3928. virtual WUGraphState queryNodeState(const char *graphName, WUGraphIDType nodeId) const
  3929. { return c->queryNodeState(graphName, nodeId); }
  3930. virtual void setGraphState(const char *graphName, unsigned wfid, WUGraphState state) const
  3931. { c->setGraphState(graphName, wfid, state); }
  3932. virtual void setNodeState(const char *graphName, WUGraphIDType nodeId, WUGraphState state) const
  3933. { c->setNodeState(graphName, nodeId, state); }
  3934. virtual IWUGraphStats *updateStats(const char *graphName, StatisticCreatorType creatorType, const char * creator, unsigned _wfid, unsigned subgraph, bool merge) const override
  3935. { return c->updateStats(graphName, creatorType, creator, _wfid, subgraph, merge); }
  3936. virtual void clearGraphProgress() const
  3937. { c->clearGraphProgress(); }
  3938. virtual IStringVal & getAbortBy(IStringVal & str) const
  3939. { return c->getAbortBy(str); }
  3940. virtual unsigned __int64 getAbortTimeStamp() const
  3941. { return c->getAbortTimeStamp(); }
  3942. virtual cost_type getExecuteCost() const
  3943. { return c->getExecuteCost(); }
  3944. virtual cost_type getFileAccessCost() const
  3945. { return c->getFileAccessCost(); }
  3946. virtual cost_type getCompileCost() const
  3947. { return c->getCompileCost(); }
  3948. virtual void import(IPropertyTree *wuTree, IPropertyTree *graphProgressTree)
  3949. { return c->import(wuTree, graphProgressTree); }
  3950. virtual void clearExceptions(const char *source=nullptr)
  3951. { c->clearExceptions(source); }
  3952. virtual void commit()
  3953. { c->commit(); }
  3954. virtual IWUException * createException()
  3955. { return c->createException(); }
  3956. virtual void addProcess(const char *type, const char *instance, unsigned pid, unsigned max, const char *pattern, bool singleLog, const char *log)
  3957. { c->addProcess(type, instance, pid, max, pattern, singleLog, log); }
  3958. virtual void protect(bool protectMode)
  3959. { c->protect(protectMode); }
  3960. virtual void setAction(WUAction action)
  3961. { c->setAction(action); }
  3962. virtual void setApplicationValue(const char * application, const char * propname, const char * value, bool overwrite)
  3963. { c->setApplicationValue(application, propname, value, overwrite); }
  3964. virtual void setApplicationValueInt(const char * application, const char * propname, int value, bool overwrite)
  3965. { c->setApplicationValueInt(application, propname, value, overwrite); }
  3966. virtual void incEventScheduledCount()
  3967. { c->incEventScheduledCount(); }
  3968. virtual void setIsQueryService(bool value)
  3969. { c->setIsQueryService(value); }
  3970. virtual void setCloneable(bool value)
  3971. { c->setCloneable(value); }
  3972. virtual void setIsClone(bool value)
  3973. { c->setIsClone(value); }
  3974. virtual void setClusterName(const char * value)
  3975. { c->setClusterName(value); }
  3976. virtual void setCodeVersion(unsigned version, const char * buildVersion, const char * eclVersion)
  3977. { c->setCodeVersion(version, buildVersion, eclVersion); }
  3978. virtual void setDebugValue(const char * propname, const char * value, bool overwrite)
  3979. { c->setDebugValue(propname, value, overwrite); }
  3980. virtual void setDebugValueInt(const char * propname, int value, bool overwrite)
  3981. { c->setDebugValueInt(propname, value, overwrite); }
  3982. virtual void setJobName(const char * value)
  3983. { c->setJobName(value); }
  3984. virtual void setPriority(WUPriorityClass cls)
  3985. { c->setPriority(cls); }
  3986. virtual void setPriorityLevel(int level)
  3987. { c->setPriorityLevel(level); }
  3988. virtual void setRescheduleFlag(bool value)
  3989. { c->setRescheduleFlag(value); }
  3990. virtual void setResultLimit(unsigned value)
  3991. { c->setResultLimit(value); }
  3992. virtual void setState(WUState state)
  3993. { c->setState(state); }
  3994. virtual void setStateEx(const char * text)
  3995. { c->setStateEx(text); }
  3996. virtual void setAgentSession(__int64 sessionId)
  3997. { c->setAgentSession(sessionId); }
  3998. virtual void setStatistic(StatisticCreatorType creatorType, const char * creator, StatisticScopeType scopeType, const char * scope, StatisticKind kind, const char * optDescription, unsigned __int64 value, unsigned __int64 count, unsigned __int64 maxValue, StatsMergeAction mergeAction)
  3999. { c->setStatistic(creatorType, creator, scopeType, scope, kind, optDescription, value, count, maxValue, mergeAction); }
  4000. virtual void setTracingValue(const char * propname, const char * value)
  4001. { c->setTracingValue(propname, value); }
  4002. virtual void setTracingValueInt(const char * propname, int value)
  4003. { c->setTracingValueInt(propname, value); }
  4004. virtual void setTracingValueInt64(const char * propname, __int64 value)
  4005. { c->setTracingValueInt64(propname, value); }
  4006. virtual void setUser(const char * value)
  4007. { c->setUser(value); }
  4008. virtual void setWuScope(const char * value)
  4009. { c->setWuScope(value); }
  4010. virtual IWorkflowItem* addWorkflowItem(unsigned wfid, WFType type, WFMode mode, unsigned success, unsigned failure, unsigned recovery, unsigned retriesAllowed, unsigned contingencyFor)
  4011. { return c->addWorkflowItem(wfid, type, mode, success, failure, recovery, retriesAllowed, contingencyFor); }
  4012. virtual void syncRuntimeWorkflow(IWorkflowItemArray * array)
  4013. { c->syncRuntimeWorkflow(array); }
  4014. virtual IWorkflowItemIterator * updateWorkflowItems()
  4015. { return c->updateWorkflowItems(); }
  4016. virtual void resetWorkflow()
  4017. { c->resetWorkflow(); }
  4018. virtual void schedule()
  4019. { c->schedule(); }
  4020. virtual void deschedule()
  4021. { c->deschedule(); }
  4022. virtual unsigned addLocalFileUpload(LocalFileUploadType type, char const * source, char const * destination, char const * eventTag)
  4023. { return c->addLocalFileUpload(type, source, destination, eventTag); }
  4024. virtual IWUResult * updateGlobalByName(const char * name)
  4025. { return c->updateGlobalByName(name); }
  4026. virtual void createGraph(const char * name, const char *label, WUGraphType type, IPropertyTree *xgmml, unsigned wfid)
  4027. { c->createGraph(name, label, type, xgmml, wfid); }
  4028. virtual IWUQuery * updateQuery()
  4029. { return c->updateQuery(); }
  4030. virtual IWUWebServicesInfo * updateWebServicesInfo(bool create)
  4031. { return c->updateWebServicesInfo(create); }
  4032. virtual IWUPlugin * updatePluginByName(const char * name)
  4033. { return c->updatePluginByName(name); }
  4034. virtual IWULibrary * updateLibraryByName(const char * name)
  4035. { return c->updateLibraryByName(name); }
  4036. virtual IWUResult * updateResultByName(const char * name)
  4037. { return c->updateResultByName(name); }
  4038. virtual IWUResult * updateResultBySequence(unsigned seq)
  4039. { return c->updateResultBySequence(seq); }
  4040. virtual IWUResult * updateTemporaryByName(const char * name)
  4041. { return c->updateTemporaryByName(name); }
  4042. virtual IWUResult * updateVariableByName(const char * name)
  4043. { return c->updateVariableByName(name); }
  4044. virtual void addFile(const char *fileName, StringArray *clusters, unsigned usageCount, WUFileKind fileKind, const char *graphOwner)
  4045. { c->addFile(fileName, clusters, usageCount, fileKind, graphOwner); }
  4046. virtual void noteFileRead(IDistributedFile *file)
  4047. { c->noteFileRead(file); }
  4048. virtual void noteFieldUsage(IPropertyTree * usage)
  4049. { c->noteFieldUsage(usage); }
  4050. virtual void releaseFile(const char *fileName)
  4051. { c->releaseFile(fileName); }
  4052. virtual void resetBeforeGeneration()
  4053. { c->resetBeforeGeneration(); }
  4054. virtual void deleteTempFiles(const char *graph, bool deleteOwned, bool deleteJobOwned)
  4055. { c->deleteTempFiles(graph, deleteOwned, deleteJobOwned); }
  4056. virtual void deleteTemporaries()
  4057. { c->deleteTemporaries(); }
  4058. virtual IPropertyTreeIterator & getFileIterator() const
  4059. { return c->getFileIterator(); }
  4060. virtual IPropertyTreeIterator & getFilesReadIterator() const
  4061. { return c->getFilesReadIterator(); }
  4062. virtual void setSnapshot(const char * value)
  4063. { c->setSnapshot(value); }
  4064. virtual void setWarningSeverity(unsigned code, ErrorSeverity severity)
  4065. { c->setWarningSeverity(code, severity); }
  4066. virtual void setTimeScheduled(const IJlibDateTime &val)
  4067. { c->setTimeScheduled(val); }
  4068. virtual void setDebugAgentListenerPort(unsigned port)
  4069. { c->setDebugAgentListenerPort(port); }
  4070. virtual void setDebugAgentListenerIP(const char * ip)
  4071. { c->setDebugAgentListenerIP(ip); }
  4072. virtual void setXmlParams(const char *params)
  4073. { c->setXmlParams(params); }
  4074. virtual void setXmlParams(IPropertyTree *tree)
  4075. { c->setXmlParams(tree); }
  4076. virtual void setHash(unsigned __int64 hash)
  4077. { c->setHash(hash); }
  4078. // ILocalWorkUnit - used for debugging etc
  4079. virtual void serialize(MemoryBuffer &tgt)
  4080. { c->serialize(tgt); }
  4081. virtual void deserialize(MemoryBuffer &src)
  4082. { c->deserialize(src); }
  4083. virtual bool switchThorQueue(const char *cluster, IQueueSwitcher *qs)
  4084. { return c->switchThorQueue(cluster,qs); }
  4085. virtual void setAllowedClusters(const char *value)
  4086. { c->setAllowedClusters(value); }
  4087. virtual IStringVal& getAllowedClusters(IStringVal &str) const
  4088. { return c->getAllowedClusters(str); }
  4089. virtual void remoteCheckAccess(IUserDescriptor *user, bool writeaccess) const
  4090. { c->remoteCheckAccess(user,writeaccess); }
  4091. virtual void setAllowAutoQueueSwitch(bool val)
  4092. { c->setAllowAutoQueueSwitch(val); }
  4093. virtual bool getAllowAutoQueueSwitch() const
  4094. { return c->getAllowAutoQueueSwitch(); }
  4095. virtual void setLibraryInformation(const char * name, unsigned interfaceHash, unsigned definitionHash)
  4096. { c->setLibraryInformation(name, interfaceHash, definitionHash); }
  4097. virtual void setResultInt(const char * name, unsigned sequence, __int64 val)
  4098. { c->setResultInt(name, sequence, val); }
  4099. virtual void setResultUInt(const char * name, unsigned sequence, unsigned __int64 val)
  4100. { c->setResultUInt(name, sequence, val); }
  4101. virtual void setResultReal(const char *name, unsigned sequence, double val)
  4102. { c->setResultReal(name, sequence, val); }
  4103. virtual void setResultVarString(const char * stepname, unsigned sequence, const char *val)
  4104. { c->setResultVarString(stepname, sequence, val); }
  4105. virtual void setResultVarUnicode(const char * stepname, unsigned sequence, UChar const *val)
  4106. { c->setResultVarUnicode(stepname, sequence, val); }
  4107. virtual void setResultString(const char * stepname, unsigned sequence, int len, const char *val)
  4108. { c->setResultString(stepname, sequence, len, val); }
  4109. virtual void setResultData(const char * stepname, unsigned sequence, int len, const void *val)
  4110. { c->setResultData(stepname, sequence, len, val); }
  4111. virtual void setResultRaw(const char * name, unsigned sequence, int len, const void *val)
  4112. { c->setResultRaw(name, sequence, len, val); }
  4113. virtual void setResultSet(const char * name, unsigned sequence, bool isAll, size32_t len, const void *val, ISetToXmlTransformer *xform)
  4114. { c->setResultSet(name, sequence, isAll, len, val, xform); }
  4115. virtual void setResultUnicode(const char * name, unsigned sequence, int len, UChar const * val)
  4116. { c->setResultUnicode(name, sequence, len, val); }
  4117. virtual void setResultBool(const char *name, unsigned sequence, bool val)
  4118. { c->setResultBool(name, sequence, val); }
  4119. virtual void setResultDecimal(const char *name, unsigned sequence, int len, int precision, bool isSigned, const void *val)
  4120. { c->setResultDecimal(name, sequence, len, precision, isSigned, val); }
  4121. virtual void setResultDataset(const char * name, unsigned sequence, size32_t len, const void *val, unsigned numRows, bool extend)
  4122. { c->setResultDataset(name, sequence, len, val, numRows, extend); }
  4123. };
  4124. IPropertyTree &CDaliWuGraphStats::queryProgressTree()
  4125. {
  4126. conn.setown(owner->getWritableProgressConnection(graphName, wfid));
  4127. return *conn->queryRoot();
  4128. }
  4129. class CLocalWUAssociated : implements IConstWUAssociatedFile, public CInterface
  4130. {
  4131. Owned<IPropertyTree> p;
  4132. public:
  4133. IMPLEMENT_IINTERFACE;
  4134. CLocalWUAssociated(IPropertyTree *p);
  4135. virtual WUFileType getType() const;
  4136. virtual IStringVal & getDescription(IStringVal & ret) const;
  4137. virtual IStringVal & getIp(IStringVal & ret) const;
  4138. virtual IStringVal & getName(IStringVal & ret) const;
  4139. virtual IStringVal & getNameTail(IStringVal & ret) const;
  4140. virtual unsigned getCrc() const;
  4141. virtual unsigned getMinActivityId() const;
  4142. virtual unsigned getMaxActivityId() const;
  4143. };
  4144. class CLocalWUQuery : implements IWUQuery, public CInterface
  4145. {
  4146. Owned<IPropertyTree> p;
  4147. mutable IArrayOf<IConstWUAssociatedFile> associated;
  4148. mutable CriticalSection crit;
  4149. mutable bool associatedCached;
  4150. private:
  4151. void addSpecialCaseAssociated(WUFileType type, const char * propname, unsigned crc) const;
  4152. void loadAssociated() const;
  4153. public:
  4154. IMPLEMENT_IINTERFACE;
  4155. CLocalWUQuery(IPropertyTree *p);
  4156. virtual WUQueryType getQueryType() const;
  4157. virtual IStringVal& getQueryText(IStringVal &str) const;
  4158. virtual IStringVal& getQueryShortText(IStringVal &str) const;
  4159. virtual IStringVal& getQueryName(IStringVal &str) const;
  4160. virtual IStringVal & getQueryMainDefinition(IStringVal & str) const;
  4161. virtual IStringVal& getQueryDllName(IStringVal &str) const;
  4162. virtual unsigned getQueryDllCrc() const;
  4163. virtual IStringVal& getQueryCppName(IStringVal &str) const;
  4164. virtual IStringVal& getQueryResTxtName(IStringVal &str) const;
  4165. virtual IConstWUAssociatedFile * getAssociatedFile(WUFileType type, unsigned index) const;
  4166. virtual IConstWUAssociatedFileIterator& getAssociatedFiles() const;
  4167. virtual bool isArchive() const;
  4168. virtual bool hasArchive() const
  4169. {
  4170. return p->getPropBool("@hasArchive");
  4171. }
  4172. virtual void setQueryType(WUQueryType qt);
  4173. virtual void setQueryText(const char *pstr);
  4174. virtual void setQueryName(const char *);
  4175. virtual void setQueryMainDefinition(const char * str);
  4176. virtual void addAssociatedFile(WUFileType type, const char * name, const char * ip, const char * desc, unsigned crc, unsigned minActivity, unsigned maxActivity);
  4177. virtual void removeAssociatedFiles();
  4178. virtual void removeAssociatedFile(WUFileType type, const char * name, const char * desc);
  4179. };
  4180. class CLocalWUWebServicesInfo : implements IWUWebServicesInfo, public CInterface
  4181. {
  4182. Owned<IPropertyTree> p;
  4183. mutable CriticalSection crit;
  4184. private:
  4185. public:
  4186. IMPLEMENT_IINTERFACE;
  4187. CLocalWUWebServicesInfo(IPropertyTree *p);
  4188. virtual IStringVal& getModuleName(IStringVal &str) const;
  4189. virtual IStringVal& getAttributeName(IStringVal &str) const;
  4190. virtual IStringVal& getDefaultName(IStringVal &str) const;
  4191. virtual IStringVal& getInfo(const char *name, IStringVal &str) const;
  4192. virtual IStringVal& getText(const char *name, IStringVal &str) const;
  4193. virtual unsigned getWebServicesCRC() const;
  4194. virtual void setModuleName(const char *);
  4195. virtual void setAttributeName(const char *);
  4196. virtual void setDefaultName(const char *);
  4197. virtual void setInfo(const char *name, const char *info);
  4198. virtual void setText(const char *name, const char *info);
  4199. virtual void setWebServicesCRC(unsigned);
  4200. };
  4201. class CLocalWUResult : implements IWUResult, public CInterface
  4202. {
  4203. friend class CLocalWorkUnit;
  4204. mutable CriticalSection crit;
  4205. Owned<IPropertyTree> p;
  4206. Owned<IProperties> xmlns;
  4207. public:
  4208. IMPLEMENT_IINTERFACE;
  4209. CLocalWUResult(IPropertyTree *props);
  4210. ~CLocalWUResult() { try { p.clear(); } catch (IException *E) {E->Release();}}
  4211. virtual WUResultStatus getResultStatus() const;
  4212. virtual IStringVal& getResultName(IStringVal &str) const;
  4213. virtual int getResultSequence() const;
  4214. virtual bool isResultScalar() const;
  4215. virtual IStringVal& getResultXml(IStringVal &str, bool hidePasswords) const;
  4216. virtual unsigned getResultFetchSize() const;
  4217. virtual __int64 getResultTotalRowCount() const;
  4218. virtual __int64 getResultRowCount() const;
  4219. virtual void getResultDataset(IStringVal & ecl, IStringVal & defs) const;
  4220. virtual IStringVal& getResultLogicalName(IStringVal &ecl) const;
  4221. virtual IStringVal& getResultKeyField(IStringVal& ecl) const;
  4222. virtual unsigned getResultRequestedRows() const;
  4223. virtual __int64 getResultInt() const;
  4224. virtual bool getResultBool() const;
  4225. virtual double getResultReal() const;
  4226. virtual IStringVal& getResultString(IStringVal & str, bool hidePassword) const;
  4227. virtual IDataVal& getResultRaw(IDataVal & data, IXmlToRawTransformer * xmlTransformer, ICsvToRawTransformer * csvTransformer) const;
  4228. virtual IDataVal& getResultUnicode(IDataVal & data) const;
  4229. virtual void getResultDecimal(void * val, unsigned length, unsigned precision, bool isSigned) const;
  4230. virtual IStringVal& getResultEclSchema(IStringVal & str) const;
  4231. virtual __int64 getResultRawSize(IXmlToRawTransformer * xmlTransformer, ICsvToRawTransformer * csvTransformer) const;
  4232. virtual IDataVal& getResultRaw(IDataVal & data, __int64 from, __int64 length, IXmlToRawTransformer * xmlTransformer, ICsvToRawTransformer * csvTransformer) const;
  4233. virtual IStringVal& getResultRecordSizeEntry(IStringVal & str) const;
  4234. virtual IStringVal& getResultTransformerEntry(IStringVal & str) const;
  4235. virtual __int64 getResultRowLimit() const;
  4236. virtual IStringVal& getResultFilename(IStringVal & str) const;
  4237. virtual WUResultFormat getResultFormat() const;
  4238. virtual unsigned getResultHash() const;
  4239. virtual bool getResultIsAll() const;
  4240. virtual IProperties *queryResultXmlns();
  4241. virtual IStringVal& getResultFieldOpt(const char *name, IStringVal &str) const;
  4242. virtual void getSchema(IArrayOf<ITypeInfo> &types, StringAttrArray &names, IStringVal * ecl=NULL) const;
  4243. virtual void getResultWriteLocation(IStringVal & _graph, unsigned & _activityId) const;
  4244. // interface IWUResult
  4245. virtual void setResultStatus(WUResultStatus status);
  4246. virtual void setResultName(const char *name);
  4247. virtual void setResultSequence(unsigned seq);
  4248. virtual void setResultSchemaRaw(unsigned len, const void *schema);
  4249. virtual void setResultScalar(bool isScalar);
  4250. virtual void setResultRaw(unsigned len, const void *xml, WUResultFormat format);
  4251. virtual void setResultFetchSize(unsigned rows); // 0 means file-loaded
  4252. virtual void setResultTotalRowCount(__int64 rows); // -1 means unknown
  4253. virtual void setResultRowCount(__int64 rows);
  4254. virtual void setResultDataset(const char *ecl, const char *defs);
  4255. virtual void setResultLogicalName(const char *logicalName);
  4256. virtual void setResultKeyField(const char * name);
  4257. virtual void setResultRequestedRows(unsigned req);
  4258. virtual void setResultRecordSizeEntry(const char * val);
  4259. virtual void setResultTransformerEntry(const char * val);
  4260. virtual void setResultInt(__int64 val);
  4261. virtual void setResultReal(double val);
  4262. virtual void setResultBool(bool val);
  4263. virtual void setResultString(const char * val, unsigned length);
  4264. virtual void setResultUnicode(const void * val, unsigned length);
  4265. virtual void setResultData(const void * val, unsigned length);
  4266. virtual void setResultDecimal(const void * val, unsigned length);
  4267. virtual void addResultRaw(unsigned len, const void * data, WUResultFormat format);
  4268. virtual void setResultRowLimit(__int64 value);
  4269. virtual void setResultFilename(const char * name);
  4270. virtual void setResultUInt(unsigned __int64 val);
  4271. virtual void setResultIsAll(bool value);
  4272. virtual void setResultFormat(WUResultFormat format);
  4273. virtual void setResultXML(const char *val);
  4274. virtual void setResultRow(unsigned len, const void * data);
  4275. virtual void setResultXmlns(const char *prefix, const char *uri);
  4276. virtual void setResultFieldOpt(const char *name, const char *value);
  4277. virtual void setResultWriteLocation(const char * _graph, unsigned _activityId);
  4278. virtual IPropertyTree *queryPTree() { return p; }
  4279. };
  4280. class CLocalWUPlugin : implements IWUPlugin, public CInterface
  4281. {
  4282. Owned<IPropertyTree> p;
  4283. public:
  4284. IMPLEMENT_IINTERFACE;
  4285. CLocalWUPlugin(IPropertyTree *p);
  4286. virtual IStringVal& getPluginName(IStringVal &str) const;
  4287. virtual IStringVal& getPluginVersion(IStringVal &str) const;
  4288. virtual void setPluginName(const char *str);
  4289. virtual void setPluginVersion(const char *str);
  4290. };
  4291. class CLocalWULibrary : implements IWULibrary, public CInterface
  4292. {
  4293. Owned<IPropertyTree> p;
  4294. public:
  4295. IMPLEMENT_IINTERFACE;
  4296. CLocalWULibrary(IPropertyTree *p);
  4297. virtual IStringVal & getName(IStringVal & str) const;
  4298. virtual void setName(const char * str);
  4299. };
  4300. class CLocalWUException : implements IWUException, public CInterface
  4301. {
  4302. Owned<IPropertyTree> p;
  4303. public:
  4304. IMPLEMENT_IINTERFACE;
  4305. CLocalWUException(IPropertyTree *p);
  4306. virtual IStringVal& getExceptionSource(IStringVal &str) const override;
  4307. virtual IStringVal& getExceptionMessage(IStringVal &str) const override;
  4308. virtual unsigned getExceptionCode() const override;
  4309. virtual ErrorSeverity getSeverity() const override;
  4310. virtual IStringVal & getTimeStamp(IStringVal & dt) const override;
  4311. virtual IStringVal & getExceptionFileName(IStringVal & str) const override;
  4312. virtual unsigned getExceptionLineNo() const override;
  4313. virtual unsigned getExceptionColumn() const override;
  4314. virtual unsigned getActivityId() const override;
  4315. virtual unsigned getSequence() const override;
  4316. virtual const char * queryScope() const override;
  4317. virtual unsigned getPriority() const override;
  4318. virtual void setExceptionSource(const char *str) override;
  4319. virtual void setExceptionMessage(const char *str) override;
  4320. virtual void setExceptionCode(unsigned code) override;
  4321. virtual void setSeverity(ErrorSeverity level) override;
  4322. virtual void setTimeStamp(const char * dt) override;
  4323. virtual void setExceptionFileName(const char *str) override;
  4324. virtual void setExceptionLineNo(unsigned r) override;
  4325. virtual void setExceptionColumn(unsigned c) override;
  4326. virtual void setActivityId(unsigned _id) override;
  4327. virtual void setScope(const char * _scope) override;
  4328. virtual void setPriority(unsigned _priority) override;
  4329. };
  4330. //==========================================================================================
  4331. extern WORKUNIT_API bool isSpecialResultSequence(unsigned sequence)
  4332. {
  4333. switch ((int) sequence)
  4334. {
  4335. case ResultSequenceInternal:
  4336. case ResultSequenceOnce:
  4337. case ResultSequencePersist:
  4338. case ResultSequenceStored:
  4339. return true;
  4340. default:
  4341. assertex(sequence <= INT_MAX);
  4342. if ((int) sequence >= LibraryBaseSequence)
  4343. return true;
  4344. return false;
  4345. }
  4346. }
  4347. class CConstWUArrayIterator : implements IConstWorkUnitIterator, public CInterface
  4348. {
  4349. unsigned curTreeNum;
  4350. IArrayOf<IPropertyTree> trees;
  4351. Owned<IConstWorkUnitInfo> cur;
  4352. void setCurrent()
  4353. {
  4354. cur.setown(new CLightweightWorkunitInfo(trees.item(curTreeNum)));
  4355. }
  4356. public:
  4357. IMPLEMENT_IINTERFACE;
  4358. CConstWUArrayIterator(IArrayOf<IPropertyTree> &_trees)
  4359. {
  4360. ForEachItemIn(t, _trees)
  4361. trees.append(*LINK(&_trees.item(t)));
  4362. curTreeNum = 0;
  4363. }
  4364. bool first()
  4365. {
  4366. curTreeNum = 0;
  4367. return next();
  4368. }
  4369. bool isValid()
  4370. {
  4371. return (NULL != cur.get());
  4372. }
  4373. bool next()
  4374. {
  4375. if (curTreeNum >= trees.ordinality())
  4376. {
  4377. cur.clear();
  4378. return false;
  4379. }
  4380. setCurrent();
  4381. ++curTreeNum;
  4382. return true;
  4383. }
  4384. IConstWorkUnitInfo & query() { return *cur; }
  4385. };
  4386. class CLocalWUFieldUsage : public CInterface, implements IConstWUFieldUsage
  4387. {
  4388. Owned<IPropertyTree> p;
  4389. public:
  4390. IMPLEMENT_IINTERFACE;
  4391. CLocalWUFieldUsage(IPropertyTree& _p) { p.setown(&_p); }
  4392. virtual const char * queryName() const { return p->queryProp("@name"); }
  4393. };
  4394. class CConstWUFieldUsageIterator : public CInterface, implements IConstWUFieldUsageIterator
  4395. {
  4396. public:
  4397. IMPLEMENT_IINTERFACE;
  4398. CConstWUFieldUsageIterator(IPropertyTreeIterator * tree) { iter.setown(tree); }
  4399. bool first() override { return iter->first(); }
  4400. bool isValid() override { return iter->isValid(); }
  4401. bool next() override { return iter->next(); }
  4402. IConstWUFieldUsage * get() const override { return new CLocalWUFieldUsage(iter->get()); }
  4403. private:
  4404. Owned<IPropertyTreeIterator> iter;
  4405. };
  4406. class CLocalWUFileUsage : public CInterface, implements IConstWUFileUsage
  4407. {
  4408. Owned<IPropertyTree> p;
  4409. public:
  4410. IMPLEMENT_IINTERFACE;
  4411. CLocalWUFileUsage(IPropertyTree& _p) { p.setown(&_p); }
  4412. virtual const char * queryName() const { return p->queryProp("@name"); }
  4413. virtual const char * queryType() const { return p->queryProp("@type"); }
  4414. virtual unsigned getNumFields() const { return p->getPropInt("@numFields"); }
  4415. virtual unsigned getNumFieldsUsed() const { return p->getPropInt("@numFieldsUsed"); }
  4416. virtual IConstWUFieldUsageIterator * getFields() const { return new CConstWUFieldUsageIterator(p->getElements("fields/field")); }
  4417. };
  4418. class CConstWUFileUsageIterator : public CInterface, implements IConstWUFileUsageIterator
  4419. {
  4420. public:
  4421. IMPLEMENT_IINTERFACE;
  4422. CConstWUFileUsageIterator(IPropertyTreeIterator * tree) { iter.setown(tree); }
  4423. bool first() override { return iter->first(); }
  4424. bool isValid() override { return iter->isValid(); }
  4425. bool next() override { return iter->next(); }
  4426. IConstWUFileUsage * get() const override { return new CLocalWUFileUsage(iter->get()); }
  4427. private:
  4428. Owned<IPropertyTreeIterator> iter;
  4429. };
  4430. //==========================================================================================
  4431. class CCachedJobNameIterator : implements IStringIterator, public CInterface
  4432. {
  4433. Owned<IPropertyTreeIterator> it;
  4434. public:
  4435. IMPLEMENT_IINTERFACE;
  4436. CCachedJobNameIterator(IPropertyTreeIterator *p) : it(p) {};
  4437. virtual bool first() { return it->first(); }
  4438. virtual bool next() { return it->next(); }
  4439. virtual bool isValid() { return it->isValid(); }
  4440. virtual IStringVal & str(IStringVal &s) { s.set(it->query().queryName()+1); return s; }
  4441. };
  4442. EnumMapping workunitSortFields[] =
  4443. {
  4444. { WUSFuser, "@submitID" },
  4445. { WUSFcluster, "@clusterName" },
  4446. { WUSFjob, "@jobName" },
  4447. { WUSFstate, "@state" },
  4448. { WUSFpriority, "@priorityClass" },
  4449. { WUSFprotected, "@protected" },
  4450. { WUSFwuid, "@" },
  4451. { WUSFecl, "Query/ShortText" },
  4452. { WUSFfileread, "FilesRead/File/@name" },
  4453. { WUSFtotalthortime, "@totalThorTime" },
  4454. { WUSFwuidhigh, "@" },
  4455. { WUSFwildwuid, "@" },
  4456. { WUSFappvalue, "Application" },
  4457. { WUSFfilewritten, "Files/File/@name" },
  4458. { WUSFterm, NULL }
  4459. };
  4460. extern const char *queryFilterXPath(WUSortField field)
  4461. {
  4462. return getEnumText(field, workunitSortFields);
  4463. }
  4464. EnumMapping querySortFields[] =
  4465. {
  4466. { WUQSFId, "@id" },
  4467. { WUQSFwuid, "@wuid" },
  4468. { WUQSFname, "@name" },
  4469. { WUQSFdll, "@dll" },
  4470. { WUQSFmemoryLimit, "@memoryLimit" },
  4471. { WUQSFmemoryLimitHi, "@memoryLimit" },
  4472. { WUQSFtimeLimit, "@timeLimit" },
  4473. { WUQSFtimeLimitHi, "@timeLimit" },
  4474. { WUQSFwarnTimeLimit, "@warnTimeLimit" },
  4475. { WUQSFwarnTimeLimitHi, "@warnTimeLimit" },
  4476. { WUQSFpriority, "@priority" },
  4477. { WUQSFpriorityHi, "@priority" },
  4478. { WUQSFQuerySet, "@querySetId" },
  4479. { WUQSFActivited, "@activated" },
  4480. { WUQSFSuspendedByUser, "@suspended" },
  4481. { WUQSFLibrary, "Library"},
  4482. { WUQSFPublishedBy, "@publishedBy" },
  4483. { WUQSFterm, NULL }
  4484. };
  4485. class asyncRemoveDllWorkItem: public CInterface, implements IWorkQueueItem // class only used in asyncRemoveDll
  4486. {
  4487. StringAttr name;
  4488. public:
  4489. IMPLEMENT_IINTERFACE;
  4490. asyncRemoveDllWorkItem(const char * _name) : name(_name)
  4491. {
  4492. }
  4493. void execute()
  4494. {
  4495. PROGLOG("WU removeDll %s", name.get());
  4496. queryDllServer().removeDll(name, true, true); // <name>, removeDlls=true, removeDirectory=true
  4497. }
  4498. };
  4499. class asyncRemoveRemoteFileWorkItem: public CInterface, implements IWorkQueueItem // class only used in asyncRemoveFile
  4500. {
  4501. RemoteFilename name;
  4502. public:
  4503. IMPLEMENT_IINTERFACE;
  4504. asyncRemoveRemoteFileWorkItem(const char * _ip, const char * _name)
  4505. {
  4506. SocketEndpoint ep(_ip);
  4507. name.setPath(ep, _name);
  4508. }
  4509. void execute()
  4510. {
  4511. Owned<IFile> file = createIFile(name);
  4512. PROGLOG("WU removeDll %s",file->queryFilename());
  4513. file->remove();
  4514. }
  4515. };
  4516. //==========================================================================================
  4517. class CConstQuerySetQueryIterator : implements IConstQuerySetQueryIterator, public CInterface
  4518. {
  4519. unsigned index;
  4520. IArrayOf<IPropertyTree> trees;
  4521. public:
  4522. IMPLEMENT_IINTERFACE;
  4523. CConstQuerySetQueryIterator(IArrayOf<IPropertyTree> &_trees)
  4524. {
  4525. ForEachItemIn(t, _trees)
  4526. trees.append(*LINK(&_trees.item(t)));
  4527. index = 0;
  4528. }
  4529. ~CConstQuerySetQueryIterator()
  4530. {
  4531. trees.kill();
  4532. }
  4533. bool first()
  4534. {
  4535. index = 0;
  4536. return (trees.ordinality()!=0);
  4537. }
  4538. bool next()
  4539. {
  4540. index++;
  4541. return (index<trees.ordinality());
  4542. }
  4543. bool isValid()
  4544. {
  4545. return (index<trees.ordinality());
  4546. }
  4547. IPropertyTree &query()
  4548. {
  4549. return trees.item(index);
  4550. }
  4551. };
  4552. class CSecurityCache
  4553. {
  4554. };
  4555. class CConstWUIterator : implements IConstWorkUnitIterator, public CInterface
  4556. {
  4557. public:
  4558. IMPLEMENT_IINTERFACE;
  4559. CConstWUIterator(IPropertyTreeIterator *_ptreeIter)
  4560. : ptreeIter(_ptreeIter)
  4561. {
  4562. }
  4563. bool first()
  4564. {
  4565. if (!ptreeIter->first())
  4566. {
  4567. cur.clear();
  4568. return false;
  4569. }
  4570. cur.setown(new CLightweightWorkunitInfo(ptreeIter->query()));
  4571. return true;
  4572. }
  4573. bool isValid()
  4574. {
  4575. return (NULL != cur.get());
  4576. }
  4577. bool next()
  4578. {
  4579. if (!ptreeIter->next())
  4580. {
  4581. cur.clear();
  4582. return false;
  4583. }
  4584. cur.setown(new CLightweightWorkunitInfo(ptreeIter->query()));
  4585. return true;
  4586. }
  4587. IConstWorkUnitInfo & query() { return *cur; }
  4588. private:
  4589. Owned<IConstWorkUnitInfo> cur;
  4590. Owned<IPropertyTreeIterator> ptreeIter;
  4591. };
  4592. class CSecureConstWUIterator : public CInterfaceOf<IConstWorkUnitIterator>
  4593. {
  4594. public:
  4595. CSecureConstWUIterator(IConstWorkUnitIterator *_parent, ISecManager *_secmgr=NULL, ISecUser *_secuser=NULL)
  4596. : parent(_parent), secmgr(_secmgr), secuser(_secuser)
  4597. {
  4598. assertex(_secuser && _secmgr);
  4599. }
  4600. bool first()
  4601. {
  4602. if (!parent->first())
  4603. return false;
  4604. return getNext();
  4605. }
  4606. bool next()
  4607. {
  4608. if (!parent->next())
  4609. return false;
  4610. return getNext();
  4611. }
  4612. virtual bool isValid()
  4613. {
  4614. return parent->isValid();
  4615. }
  4616. virtual IConstWorkUnitInfo &query()
  4617. {
  4618. return parent->query();
  4619. }
  4620. private:
  4621. Owned<IConstWorkUnitIterator> parent;
  4622. MapStringTo<int> scopePermissions;
  4623. Linked<ISecManager> secmgr;
  4624. Linked<ISecUser> secuser;
  4625. bool getNext() // scan for a workunit with permissions
  4626. {
  4627. do
  4628. {
  4629. const char *scopeName = parent->query().queryWuScope();
  4630. if (!scopeName || !*scopeName || checkScope(scopeName))
  4631. return true;
  4632. } while (parent->next());
  4633. return false;
  4634. }
  4635. bool checkScope(const char *scopeName)
  4636. {
  4637. int *perms = scopePermissions.getValue(scopeName);
  4638. SecAccessFlags perm;
  4639. if (!perms)
  4640. {
  4641. perm = secuser.get() ? secmgr->authorizeWorkunitScope(*secuser, scopeName) : SecAccess_Unavailable;
  4642. scopePermissions.setValue(scopeName, perm);
  4643. }
  4644. else
  4645. perm = (SecAccessFlags)*perms;
  4646. return perm >= SecAccess_Read;
  4647. }
  4648. };
  4649. CWorkUnitFactory::CWorkUnitFactory()
  4650. {
  4651. }
  4652. CWorkUnitFactory::~CWorkUnitFactory()
  4653. {
  4654. }
  4655. IWorkUnit* CWorkUnitFactory::createNamedWorkUnit(const char *wuid, const char *app, const char *scope, ISecManager *secmgr, ISecUser *secuser)
  4656. {
  4657. checkWuScopeSecAccess(scope, secmgr, secuser, SecAccess_Write, "Create", true, true);
  4658. Owned<CLocalWorkUnit> cw = _createWorkUnit(wuid, secmgr, secuser);
  4659. if (scope)
  4660. cw->setWuScope(scope); // Note - this may check access rights and throw exception. Is that correct? We might prefer to only check access once, and this will check on the lock too...
  4661. cw->setDistributedAccessToken(secuser ? secuser->getName() : "");//create and sign the workunit distributed access token
  4662. IWorkUnit* ret = &cw->lockRemote(false); // Note - this may throw exception if user does not have rights.
  4663. ret->setDebugValue("CREATED_BY", app, true);
  4664. ret->setDebugValue("CREATED_FOR", scope, true);
  4665. return ret;
  4666. }
  4667. IWorkUnit* CWorkUnitFactory::createWorkUnit(const char *app, const char *scope, ISecManager *secmgr, ISecUser *secuser)
  4668. {
  4669. StringBuffer wuid("W");
  4670. char result[32];
  4671. time_t ltime;
  4672. time( &ltime );
  4673. tm *today = localtime( &ltime ); // MORE - this is not threadsafe. But I probably don't care that much!
  4674. strftime(result, sizeof(result), "%Y%m%d-%H%M%S", today);
  4675. wuid.append(result);
  4676. if (workUnitTraceLevel > 1)
  4677. DBGLOG("createWorkUnit created %s", wuid.str());
  4678. IWorkUnit* ret = createNamedWorkUnit(wuid.str(), app, scope, secmgr, secuser);
  4679. if (workUnitTraceLevel > 1)
  4680. DBGLOG("createWorkUnit created %s", ret->queryWuid());
  4681. addTimeStamp(ret, SSTglobal, NULL, StWhenCreated);
  4682. return ret;
  4683. }
  4684. bool CWorkUnitFactory::deleteWorkUnitEx(const char * wuid, bool throwException, ISecManager *secmgr, ISecUser *secuser)
  4685. {
  4686. if (workUnitTraceLevel > 1)
  4687. DBGLOG("deleteWorkUnit %s", wuid);
  4688. StringBuffer wuRoot;
  4689. getXPath(wuRoot, wuid);
  4690. Owned<CLocalWorkUnit> cw = _updateWorkUnit(wuid, secmgr, secuser);
  4691. if (!checkWuSecAccess(*cw.get(), secmgr, secuser, SecAccess_Full, "delete", true, true))
  4692. return false;
  4693. if (throwException)
  4694. cw->cleanupAndDelete(true, true);
  4695. else
  4696. {
  4697. try
  4698. {
  4699. cw->cleanupAndDelete(true, true);
  4700. }
  4701. catch (IException *E)
  4702. {
  4703. StringBuffer s;
  4704. LOG(MCexception(E, MSGCLS_warning), E, s.append("Exception during deleteWorkUnit: ").append(wuid).str());
  4705. E->Release();
  4706. return false;
  4707. }
  4708. }
  4709. removeWorkUnitFromAllQueues(wuid); //known active workunits wouldn't make it this far
  4710. return true;
  4711. }
  4712. bool CWorkUnitFactory::deleteWorkUnit(const char * wuid, ISecManager *secmgr, ISecUser *secuser)
  4713. {
  4714. return deleteWorkUnitEx(wuid, false, secmgr, secuser);
  4715. }
  4716. IConstWorkUnit* CWorkUnitFactory::openWorkUnit(const char *wuid, ISecManager *secmgr, ISecUser *secuser)
  4717. {
  4718. StringBuffer wuidStr(wuid);
  4719. wuidStr.trim();
  4720. if (wuidStr.length() && ('w' == wuidStr.charAt(0)))
  4721. wuidStr.setCharAt(0, 'W');
  4722. if (!wuidStr.length() || ('W' != wuidStr.charAt(0)))
  4723. {
  4724. if (workUnitTraceLevel > 1)
  4725. DBGLOG("openWorkUnit %s invalid WUID", nullText(wuidStr.str()));
  4726. return NULL;
  4727. }
  4728. if (workUnitTraceLevel > 1)
  4729. DBGLOG("openWorkUnit %s", wuidStr.str());
  4730. Owned<IConstWorkUnit> wu = _openWorkUnit(wuid, secmgr, secuser);
  4731. if (wu)
  4732. {
  4733. if (!checkWuSecAccess(*wu, secmgr, secuser, SecAccess_Read, "opening", true, true))
  4734. return NULL; // Actually throws exception on failure, so won't reach here
  4735. return wu.getClear();
  4736. }
  4737. else
  4738. {
  4739. if (workUnitTraceLevel > 0)
  4740. IERRLOG("openWorkUnit %s not found", wuidStr.str());
  4741. return NULL;
  4742. }
  4743. }
  4744. IWorkUnit* CWorkUnitFactory::updateWorkUnit(const char *wuid, ISecManager *secmgr, ISecUser *secuser)
  4745. {
  4746. if (workUnitTraceLevel > 1)
  4747. DBGLOG("updateWorkUnit %s", wuid);
  4748. Owned<CLocalWorkUnit> wu = _updateWorkUnit(wuid, secmgr, secuser);
  4749. if (wu)
  4750. {
  4751. if (!checkWuSecAccess(*wu.get(), secmgr, secuser, SecAccess_Write, "updating", true, true))
  4752. return NULL;
  4753. return &wu->lockRemote(false);
  4754. }
  4755. else
  4756. {
  4757. if (workUnitTraceLevel > 0)
  4758. IERRLOG("updateWorkUnit %s not found", wuid);
  4759. return NULL;
  4760. }
  4761. }
  4762. IPropertyTree * pruneBranch(IPropertyTree * from, char const * xpath)
  4763. {
  4764. Owned<IPropertyTree> ret;
  4765. IPropertyTree * branch = from->queryPropTree(xpath);
  4766. if(branch)
  4767. {
  4768. ret.setown(createPTreeFromIPT(branch));
  4769. from->removeTree(branch);
  4770. }
  4771. return ret.getClear();
  4772. }
  4773. bool CWorkUnitFactory::restoreWorkUnit(const char *base, const char *wuid, bool restoreAssociated)
  4774. {
  4775. StringBuffer path(base);
  4776. addPathSepChar(path).append(wuid).append(".xml");
  4777. Owned<IPTree> pt = createPTreeFromXMLFile(path);
  4778. if (!pt)
  4779. return false;
  4780. CDateTime dt;
  4781. dt.setNow();
  4782. StringBuffer dts;
  4783. dt.getString(dts);
  4784. pt->setProp("@restoredDate", dts.str());
  4785. Owned<IPropertyTree> generatedDlls = pruneBranch(pt, "GeneratedDlls[1]");
  4786. Owned<IPropertyTree> associatedFiles;
  4787. IPropertyTree *srcAssociated = pt->queryPropTree("Query/Associated");
  4788. if (srcAssociated)
  4789. associatedFiles.setown(createPTreeFromIPT(srcAssociated));
  4790. // The updating of the repo is implementation specific...
  4791. if (!_restoreWorkUnit(pt.getClear(), wuid))
  4792. return false;
  4793. // now kludge back GeneratedDlls
  4794. if (generatedDlls)
  4795. {
  4796. Owned<IPropertyTreeIterator> dlls = generatedDlls->getElements("GeneratedDll");
  4797. for(dlls->first(); dlls->isValid(); dlls->next())
  4798. {
  4799. IPropertyTree & dll = dlls->query();
  4800. char const * name = dll.queryProp("@name");
  4801. char const * kind = dll.queryProp("@kind");
  4802. char const * location = dll.queryProp("@location");
  4803. Owned<IDllEntry> got = queryDllServer().getEntry(name);
  4804. if (!got)
  4805. {
  4806. RemoteFilename dstRfn;
  4807. dstRfn.setRemotePath(location);
  4808. StringBuffer srcPath(base);
  4809. addPathSepChar(srcPath);
  4810. dstRfn.getTail(srcPath);
  4811. OwnedIFile srcFile = createIFile(srcPath);
  4812. OwnedIFile dstFile = createIFile(dstRfn);
  4813. copyFile(dstFile, srcFile);
  4814. queryDllServer().registerDll(name, kind, location);
  4815. }
  4816. }
  4817. }
  4818. if (associatedFiles)
  4819. {
  4820. Owned<IPropertyTreeIterator> associated = associatedFiles->getElements("*");
  4821. ForEach(*associated)
  4822. {
  4823. IPropertyTree &file = associated->query();
  4824. const char *filename = file.queryProp("@filename");
  4825. SocketEndpoint ep(file.queryProp("@ip"));
  4826. RemoteFilename rfn;
  4827. rfn.setPath(ep, filename);
  4828. OwnedIFile dstFile = createIFile(rfn);
  4829. StringBuffer srcPath(base), name;
  4830. addPathSepChar(srcPath);
  4831. rfn.getTail(name);
  4832. srcPath.append(name);
  4833. if (generatedDlls)
  4834. {
  4835. VStringBuffer gDllPath("GeneratedDll[@name=\"%s\"]", name.str());
  4836. if (generatedDlls->hasProp(gDllPath))
  4837. continue; // generated dlls handled separately - see above
  4838. }
  4839. OwnedIFile srcFile = createIFile(srcPath);
  4840. if (srcFile->exists())
  4841. {
  4842. try
  4843. {
  4844. copyFile(dstFile, srcFile);
  4845. }
  4846. catch (IException *e)
  4847. {
  4848. VStringBuffer msg("Failed to restore associated file '%s' to destination '%s'", srcFile->queryFilename(), dstFile->queryFilename());
  4849. EXCLOG(e, msg.str());
  4850. e->Release();
  4851. }
  4852. }
  4853. }
  4854. }
  4855. return true;
  4856. }
  4857. void CWorkUnitFactory::importWorkUnit(const char *zapReportFileName, const char *zapReportPassword,
  4858. const char *importDir, const char *app, const char *user, ISecManager *secMgr, ISecUser *secUser)
  4859. {
  4860. class CImportWorkUnitHelper
  4861. {
  4862. StringAttr zapReportFileName, zapReportPassword, importDir, user, wuid, fromWUID, scope;
  4863. StringBuffer unzipDir;
  4864. Owned<IPTree> wuTree, graphProgressTree;
  4865. bool findZAPFile(const char *mask, bool optional, StringBuffer &fileName)
  4866. {
  4867. Owned<IFile> d = createIFile(unzipDir);
  4868. if (!d->exists())
  4869. throw MakeStringException(WUERR_InvalidUserInput, "%s not found.", unzipDir.str());
  4870. Owned<IDirectoryIterator> di = d->directoryFiles(mask, false, false);
  4871. if (!di->first())
  4872. {
  4873. if (optional)
  4874. return false;
  4875. throw MakeStringException(WUERR_InvalidUserInput, "Failed to find %s in %s.", mask, unzipDir.str());
  4876. }
  4877. fileName.set(unzipDir).append(PATHSEPSTR);
  4878. di->getName(fileName);
  4879. if (di->next())
  4880. throw MakeStringException(WUERR_InvalidUserInput, "More than 1 %s files found in %s.", mask, unzipDir.str());
  4881. return true;
  4882. }
  4883. IPTree *readWUXMLFile(const char *pattern, bool optional)
  4884. {
  4885. StringBuffer fileName;
  4886. if (!findZAPFile(pattern, optional, fileName))
  4887. return nullptr;
  4888. Owned<IPTree> tree = createPTreeFromXMLFile(fileName);
  4889. if (!tree)
  4890. throw MakeStringException(WUERR_InvalidUserInput, "Failed to retrieve %s.", pattern);
  4891. Owned<IFile> f = createIFile(fileName);
  4892. f->remove();
  4893. return tree.getClear();
  4894. }
  4895. void updateWUQueryAssociatedFilesAttrs(const char *localIP)
  4896. {
  4897. ICopyArrayOf<IPropertyTree> fileTreesToRemove;
  4898. IPropertyTree *queryAssociatedTreeRoot = wuTree->queryPropTree("Query/Associated");
  4899. Owned<IPropertyTreeIterator> itr = queryAssociatedTreeRoot->getElements("File");
  4900. ForEach (*itr)
  4901. {
  4902. IPropertyTree &fileTree = itr->query();
  4903. const char *fileName = fileTree.queryProp("@filename");
  4904. StringBuffer fileNameWithPath(unzipDir);
  4905. fileNameWithPath.append(PATHSEPSTR).append(pathTail(fileName));
  4906. if (checkFileExists(fileNameWithPath)) //Check if the ZAP report contains this file.
  4907. {
  4908. fileTree.setProp("@ip", localIP);
  4909. fileTree.setProp("@filename", fileNameWithPath);
  4910. }
  4911. else
  4912. fileTreesToRemove.append(fileTree);
  4913. }
  4914. ForEachItemIn(r, fileTreesToRemove)
  4915. queryAssociatedTreeRoot->removeTree(&fileTreesToRemove.item(r));
  4916. }
  4917. void getWUAttributes(IWorkUnit *workunit)
  4918. {
  4919. wuid.set(workunit->queryWuid());
  4920. scope.set(workunit->queryWuScope());
  4921. }
  4922. IPTree *queryWUPTree() const
  4923. {
  4924. return wuTree;
  4925. }
  4926. IPTree *queryGraphProgressPTree() const
  4927. {
  4928. return graphProgressTree;
  4929. }
  4930. void setUNZIPDir()
  4931. { //Set a unique unzip folder inside the component's data folder
  4932. unzipDir.append(importDir).append(PATHSEPSTR).append(wuid.get());
  4933. }
  4934. void unzipZAPReport()
  4935. {
  4936. Owned<IFile> unzipFolder = createIFile(unzipDir);
  4937. if (!unzipFolder->exists())
  4938. unzipFolder->createDirectory();
  4939. else
  4940. {//This should not happen. Just in case.
  4941. throw MakeStringException(WUERR_CannotImportWorkunit, "Workunit %s had been created twice and passed to import()..", wuid.get());
  4942. }
  4943. //Unzip ZAP Report
  4944. VStringBuffer zipCommand("unzip %s -d %s", zapReportFileName.get(), unzipDir.str());
  4945. if (!isEmptyString(zapReportPassword))
  4946. zipCommand.appendf(" -P %s", zapReportPassword.get());
  4947. DWORD runcode;
  4948. bool success = invoke_program(zipCommand.str(), runcode, true);
  4949. if (!success)
  4950. throw MakeStringException(WUERR_CannotImportWorkunit, "Failed in execution : %s.", zipCommand.str());
  4951. if (runcode != 0)
  4952. throw MakeStringException(WUERR_CannotImportWorkunit, "Failed in execution : %s, error(%" I64F "i)", zipCommand.str(), (unsigned __int64) runcode);
  4953. }
  4954. void buildPTreesFromZAPReport()
  4955. {
  4956. wuTree.setown(readWUXMLFile("ZAPReport_W*.xml", false));
  4957. fromWUID.set(wuTree->queryName());
  4958. wuTree->setProp("@scope", scope);
  4959. //update QueryAssociatedFiles in WU XML;
  4960. updateWUQueryAssociatedFilesAttrs(GetCachedHostName());
  4961. graphProgressTree.setown(readWUXMLFile("ZAPReport_*.graphprogress", true));
  4962. }
  4963. void updateJobName(IWorkUnit *workunit)
  4964. {
  4965. StringBuffer newJobName;
  4966. const char *jobName = workunit->queryJobName();
  4967. if (isEmptyString(jobName))
  4968. newJobName.appendf("IMPORTED from %s", fromWUID.get());
  4969. else
  4970. newJobName.appendf("IMPORTED: %s", jobName);
  4971. workunit->setJobName(newJobName);
  4972. }
  4973. void setImportDebugAttribute(IWorkUnit *workunit)
  4974. {
  4975. StringBuffer attr, importDateTime;
  4976. CDateTime dt;
  4977. dt.setNow();
  4978. dt.getString(importDateTime);
  4979. attr.append("FromWUID=").append(fromWUID).append(",");
  4980. attr.append("ImportDT=").append(importDateTime).append(",");
  4981. attr.append("ZAPReport=").append(pathTail(zapReportFileName));
  4982. workunit->setDebugValue("imported", attr, true);
  4983. }
  4984. public:
  4985. CImportWorkUnitHelper(const char *_zapReportFileName, const char *_zapReportPassword, const char *_importDir, const char *_user)
  4986. : zapReportFileName(_zapReportFileName), zapReportPassword(_zapReportPassword), importDir(_importDir), user(_user) { };
  4987. void import(IWorkUnit *workunit)
  4988. {
  4989. getWUAttributes(workunit);
  4990. setUNZIPDir();
  4991. unzipZAPReport();
  4992. buildPTreesFromZAPReport();
  4993. workunit->import(queryWUPTree(), queryGraphProgressPTree());
  4994. setImportDebugAttribute(workunit);
  4995. updateJobName(workunit);
  4996. removeFileTraceIfFail(zapReportFileName);
  4997. }
  4998. };
  4999. Owned<IWorkUnit> newWU = createWorkUnit(app, user, secMgr, secUser);
  5000. CImportWorkUnitHelper helper(zapReportFileName, zapReportPassword, importDir, user);
  5001. helper.import(newWU);
  5002. }
  5003. int CWorkUnitFactory::setTracingLevel(int newLevel)
  5004. {
  5005. if (newLevel)
  5006. DBGLOG("Setting workunit trace level to %d", newLevel);
  5007. int level = workUnitTraceLevel;
  5008. workUnitTraceLevel = newLevel;
  5009. return level;
  5010. }
  5011. void CWorkUnitFactory::descheduleAllWorkUnits(ISecManager *secmgr, ISecUser *secuser)
  5012. {
  5013. Owned<IRemoteConnection> conn = querySDS().connect("/Schedule", myProcessSession(), RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  5014. if(!conn) return;
  5015. Owned<IPropertyTree> root(conn->queryRoot()->getBranch("."));
  5016. KeptAtomTable entries;
  5017. Owned<IPropertyTreeIterator> iter(root->getElements("*/*/*/*"));
  5018. StringBuffer wuid;
  5019. for(iter->first(); iter->isValid(); iter->next())
  5020. {
  5021. char const * entry = iter->query().queryName();
  5022. if(!entries.find(entry))
  5023. {
  5024. entries.addAtom(entry);
  5025. ncnameUnescape(entry, wuid.clear());
  5026. Owned<IWorkUnit> wu = updateWorkUnit(wuid, secmgr, secuser);
  5027. if(wu && (wu->getState() == WUStateWait))
  5028. wu->setState(WUStateCompleted);
  5029. }
  5030. }
  5031. bool more;
  5032. do more = root->removeProp("*"); while(more);
  5033. }
  5034. IConstQuerySetQueryIterator* CWorkUnitFactory::getQuerySetQueriesSorted( WUQuerySortField *sortorder, // list of fields to sort by (terminated by WUSFterm)
  5035. WUQuerySortField *filters, // NULL or list of fields to filter on (terminated by WUSFterm)
  5036. const void *filterbuf, // (appended) string values for filters
  5037. unsigned startoffset,
  5038. unsigned maxnum,
  5039. __int64 *cachehint,
  5040. unsigned *total,
  5041. const MapStringTo<bool> *_subset,
  5042. const MapStringTo<bool> *_suspendedQueriesByCluster)
  5043. {
  5044. struct PostFilters
  5045. {
  5046. WUQueryFilterBoolean activatedFilter;
  5047. WUQueryFilterSuspended suspendedFilter;
  5048. PostFilters()
  5049. {
  5050. activatedFilter = WUQFSAll;
  5051. suspendedFilter = WUQFAllQueries;
  5052. };
  5053. } postFilters;
  5054. class CQuerySetQueriesPager : public CSimpleInterface, implements IElementsPager
  5055. {
  5056. StringAttr querySet;
  5057. StringAttr xPath;
  5058. StringAttr sortOrder;
  5059. PostFilters postFilters;
  5060. StringArray unknownAttributes;
  5061. const MapStringTo<bool> *subset;
  5062. const MapStringTo<bool> *suspendedQueriesByCluster;
  5063. void populateQueryTree(const IPropertyTree* querySetTree, IPropertyTree* queryTree)
  5064. {
  5065. const char* querySetId = querySetTree->queryProp("@id");
  5066. std::unordered_set<std::string> aliasSet;
  5067. Owned<IPropertyTreeIterator> aliasIter = querySetTree->getElements("Alias");
  5068. ForEach(*aliasIter)
  5069. {
  5070. const char *id = aliasIter->query().queryProp("@id");
  5071. if (!isEmptyString(id))
  5072. aliasSet.insert(id);
  5073. }
  5074. VStringBuffer path("Query%s", xPath.get());
  5075. Owned<IPropertyTreeIterator> iter = querySetTree->getElements(path.str());
  5076. ForEach(*iter)
  5077. {
  5078. IPropertyTree &query = iter->query();
  5079. bool activated = false;
  5080. const char* queryId = query.queryProp("@id");
  5081. if (queryId && *queryId)
  5082. {
  5083. if (subset)
  5084. {
  5085. VStringBuffer match("%s/%s", querySetId, queryId);
  5086. if (!subset->getValue(match))
  5087. continue;
  5088. }
  5089. if (aliasSet.find(queryId) != aliasSet.end())
  5090. activated = true;
  5091. }
  5092. if (activated && (postFilters.activatedFilter == WUQFSNo))
  5093. continue;
  5094. if (!activated && (postFilters.activatedFilter == WUQFSYes))
  5095. continue;
  5096. bool isSuspendedByUser = query.hasProp(getEnumText(WUQSFSuspendedByUser,querySortFields));
  5097. bool skip = false;
  5098. switch(postFilters.suspendedFilter)
  5099. {
  5100. case WUQFSUSPDNo:
  5101. if (isSuspendedByUser || checkSuspendedByCluster(querySetId, queryId))
  5102. skip = true;
  5103. break;
  5104. case WUQFSUSPDYes:
  5105. if (!isSuspendedByUser && !checkSuspendedByCluster(querySetId, queryId))
  5106. skip = true;
  5107. break;
  5108. case WUQFSUSPDByUser:
  5109. if (!isSuspendedByUser)
  5110. skip = true;
  5111. break;
  5112. case WUQFSUSPDByFirstNode:
  5113. case WUQFSUSPDByAnyNode:
  5114. if (!checkSuspendedByCluster(querySetId, queryId))
  5115. skip = true;
  5116. break;
  5117. }
  5118. if (skip)
  5119. continue;
  5120. IPropertyTree *queryWithSetId = queryTree->addPropTree("Query", createPTreeFromIPT(&query));
  5121. queryWithSetId->setProp("@querySetId", querySetId);
  5122. queryWithSetId->setPropBool("@activated", activated);
  5123. }
  5124. }
  5125. IRemoteConnection* populateQueryTree(IPropertyTree* queryTree)
  5126. {
  5127. StringBuffer querySetXPath("QuerySets");
  5128. if (!querySet.isEmpty())
  5129. querySetXPath.appendf("/QuerySet[@id=\"%s\"]", querySet.get());
  5130. Owned<IRemoteConnection> conn = querySDS().connect(querySetXPath.str(), myProcessSession(), 0, SDS_LOCK_TIMEOUT);
  5131. if (!conn)
  5132. return NULL;
  5133. IPropertyTree *root = conn->queryRoot()->queryBranch(nullptr);
  5134. if (querySet.isEmpty())
  5135. {
  5136. Owned<IPropertyTreeIterator> querySetIter = root->getElements("*");
  5137. ForEach(*querySetIter)
  5138. populateQueryTree(&querySetIter->query(), queryTree);
  5139. }
  5140. else
  5141. populateQueryTree(root, queryTree);
  5142. return conn.getClear();
  5143. }
  5144. bool checkSuspendedByCluster(const char *querySetId, const char *queryId)
  5145. {
  5146. if (!suspendedQueriesByCluster)
  5147. return false;
  5148. VStringBuffer match("%s/%s", querySetId, queryId);
  5149. bool *found = suspendedQueriesByCluster->getValue(match);
  5150. return (found && *found);
  5151. }
  5152. public:
  5153. IMPLEMENT_IINTERFACE_USING(CSimpleInterface);
  5154. CQuerySetQueriesPager(const char* _querySet, const char* _xPath, const char *_sortOrder, PostFilters& _postFilters,
  5155. StringArray& _unknownAttributes, const MapStringTo<bool> *_subset, const MapStringTo<bool> *_suspendedQueriesByCluster)
  5156. : querySet(_querySet), xPath(_xPath), sortOrder(_sortOrder), subset(_subset), suspendedQueriesByCluster(_suspendedQueriesByCluster)
  5157. {
  5158. postFilters.activatedFilter = _postFilters.activatedFilter;
  5159. postFilters.suspendedFilter = _postFilters.suspendedFilter;
  5160. ForEachItemIn(x, _unknownAttributes)
  5161. unknownAttributes.append(_unknownAttributes.item(x));
  5162. }
  5163. virtual IRemoteConnection* getElements(IArrayOf<IPropertyTree> &elements)
  5164. {
  5165. Owned<IPropertyTree> elementTree = createPTree("Queries");
  5166. Owned<IRemoteConnection> conn = populateQueryTree(elementTree);
  5167. if (!conn)
  5168. return NULL;
  5169. Owned<IPropertyTreeIterator> iter = elementTree->getElements("*");
  5170. if (!iter)
  5171. return NULL;
  5172. sortElements(iter, sortOrder.get(), NULL, NULL, unknownAttributes, elements);
  5173. return conn.getClear();
  5174. }
  5175. virtual bool allMatchingElementsReceived() { return true; } //For now, dali always returns all of matched Queries.
  5176. };
  5177. StringAttr querySet;
  5178. StringBuffer xPath;
  5179. StringBuffer so;
  5180. StringArray unknownAttributes;
  5181. if (filters)
  5182. {
  5183. const char *fv = (const char *)filterbuf;
  5184. for (unsigned i=0;filters[i]!=WUQSFterm;i++) {
  5185. int fmt = filters[i];
  5186. int subfmt = (fmt&0xff);
  5187. if (subfmt==WUQSFQuerySet)
  5188. querySet.set(fv);
  5189. else if ((subfmt==WUQSFmemoryLimit) || (subfmt==WUQSFtimeLimit) || (subfmt==WUQSFwarnTimeLimit) || (subfmt==WUQSFpriority))
  5190. xPath.append('[').append(getEnumText(subfmt,querySortFields)).append(">=").append(fv).append("]");
  5191. else if ((subfmt==WUQSFmemoryLimitHi) || (subfmt==WUQSFtimeLimitHi) || (subfmt==WUQSFwarnTimeLimitHi) || (subfmt==WUQSFpriorityHi))
  5192. xPath.append('[').append(getEnumText(subfmt,querySortFields)).append("<=").append(fv).append("]");
  5193. else if (subfmt==WUQSFActivited)
  5194. postFilters.activatedFilter = (WUQueryFilterBoolean) atoi(fv);
  5195. else if (subfmt==WUQSFSuspendedFilter)
  5196. postFilters.suspendedFilter = (WUQueryFilterSuspended) atoi(fv);
  5197. else if (!fv || !*fv)
  5198. unknownAttributes.append(getEnumText(subfmt,querySortFields));
  5199. else {
  5200. xPath.append('[').append(getEnumText(subfmt,querySortFields)).append('=');
  5201. if (fmt&WUQSFnocase)
  5202. xPath.append('?');
  5203. if (fmt&WUQSFnumeric)
  5204. xPath.append('#');
  5205. if (fmt&WUQSFwild)
  5206. xPath.append('~');
  5207. xPath.append('"').append(fv).append("\"]");
  5208. }
  5209. fv = fv + strlen(fv)+1;
  5210. }
  5211. }
  5212. if (sortorder) {
  5213. for (unsigned i=0;sortorder[i]!=WUQSFterm;i++) {
  5214. if (so.length())
  5215. so.append(',');
  5216. int fmt = sortorder[i];
  5217. if (fmt&WUQSFreverse)
  5218. so.append('-');
  5219. if (fmt&WUQSFnocase)
  5220. so.append('?');
  5221. if (fmt&WUQSFnumeric)
  5222. so.append('#');
  5223. so.append(getEnumText(fmt&0xff,querySortFields));
  5224. }
  5225. }
  5226. IArrayOf<IPropertyTree> results;
  5227. Owned<IElementsPager> elementsPager = new CQuerySetQueriesPager(querySet.get(), xPath.str(), so.length()?so.str():NULL,
  5228. postFilters, unknownAttributes, _subset, _suspendedQueriesByCluster);
  5229. Owned<IRemoteConnection> conn=getElementsPaged(elementsPager,startoffset,maxnum,NULL,"",cachehint,results,total,NULL);
  5230. return new CConstQuerySetQueryIterator(results);
  5231. }
  5232. bool CWorkUnitFactory::isAborting(const char *wuid) const
  5233. {
  5234. VStringBuffer apath("/WorkUnitAborts/%s", wuid);
  5235. try
  5236. {
  5237. Owned<IRemoteConnection> acon = querySDS().connect(apath.str(), myProcessSession(), 0, SDS_LOCK_TIMEOUT);
  5238. if (acon)
  5239. return acon->queryRoot()->getPropInt(NULL) != 0;
  5240. }
  5241. catch (IException *E)
  5242. {
  5243. EXCLOG(E);
  5244. E->Release();
  5245. }
  5246. return false;
  5247. }
  5248. void CWorkUnitFactory::clearAborting(const char *wuid)
  5249. {
  5250. VStringBuffer apath("/WorkUnitAborts/%s", wuid);
  5251. try
  5252. {
  5253. Owned<IRemoteConnection> acon = querySDS().connect(apath.str(), myProcessSession(), RTM_LOCK_WRITE|RTM_LOCK_SUB, SDS_LOCK_TIMEOUT);
  5254. if (acon)
  5255. acon->close(true);
  5256. }
  5257. catch (IException *E)
  5258. {
  5259. EXCLOG(E);
  5260. E->Release();
  5261. }
  5262. }
  5263. void CWorkUnitFactory::reportAbnormalTermination(const char *wuid, WUState &state, SessionId agent)
  5264. {
  5265. WARNLOG("reportAbnormalTermination: session stopped unexpectedly: %" I64F "d state: %d", (__int64) agent, (int) state);
  5266. bool isEcl = false;
  5267. switch (state)
  5268. {
  5269. case WUStateAborting:
  5270. state = WUStateAborted;
  5271. break;
  5272. case WUStateCompiling:
  5273. isEcl = true;
  5274. // drop into
  5275. default:
  5276. state = WUStateFailed;
  5277. }
  5278. Owned<IWorkUnit> wu = updateWorkUnit(wuid, NULL, NULL);
  5279. wu->setState(state);
  5280. Owned<IWUException> e = wu->createException();
  5281. e->setExceptionCode(isEcl ? 1001 : 1000);
  5282. e->setExceptionMessage(isEcl ? "EclCC terminated unexpectedly" : "Workunit terminated unexpectedly");
  5283. }
  5284. static CriticalSection deleteDllLock;
  5285. static IWorkQueueThread *deleteDllWorkQ = nullptr;
  5286. MODULE_INIT(INIT_PRIORITY_STANDARD)
  5287. {
  5288. return true;
  5289. }
  5290. MODULE_EXIT()
  5291. {
  5292. CriticalBlock b(deleteDllLock);
  5293. if (deleteDllWorkQ)
  5294. ::Release(deleteDllWorkQ);
  5295. deleteDllWorkQ = nullptr;
  5296. }
  5297. static void asyncRemoveDll(const char * name)
  5298. {
  5299. CriticalBlock b(deleteDllLock);
  5300. if (!deleteDllWorkQ)
  5301. deleteDllWorkQ = createWorkQueueThread();
  5302. deleteDllWorkQ->post(new asyncRemoveDllWorkItem(name));
  5303. }
  5304. static void asyncRemoveFile(const char * ip, const char * name)
  5305. {
  5306. CriticalBlock b(deleteDllLock);
  5307. if (!deleteDllWorkQ)
  5308. deleteDllWorkQ = createWorkQueueThread();
  5309. deleteDllWorkQ->post(new asyncRemoveRemoteFileWorkItem(ip, name));
  5310. }
  5311. class CDaliWorkUnitFactory : public CWorkUnitFactory, implements IDaliClientShutdown
  5312. {
  5313. public:
  5314. IMPLEMENT_IINTERFACE_USING(CWorkUnitFactory);
  5315. CDaliWorkUnitFactory()
  5316. {
  5317. // Assumes dali client configuration has already been done
  5318. sdsManager = &querySDS();
  5319. session = myProcessSession();
  5320. addShutdownHook(*this);
  5321. }
  5322. ~CDaliWorkUnitFactory()
  5323. {
  5324. removeShutdownHook(*this);
  5325. }
  5326. virtual bool initializeStore()
  5327. {
  5328. throwUnexpected(); // Used when loading a plugin factory - not applicable here
  5329. }
  5330. virtual IWorkUnitWatcher *getWatcher(IWorkUnitSubscriber *subscriber, WUSubscribeOptions options, const char *wuid) const
  5331. {
  5332. return new CDaliWorkUnitWatcher(subscriber, options, wuid);
  5333. }
  5334. virtual unsigned validateRepository(bool fixErrors)
  5335. {
  5336. return 0;
  5337. }
  5338. virtual void deleteRepository(bool recreate)
  5339. {
  5340. Owned<IRemoteConnection> conn = sdsManager->connect("/WorkUnits", session, RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  5341. if (conn)
  5342. conn->close(true);
  5343. conn.setown(sdsManager->connect("/GraphProgress", session, RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT));
  5344. if (conn)
  5345. conn->close(true);
  5346. }
  5347. virtual void createRepository()
  5348. {
  5349. // Nothing to do
  5350. }
  5351. virtual const char *queryStoreType() const
  5352. {
  5353. return "Dali";
  5354. }
  5355. virtual CLocalWorkUnit *_createWorkUnit(const char *wuid, ISecManager *secmgr, ISecUser *secuser)
  5356. {
  5357. StringBuffer wuRoot;
  5358. getXPath(wuRoot, wuid);
  5359. IRemoteConnection *conn;
  5360. conn = sdsManager->connect(wuRoot.str(), session, RTM_LOCK_WRITE|RTM_CREATE_UNIQUE, SDS_LOCK_TIMEOUT);
  5361. conn->queryRoot()->setProp("@xmlns:xsi", "http://www.w3.org/1999/XMLSchema-instance");
  5362. conn->queryRoot()->setPropInt("@wuidVersion", WUID_VERSION);
  5363. conn->queryRoot()->setProp("@totalThorTime", "");
  5364. return new CDaliWorkUnit(conn, secmgr, secuser);
  5365. }
  5366. virtual CLocalWorkUnit* _openWorkUnit(const char *wuid, ISecManager *secmgr, ISecUser *secuser)
  5367. {
  5368. StringBuffer wuRoot;
  5369. getXPath(wuRoot, wuid);
  5370. IRemoteConnection* conn = sdsManager->connect(wuRoot.str(), session, 0, SDS_LOCK_TIMEOUT);
  5371. if (conn)
  5372. return new CDaliWorkUnit(conn, secmgr, secuser);
  5373. else
  5374. return NULL;
  5375. }
  5376. virtual bool _restoreWorkUnit(IPTree *_pt, const char *wuid)
  5377. {
  5378. Owned<IPTree> pt(_pt);
  5379. Owned<IPropertyTree> gprogress = pruneBranch(pt, "GraphProgress[1]");
  5380. StringBuffer wuRoot;
  5381. getXPath(wuRoot, wuid);
  5382. Owned<IRemoteConnection> conn = sdsManager->connect(wuRoot.str(), myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  5383. if (!conn)
  5384. {
  5385. ERRLOG("restoreWorkUnit could not create to %s", wuRoot.str());
  5386. return false;
  5387. }
  5388. IPropertyTree *root = conn->queryRoot();
  5389. if (root->hasChildren())
  5390. {
  5391. ERRLOG("restoreWorkUnit WUID %s already exists", wuid);
  5392. return false;
  5393. }
  5394. root->setPropTree(NULL, pt.getClear());
  5395. conn.clear();
  5396. // now kludge back GraphProgress
  5397. if (gprogress)
  5398. {
  5399. VStringBuffer xpath("/GraphProgress/%s", wuid);
  5400. conn.setown(querySDS().connect(xpath, myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT));
  5401. if (conn)
  5402. {
  5403. IPropertyTree *groot = conn->queryRoot();
  5404. if (groot->hasChildren())
  5405. WARNLOG("restoreWorkUnit WUID %s graphprogress already exists, replacing",wuid);
  5406. groot->setPropTree(NULL, gprogress.getClear());
  5407. }
  5408. }
  5409. return true;
  5410. }
  5411. virtual CLocalWorkUnit* _updateWorkUnit(const char *wuid, ISecManager *secmgr, ISecUser *secuser)
  5412. {
  5413. StringBuffer wuRoot;
  5414. getXPath(wuRoot, wuid);
  5415. IRemoteConnection* conn = sdsManager->connect(wuRoot.str(), session, RTM_LOCK_WRITE|RTM_LOCK_SUB, SDS_LOCK_TIMEOUT);
  5416. if (conn)
  5417. return new CDaliWorkUnit(conn, secmgr, secuser);
  5418. else
  5419. return NULL;
  5420. }
  5421. virtual IWorkUnit* getGlobalWorkUnit(ISecManager *secmgr, ISecUser *secuser)
  5422. {
  5423. // MORE - should it check security?
  5424. StringBuffer wuRoot;
  5425. getXPath(wuRoot, GLOBAL_WORKUNIT);
  5426. IRemoteConnection* conn = sdsManager->connect(wuRoot.str(), session, RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  5427. conn->queryRoot()->setProp("@xmlns:xsi", "http://www.w3.org/1999/XMLSchema-instance");
  5428. Owned<CLocalWorkUnit> cw = new CDaliWorkUnit(conn, (ISecManager *) NULL, NULL);
  5429. return &cw->lockRemote(false);
  5430. }
  5431. virtual IConstWorkUnitIterator* getWorkUnitsByOwner(const char * owner, ISecManager *secmgr, ISecUser *secuser)
  5432. {
  5433. StringBuffer path("*");
  5434. if (owner && *owner)
  5435. path.append("[@submitID=?~\"").append(owner).append("\"]");
  5436. return _getWorkUnitsByXPath(path.str(), secmgr, secuser);
  5437. }
  5438. IConstWorkUnitIterator* getScheduledWorkUnits(ISecManager *secmgr, ISecUser *secuser)
  5439. {
  5440. StringBuffer path("*");
  5441. path.append("[@state=\"").append(getEnumText(WUStateScheduled, states)).append("\"]");
  5442. return _getWorkUnitsByXPath(path.str(), secmgr, secuser);
  5443. }
  5444. virtual void clientShutdown();
  5445. virtual unsigned numWorkUnits()
  5446. {
  5447. Owned<IRemoteConnection> conn = sdsManager->connect("/WorkUnits", session, 0, SDS_LOCK_TIMEOUT);
  5448. if (!conn)
  5449. return 0;
  5450. IPropertyTree *root = conn->queryRoot();
  5451. return root->numChildren();
  5452. }
  5453. /**
  5454. * Add a filter to an xpath query, with the appropriate filter flags
  5455. */
  5456. static void appendFilterToQueryString(StringBuffer& query, int flags, const char* name, const char* value)
  5457. {
  5458. query.append('[').append(name).append('=');
  5459. if (flags & WUSFnocase)
  5460. query.append('?');
  5461. if (flags & WUSFwild)
  5462. query.append('~');
  5463. query.append('"').append(value).append("\"]");
  5464. };
  5465. IConstWorkUnitIterator* getWorkUnitsSorted( WUSortField sortorder, // field to sort by (and flags for desc sort etc)
  5466. WUSortField *filters, // NULL or list of fields to filter on (terminated by WUSFterm)
  5467. const void *filterbuf, // (appended) string values for filters
  5468. unsigned startoffset,
  5469. unsigned maxnum,
  5470. __int64 *cachehint,
  5471. unsigned *total,
  5472. ISecManager *secmgr,
  5473. ISecUser *secuser)
  5474. {
  5475. class CQueryOrFilter : public CInterface
  5476. {
  5477. unsigned flags;
  5478. StringAttr name;
  5479. StringArray values;
  5480. public:
  5481. IMPLEMENT_IINTERFACE;
  5482. CQueryOrFilter(unsigned _flags, const char *_name, const char *_value)
  5483. : flags(_flags), name(_name)
  5484. {
  5485. values.appendListUniq(_value, "|");
  5486. };
  5487. const char* queryName() { return name.get(); };
  5488. unsigned querySearchFlags() { return flags; };
  5489. const StringArray& queryValues() const { return values; };
  5490. };
  5491. class CMultiPTreeIterator : public CInterfaceOf<IPropertyTreeIterator>
  5492. {
  5493. public:
  5494. virtual bool first() override
  5495. {
  5496. curSource = 0;
  5497. while (sources.isItem(curSource))
  5498. {
  5499. if (sources.item(curSource).first())
  5500. return true;
  5501. curSource++;
  5502. }
  5503. return false;
  5504. }
  5505. virtual bool next() override
  5506. {
  5507. if (sources.isItem(curSource))
  5508. {
  5509. if (sources.item(curSource).next())
  5510. return true;
  5511. curSource++;
  5512. while (sources.isItem(curSource))
  5513. {
  5514. if (sources.item(curSource).first())
  5515. return true;
  5516. curSource++;
  5517. }
  5518. }
  5519. return false;
  5520. }
  5521. virtual bool isValid() override
  5522. {
  5523. return sources.isItem(curSource);
  5524. }
  5525. virtual IPropertyTree & query() override
  5526. {
  5527. return sources.item(curSource).query();
  5528. }
  5529. void addSource(IPropertyTreeIterator &source)
  5530. {
  5531. sources.append(source);
  5532. }
  5533. private:
  5534. IArrayOf<IPropertyTreeIterator> sources;
  5535. unsigned curSource = 0;
  5536. };
  5537. class CWorkUnitsPager : public CSimpleInterface, implements IElementsPager
  5538. {
  5539. StringAttr xPath;
  5540. StringAttr sortOrder;
  5541. StringAttr nameFilterLo;
  5542. StringAttr nameFilterHi;
  5543. StringArray unknownAttributes;
  5544. Owned<CQueryOrFilter> orFilter;
  5545. public:
  5546. IMPLEMENT_IINTERFACE_USING(CSimpleInterface);
  5547. CWorkUnitsPager(const char* _xPath, CQueryOrFilter* _orFilter, const char *_sortOrder, const char* _nameFilterLo, const char* _nameFilterHi, StringArray& _unknownAttributes)
  5548. : xPath(_xPath), orFilter(_orFilter), sortOrder(_sortOrder), nameFilterLo(_nameFilterLo), nameFilterHi(_nameFilterHi)
  5549. {
  5550. ForEachItemIn(x, _unknownAttributes)
  5551. unknownAttributes.append(_unknownAttributes.item(x));
  5552. }
  5553. virtual IRemoteConnection* getElements(IArrayOf<IPropertyTree> &elements)
  5554. {
  5555. Owned<IRemoteConnection> conn = querySDS().connect("WorkUnits", myProcessSession(), 0, SDS_LOCK_TIMEOUT);
  5556. if (!conn)
  5557. return NULL;
  5558. Owned<IPropertyTreeIterator> iter;
  5559. if (!orFilter)
  5560. {
  5561. iter.setown(conn->getElements(xPath.get()));
  5562. }
  5563. else
  5564. {
  5565. Owned <CMultiPTreeIterator> multi = new CMultiPTreeIterator;
  5566. bool added = false;
  5567. const char* fieldName = orFilter->queryName();
  5568. unsigned flags = orFilter->querySearchFlags();
  5569. const StringArray& values = orFilter->queryValues();
  5570. ForEachItemIn(i, values)
  5571. {
  5572. StringBuffer path(xPath.get());
  5573. const char* value = values.item(i);
  5574. if (!isEmptyString(value))
  5575. {
  5576. appendFilterToQueryString(path, flags, fieldName, value);
  5577. IPropertyTreeIterator *itr = conn->getElements(path.str());
  5578. if (itr)
  5579. {
  5580. multi->addSource(*itr);
  5581. added = true;
  5582. }
  5583. }
  5584. }
  5585. if (added)
  5586. iter.setown(multi.getClear());
  5587. }
  5588. if (!iter)
  5589. return NULL;
  5590. sortElements(iter, sortOrder.get(), nameFilterLo.get(), nameFilterHi.get(), unknownAttributes, elements);
  5591. return conn.getClear();
  5592. }
  5593. virtual bool allMatchingElementsReceived() { return true; }//For now, dali always returns all of matched WUs.
  5594. };
  5595. class CScopeChecker : public CSimpleInterface, implements ISortedElementsTreeFilter
  5596. {
  5597. UniqueScopes done;
  5598. ISecManager *secmgr;
  5599. ISecUser *secuser;
  5600. CriticalSection crit;
  5601. public:
  5602. IMPLEMENT_IINTERFACE_USING(CSimpleInterface);
  5603. CScopeChecker(ISecManager *_secmgr,ISecUser *_secuser)
  5604. {
  5605. secmgr = _secmgr;
  5606. secuser = _secuser;
  5607. }
  5608. bool isOK(IPropertyTree &tree)
  5609. {
  5610. const char *scopename = tree.queryProp("@scope");
  5611. if (!scopename||!*scopename)
  5612. return true;
  5613. {
  5614. CriticalBlock block(crit);
  5615. const bool *b = done.getValue(scopename);
  5616. if (b)
  5617. return *b;
  5618. }
  5619. bool ret = checkWuScopeSecAccess(scopename,secmgr,secuser,SecAccess_Read,"iterating",false,false);
  5620. {
  5621. // conceivably could have already been checked and added, but ok.
  5622. CriticalBlock block(crit);
  5623. done.setValue(scopename,ret);
  5624. }
  5625. return ret;
  5626. }
  5627. };
  5628. Owned<CQueryOrFilter> orFilter;
  5629. Owned<ISortedElementsTreeFilter> sc = new CScopeChecker(secmgr,secuser);
  5630. StringBuffer query;
  5631. StringBuffer so;
  5632. StringAttr namefilter("*");
  5633. StringAttr namefilterlo;
  5634. StringAttr namefilterhi;
  5635. StringArray unknownAttributes;
  5636. if (filters)
  5637. {
  5638. const char *fv = (const char *) filterbuf;
  5639. for (unsigned i=0;filters[i]!=WUSFterm;i++)
  5640. {
  5641. assertex(fv);
  5642. int fmt = filters[i];
  5643. int subfmt = (fmt&0xff);
  5644. if (subfmt==WUSFwuid)
  5645. namefilterlo.set(fv);
  5646. else if (subfmt==WUSFwuidhigh)
  5647. namefilterhi.set(fv);
  5648. else if (subfmt==WUSFwildwuid)
  5649. namefilter.set(fv);
  5650. else if (subfmt==WUSFappvalue)
  5651. {
  5652. const char *app = fv;
  5653. fv = fv + strlen(fv)+1;
  5654. query.append("[Application/").append(app);
  5655. if (*fv)
  5656. query.append("=?~\"").append(fv).append('\"');
  5657. query.append("]");
  5658. }
  5659. else if (subfmt==WUSFtotalthortime)
  5660. {
  5661. query.append("[@totalThorTime>=\"");
  5662. formatTimeCollatable(query, milliToNano(atoi(fv)), false);
  5663. query.append("\"]");
  5664. }
  5665. else if (!*fv)
  5666. {
  5667. unknownAttributes.append(getEnumText(subfmt,workunitSortFields));
  5668. if (subfmt==WUSFtotalthortime)
  5669. sortorder = (WUSortField) (sortorder & ~WUSFnumeric);
  5670. }
  5671. else
  5672. {
  5673. const char* fieldName = getEnumText(subfmt,workunitSortFields);
  5674. if (!strchr(fv, '|'))
  5675. appendFilterToQueryString(query, fmt, fieldName, fv);
  5676. else if (orFilter)
  5677. throw MakeStringException(WUERR_InvalidUserInput, "Multiple OR filters not allowed");
  5678. else
  5679. {
  5680. if (!strieq(fieldName, getEnumText(WUSFstate,workunitSortFields)) &&
  5681. !strieq(fieldName, getEnumText(WUSFuser,workunitSortFields)) &&
  5682. !strieq(fieldName, getEnumText(WUSFcluster,workunitSortFields)))
  5683. throw MakeStringException(WUERR_InvalidUserInput, "OR filters not allowed for %s", fieldName);
  5684. orFilter.setown(new CQueryOrFilter(fmt, fieldName, fv));
  5685. }
  5686. }
  5687. fv = fv + strlen(fv)+1;
  5688. }
  5689. }
  5690. if ((sortorder&0xff)==WUSFtotalthortime)
  5691. sortorder = (WUSortField) (sortorder & ~WUSFnumeric);
  5692. query.insert(0, namefilter.get());
  5693. if (sortorder)
  5694. {
  5695. if (so.length())
  5696. so.append(',');
  5697. if (sortorder & WUSFreverse)
  5698. so.append('-');
  5699. if (sortorder & WUSFnocase)
  5700. so.append('?');
  5701. if (sortorder & WUSFnumeric)
  5702. so.append('#');
  5703. so.append(getEnumText(sortorder&0xff,workunitSortFields));
  5704. }
  5705. IArrayOf<IPropertyTree> results;
  5706. Owned<IElementsPager> elementsPager = new CWorkUnitsPager(query.str(), orFilter.getClear(), so.length()?so.str():NULL, namefilterlo.get(), namefilterhi.get(), unknownAttributes);
  5707. Owned<IRemoteConnection> conn=getElementsPaged(elementsPager,startoffset,maxnum,secmgr?sc:NULL,"",cachehint,results,total,NULL);
  5708. return new CConstWUArrayIterator(results);
  5709. }
  5710. virtual WUState waitForWorkUnit(const char * wuid, unsigned timeout, bool compiled, std::list<WUState> expectedStates)
  5711. {
  5712. WUState ret = WUStateUnknown;
  5713. StringBuffer wuRoot;
  5714. getXPath(wuRoot, wuid);
  5715. Owned<IRemoteConnection> conn = sdsManager->connect(wuRoot.str(), session, 0, SDS_LOCK_TIMEOUT);
  5716. if (timeout == 0) //no need to subscribe
  5717. {
  5718. ret = (WUState) getEnum(conn->queryRoot(), "@state", states);
  5719. auto it = std::find(expectedStates.begin(), expectedStates.end(), ret);
  5720. if (it != expectedStates.end())
  5721. return ret;
  5722. switch (ret)
  5723. {
  5724. case WUStateCompiled:
  5725. case WUStateUploadingFiles:
  5726. if (!compiled)
  5727. break;
  5728. //fall through
  5729. case WUStateCompleted:
  5730. case WUStateFailed:
  5731. case WUStateAborted:
  5732. return ret;
  5733. default:
  5734. break;
  5735. }
  5736. return WUStateUnknown;
  5737. }
  5738. Owned<WorkUnitWaiter> waiter = new WorkUnitWaiter(wuid, SubscribeOptionState);
  5739. LocalIAbortHandler abortHandler(*waiter);
  5740. if (conn)
  5741. {
  5742. SessionId agent = -1;
  5743. bool agentSessionStopped = false;
  5744. unsigned start = msTick();
  5745. for (;;)
  5746. {
  5747. ret = (WUState) getEnum(conn->queryRoot(), "@state", states);
  5748. auto it = std::find(expectedStates.begin(), expectedStates.end(), ret);
  5749. if (it != expectedStates.end())
  5750. return ret;
  5751. switch (ret)
  5752. {
  5753. case WUStateCompiled:
  5754. case WUStateUploadingFiles:
  5755. if (!compiled)
  5756. break;
  5757. // fall into
  5758. case WUStateCompleted:
  5759. case WUStateFailed:
  5760. case WUStateAborted:
  5761. return ret;
  5762. case WUStateWait:
  5763. break;
  5764. case WUStateCompiling:
  5765. case WUStateRunning:
  5766. case WUStateDebugPaused:
  5767. case WUStateDebugRunning:
  5768. case WUStateBlocked:
  5769. case WUStateAborting:
  5770. if (agentSessionStopped)
  5771. {
  5772. reportAbnormalTermination(wuid, ret, agent);
  5773. return ret;
  5774. }
  5775. if (queryDaliServerVersion().compare("2.1")>=0)
  5776. {
  5777. agent = conn->queryRoot()->getPropInt64("@agentSession", -1);
  5778. if((agent>0) && querySessionManager().sessionStopped(agent, 0))
  5779. {
  5780. agentSessionStopped = true;
  5781. conn->reload();
  5782. continue;
  5783. }
  5784. }
  5785. break;
  5786. }
  5787. agentSessionStopped = false; // reset for state changes such as WUStateWait then WUStateRunning again
  5788. unsigned waited = msTick() - start;
  5789. if (timeout==-1 || waited + 20000 < timeout)
  5790. {
  5791. waiter->wait(20000); // recheck state every 20 seconds, in case eclagent has crashed.
  5792. if (waiter->isAborted())
  5793. {
  5794. ret = WUStateUnknown; // MORE - throw an exception?
  5795. break;
  5796. }
  5797. }
  5798. else if (waited > timeout || !waiter->wait(timeout-waited))
  5799. {
  5800. ret = WUStateUnknown; // MORE - throw an exception?
  5801. break;
  5802. }
  5803. conn->reload();
  5804. }
  5805. }
  5806. return ret;
  5807. }
  5808. virtual WUAction waitForWorkUnitAction(const char * wuid, WUAction original)
  5809. {
  5810. Owned<WorkUnitWaiter> waiter = new WorkUnitWaiter(wuid, SubscribeOptionAction);
  5811. LocalIAbortHandler abortHandler(*waiter);
  5812. WUAction ret = WUActionUnknown;
  5813. StringBuffer wuRoot;
  5814. getXPath(wuRoot, wuid);
  5815. Owned<IRemoteConnection> conn = sdsManager->connect(wuRoot.str(), session, 0, SDS_LOCK_TIMEOUT);
  5816. if (conn)
  5817. {
  5818. unsigned start = msTick();
  5819. for (;;)
  5820. {
  5821. ret = (WUAction) getEnum(conn->queryRoot(), "Action", actions);
  5822. if (ret != original)
  5823. break;
  5824. unsigned waited = msTick() - start;
  5825. waiter->wait(20000); // recheck state every 20 seconds even if no timeout, in case eclagent has crashed.
  5826. if (waiter->isAborted())
  5827. {
  5828. ret = WUActionUnknown; // MORE - throw an exception?
  5829. break;
  5830. }
  5831. conn->reload();
  5832. }
  5833. }
  5834. waiter->unsubscribe();
  5835. return ret;
  5836. }
  5837. protected:
  5838. IConstWorkUnitIterator * _getWorkUnitsByXPath(const char *xpath, ISecManager *secmgr, ISecUser *secuser)
  5839. {
  5840. Owned<IRemoteConnection> conn = sdsManager->connect("/WorkUnits", session, 0, SDS_LOCK_TIMEOUT);
  5841. if (conn)
  5842. {
  5843. CDaliVersion serverVersionNeeded("3.2");
  5844. Owned<IPropertyTreeIterator> iter(queryDaliServerVersion().compare(serverVersionNeeded) < 0 ?
  5845. conn->queryRoot()->getElements(xpath) :
  5846. conn->getElements(xpath));
  5847. return createSecureConstWUIterator(iter.getClear(), secmgr, secuser);
  5848. }
  5849. else
  5850. return NULL;
  5851. }
  5852. ISDSManager *sdsManager;
  5853. SessionId session;
  5854. };
  5855. extern WORKUNIT_API IConstWorkUnitIterator *createSecureConstWUIterator(IConstWorkUnitIterator *iter, ISecManager *secmgr, ISecUser *secuser)
  5856. {
  5857. if (secmgr)
  5858. return new CSecureConstWUIterator(iter, secmgr, secuser);
  5859. else
  5860. return iter;
  5861. }
  5862. extern WORKUNIT_API IConstWorkUnitIterator *createSecureConstWUIterator(IPropertyTreeIterator *iter, ISecManager *secmgr, ISecUser *secuser)
  5863. {
  5864. if (secmgr)
  5865. return new CSecureConstWUIterator(new CConstWUIterator(iter), secmgr, secuser);
  5866. else
  5867. return new CConstWUIterator(iter);
  5868. }
  5869. static CriticalSection factoryCrit;
  5870. static Owned<IWorkUnitFactory> globalFactory;
  5871. void CDaliWorkUnitFactory::clientShutdown()
  5872. {
  5873. CriticalBlock b(factoryCrit);
  5874. globalFactory.clear();
  5875. }
  5876. void clientShutdownWorkUnit()
  5877. {
  5878. CriticalBlock b(factoryCrit);
  5879. globalFactory.clear();
  5880. }
  5881. extern WORKUNIT_API void setWorkUnitFactory(IWorkUnitFactory * _factory)
  5882. {
  5883. CriticalBlock b(factoryCrit);
  5884. globalFactory.setown(_factory);
  5885. }
  5886. extern WORKUNIT_API IWorkUnitFactory * getWorkUnitFactory()
  5887. {
  5888. if (!globalFactory)
  5889. {
  5890. CriticalBlock b(factoryCrit);
  5891. if (!globalFactory) // NOTE - this "double test" paradigm is not guaranteed threadsafe on modern systems/compilers - I think in this instance that is harmless even in the (extremely) unlikely event that it resulted in the setown being called twice.
  5892. {
  5893. const char *forceEnv = getenv("FORCE_DALI_WORKUNITS");
  5894. bool forceDali = forceEnv && !strieq(forceEnv, "off") && !strieq(forceEnv, "0");
  5895. Owned<IRemoteConnection> env = querySDS().connect("/Environment", myProcessSession(), 0, SDS_LOCK_TIMEOUT);
  5896. IPropertyTree *pluginInfo = NULL;
  5897. if (env)
  5898. {
  5899. SocketEndpoint targetDali = queryCoven().queryGroup().queryNode(0).endpoint();
  5900. IPropertyTree *daliInfo = findDaliProcess(env->queryRoot(), targetDali);
  5901. if (daliInfo)
  5902. {
  5903. const char *daliName = daliInfo->queryProp("@name");
  5904. if (daliName)
  5905. {
  5906. VStringBuffer xpath("Software/DaliServerPlugin[@type='WorkunitServer'][@daliServers='%s']", daliName);
  5907. pluginInfo = env->queryRoot()->queryPropTree(xpath);
  5908. }
  5909. if (!pluginInfo)
  5910. pluginInfo = daliInfo->queryPropTree("Plugin[@type='WorkunitServer']"); // Compatibility with early betas of 6.0 ...
  5911. }
  5912. }
  5913. if (pluginInfo && !forceDali)
  5914. globalFactory.setown( (IWorkUnitFactory *) loadPlugin(pluginInfo));
  5915. else
  5916. globalFactory.setown(new CDaliWorkUnitFactory());
  5917. }
  5918. }
  5919. return globalFactory.getLink();
  5920. }
  5921. extern WORKUNIT_API IWorkUnitFactory * getDaliWorkUnitFactory()
  5922. {
  5923. if (!globalFactory)
  5924. {
  5925. CriticalBlock b(factoryCrit);
  5926. if (!globalFactory) // NOTE - this "double test" paradigm is not guaranteed threadsafe on modern systems/compilers - I think in this instance that is harmless even in the (extremely) unlikely event that it resulted in the setown being called twice.
  5927. globalFactory.setown(new CDaliWorkUnitFactory());
  5928. }
  5929. return globalFactory.getLink();
  5930. }
  5931. // A SecureWorkUnitFactory allows the security params to be supplied once to the factory rather than being supplied to each call.
  5932. // They can still be supplied if you want...
  5933. class CSecureWorkUnitFactory : implements IWorkUnitFactory, public CInterface
  5934. {
  5935. public:
  5936. IMPLEMENT_IINTERFACE;
  5937. CSecureWorkUnitFactory(IWorkUnitFactory *_baseFactory, ISecManager *_secMgr, ISecUser *_secUser)
  5938. : baseFactory(_baseFactory), defaultSecMgr(_secMgr), defaultSecUser(_secUser)
  5939. {
  5940. }
  5941. virtual bool initializeStore()
  5942. {
  5943. throwUnexpected(); // Used when loading a plugin factory - not applicable here
  5944. }
  5945. virtual IWorkUnitWatcher *getWatcher(IWorkUnitSubscriber *subscriber, WUSubscribeOptions options, const char *wuid) const
  5946. {
  5947. return baseFactory->getWatcher(subscriber, options, wuid);
  5948. }
  5949. virtual unsigned validateRepository(bool fix)
  5950. {
  5951. return baseFactory->validateRepository(fix);
  5952. }
  5953. virtual void deleteRepository(bool recreate)
  5954. {
  5955. return baseFactory->deleteRepository(recreate);
  5956. }
  5957. virtual void createRepository()
  5958. {
  5959. return baseFactory->createRepository();
  5960. }
  5961. virtual const char *queryStoreType() const
  5962. {
  5963. return baseFactory->queryStoreType();
  5964. }
  5965. virtual StringArray &getUniqueValues(WUSortField field, const char *prefix, StringArray &result) const
  5966. {
  5967. return baseFactory->getUniqueValues(field, prefix, result);
  5968. }
  5969. virtual IWorkUnit* createNamedWorkUnit(const char *wuid, const char *app, const char *user, ISecManager *secMgr, ISecUser *secUser)
  5970. {
  5971. if (!secMgr) secMgr = defaultSecMgr.get();
  5972. if (!secUser) secUser = defaultSecUser.get();
  5973. return baseFactory->createNamedWorkUnit(wuid, app, user, secMgr, secUser);
  5974. }
  5975. virtual IWorkUnit* createWorkUnit(const char *app, const char *user, ISecManager *secMgr, ISecUser *secUser)
  5976. {
  5977. if (!secMgr) secMgr = defaultSecMgr.get();
  5978. if (!secUser) secUser = defaultSecUser.get();
  5979. return baseFactory->createWorkUnit(app, user, secMgr, secUser);
  5980. }
  5981. virtual bool deleteWorkUnit(const char * wuid, ISecManager *secMgr, ISecUser *secUser)
  5982. {
  5983. if (!secMgr) secMgr = defaultSecMgr.get();
  5984. if (!secUser) secUser = defaultSecUser.get();
  5985. return baseFactory->deleteWorkUnit(wuid, secMgr, secUser);
  5986. }
  5987. virtual bool deleteWorkUnitEx(const char * wuid, bool throwException, ISecManager *secMgr, ISecUser *secUser)
  5988. {
  5989. if (!secMgr) secMgr = defaultSecMgr.get();
  5990. if (!secUser) secUser = defaultSecUser.get();
  5991. return baseFactory->deleteWorkUnitEx(wuid, throwException, secMgr, secUser);
  5992. }
  5993. virtual IConstWorkUnit* openWorkUnit(const char *wuid, ISecManager *secMgr, ISecUser *secUser)
  5994. {
  5995. if (!secMgr) secMgr = defaultSecMgr.get();
  5996. if (!secUser) secUser = defaultSecUser.get();
  5997. return baseFactory->openWorkUnit(wuid, secMgr, secUser);
  5998. }
  5999. virtual IWorkUnit* updateWorkUnit(const char *wuid, ISecManager *secMgr, ISecUser *secUser)
  6000. {
  6001. if (!secMgr) secMgr = defaultSecMgr.get();
  6002. if (!secUser) secUser = defaultSecUser.get();
  6003. return baseFactory->updateWorkUnit(wuid, secMgr, secUser);
  6004. }
  6005. virtual bool restoreWorkUnit(const char *base, const char *wuid, bool restoreAssociated)
  6006. {
  6007. return baseFactory->restoreWorkUnit(base, wuid, restoreAssociated);
  6008. }
  6009. virtual void importWorkUnit(const char *zapReportFileName, const char *zapReportPassword,
  6010. const char *importDir, const char *app, const char *user, ISecManager *secMgr, ISecUser *secUser)
  6011. {
  6012. baseFactory->importWorkUnit(zapReportFileName, zapReportPassword, importDir, app, user, secMgr, secUser);
  6013. }
  6014. virtual IWorkUnit * getGlobalWorkUnit(ISecManager *secMgr, ISecUser *secUser)
  6015. {
  6016. if (!secMgr) secMgr = defaultSecMgr.get();
  6017. if (!secUser) secUser = defaultSecUser.get();
  6018. return baseFactory->getGlobalWorkUnit(secMgr, secUser);
  6019. }
  6020. virtual IConstWorkUnitIterator * getWorkUnitsByOwner(const char * owner, ISecManager *secMgr, ISecUser *secUser)
  6021. {
  6022. if (!secMgr) secMgr = defaultSecMgr.get();
  6023. if (!secUser) secUser = defaultSecUser.get();
  6024. return baseFactory->getWorkUnitsByOwner(owner, secMgr, secUser);
  6025. }
  6026. virtual IConstWorkUnitIterator * getScheduledWorkUnits(ISecManager *secMgr, ISecUser *secUser)
  6027. {
  6028. if (!secMgr) secMgr = defaultSecMgr.get();
  6029. if (!secUser) secUser = defaultSecUser.get();
  6030. return baseFactory->getScheduledWorkUnits(secMgr, secUser);
  6031. }
  6032. virtual void descheduleAllWorkUnits(ISecManager *secMgr, ISecUser *secUser)
  6033. {
  6034. if (!secMgr) secMgr = defaultSecMgr.get();
  6035. if (!secUser) secUser = defaultSecUser.get();
  6036. baseFactory->descheduleAllWorkUnits(secMgr, secUser);
  6037. }
  6038. virtual int setTracingLevel(int newLevel)
  6039. {
  6040. return baseFactory->setTracingLevel(newLevel);
  6041. }
  6042. virtual IConstWorkUnitIterator* getWorkUnitsSorted( WUSortField sortorder, // field to sort by
  6043. WUSortField *filters, // NULL or list of fields to filter on (terminated by WUSFterm)
  6044. const void *filterbuf, // (appended) string values for filters
  6045. unsigned startoffset,
  6046. unsigned maxnum,
  6047. __int64 *cachehint,
  6048. unsigned *total,
  6049. ISecManager *secMgr, ISecUser *secUser)
  6050. {
  6051. if (!secMgr) secMgr = defaultSecMgr.get();
  6052. if (!secUser) secUser = defaultSecUser.get();
  6053. return baseFactory->getWorkUnitsSorted(sortorder,filters,filterbuf,startoffset,maxnum,cachehint, total, secMgr, secUser);
  6054. }
  6055. virtual IConstQuerySetQueryIterator* getQuerySetQueriesSorted( WUQuerySortField *sortorder,
  6056. WUQuerySortField *filters,
  6057. const void *filterbuf,
  6058. unsigned startoffset,
  6059. unsigned maxnum,
  6060. __int64 *cachehint,
  6061. unsigned *total,
  6062. const MapStringTo<bool> *subset,
  6063. const MapStringTo<bool> *suspendedByCluster)
  6064. {
  6065. // MORE - why no security?
  6066. return baseFactory->getQuerySetQueriesSorted(sortorder,filters,filterbuf,startoffset,maxnum,cachehint,total,subset,suspendedByCluster);
  6067. }
  6068. virtual unsigned numWorkUnits()
  6069. {
  6070. return baseFactory->numWorkUnits();
  6071. }
  6072. virtual bool isAborting(const char *wuid) const
  6073. {
  6074. return baseFactory->isAborting(wuid);
  6075. }
  6076. virtual void clearAborting(const char *wuid)
  6077. {
  6078. baseFactory->clearAborting(wuid);
  6079. }
  6080. virtual WUState waitForWorkUnit(const char * wuid, unsigned timeout, bool compiled, std::list<WUState> expectedStates)
  6081. {
  6082. return baseFactory->waitForWorkUnit(wuid, timeout, compiled, expectedStates);
  6083. }
  6084. virtual WUAction waitForWorkUnitAction(const char * wuid, WUAction original)
  6085. {
  6086. return baseFactory->waitForWorkUnitAction(wuid, original);
  6087. }
  6088. private:
  6089. Owned<IWorkUnitFactory> baseFactory;
  6090. Linked<ISecManager> defaultSecMgr;
  6091. Linked<ISecUser> defaultSecUser;
  6092. };
  6093. extern WORKUNIT_API IWorkUnitFactory * getWorkUnitFactory(ISecManager *secmgr, ISecUser *secuser)
  6094. {
  6095. if (secmgr && secuser)
  6096. return new CSecureWorkUnitFactory(getWorkUnitFactory(), secmgr, secuser);
  6097. else
  6098. return getWorkUnitFactory();
  6099. }
  6100. //==========================================================================================
  6101. class CStringPTreeIterator : implements IStringIterator, public CInterface
  6102. {
  6103. Owned<IPropertyTreeIterator> it;
  6104. public:
  6105. IMPLEMENT_IINTERFACE;
  6106. CStringPTreeIterator(IPropertyTreeIterator *p) : it(p) {};
  6107. virtual bool first() { return it->first(); }
  6108. virtual bool next() { return it->next(); }
  6109. virtual bool isValid() { return it->isValid(); }
  6110. virtual IStringVal & str(IStringVal &s) { s.set(it->query().queryProp(NULL)); return s; }
  6111. };
  6112. class CStringPTreeTagIterator : implements IStringIterator, public CInterface
  6113. {
  6114. Owned<IPropertyTreeIterator> it;
  6115. public:
  6116. IMPLEMENT_IINTERFACE;
  6117. CStringPTreeTagIterator(IPropertyTreeIterator *p) : it(p) {};
  6118. virtual bool first() { return it->first(); }
  6119. virtual bool next() { return it->next(); }
  6120. virtual bool isValid() { return it->isValid(); }
  6121. virtual IStringVal & str(IStringVal &s) { s.set(it->query().queryName()); return s; }
  6122. };
  6123. class CStringPTreeAttrIterator : implements IStringIterator, public CInterface
  6124. {
  6125. Owned<IPropertyTreeIterator> it;
  6126. StringAttr name;
  6127. public:
  6128. IMPLEMENT_IINTERFACE;
  6129. CStringPTreeAttrIterator(IPropertyTreeIterator *p, const char *_name) : it(p), name(_name) {};
  6130. virtual bool first() { return it->first(); }
  6131. virtual bool next() { return it->next(); }
  6132. virtual bool isValid() { return it->isValid(); }
  6133. virtual IStringVal & str(IStringVal &s) { s.set(it->query().queryProp(name)); return s; }
  6134. };
  6135. //==========================================================================================
  6136. CLocalWorkUnit::CLocalWorkUnit(ISecManager *secmgr, ISecUser *secuser)
  6137. {
  6138. clearCached(false);
  6139. secMgr.set(secmgr);
  6140. secUser.set(secuser);
  6141. workflowIteratorCached = false;
  6142. resultsCached = false;
  6143. graphsCached = false;
  6144. temporariesCached = false;
  6145. variablesCached = false;
  6146. exceptionsCached = false;
  6147. pluginsCached = false;
  6148. librariesCached = false;
  6149. activitiesCached = false;
  6150. webServicesInfoCached = false;
  6151. roxieQueryInfoCached = false;
  6152. }
  6153. void CLocalWorkUnit::clearCached(bool clearTree)
  6154. {
  6155. query.clear();
  6156. webServicesInfo.clear();
  6157. workflowIterator.clear();
  6158. graphs.kill();
  6159. results.kill();
  6160. variables.kill();
  6161. plugins.kill();
  6162. libraries.kill();
  6163. exceptions.kill();
  6164. temporaries.kill();
  6165. statistics.kill();
  6166. appvalues.kill();
  6167. if (clearTree)
  6168. p.clear();
  6169. workflowIteratorCached = false;
  6170. resultsCached = false;
  6171. graphsCached = false;
  6172. temporariesCached = false;
  6173. variablesCached = false;
  6174. exceptionsCached = false;
  6175. pluginsCached = false;
  6176. librariesCached = false;
  6177. activitiesCached = false;
  6178. webServicesInfoCached = false;
  6179. roxieQueryInfoCached = false;
  6180. }
  6181. void CLocalWorkUnit::loadPTree(IPropertyTree *ptree)
  6182. {
  6183. clearCached(false);
  6184. p.setown(ptree);
  6185. }
  6186. void CLocalWorkUnit::beforeDispose()
  6187. {
  6188. try
  6189. {
  6190. unsubscribe();
  6191. clearCached(true);
  6192. userDesc.clear();
  6193. secMgr.clear();
  6194. secUser.clear();
  6195. }
  6196. catch (IException *E) { LOG(MCexception(E, MSGCLS_warning), E, "Exception during ~CLocalWorkUnit"); E->Release(); }
  6197. }
  6198. void CLocalWorkUnit::cleanupAndDelete(bool deldll, bool deleteOwned, const StringArray *deleteExclusions)
  6199. {
  6200. MTIME_SECTION(queryActiveTimer(), "WUDELETE cleanupAndDelete total");
  6201. // Delete any related things in SDS etc that might otherwise be forgotten
  6202. if (p->getPropBool("@protected", false))
  6203. throw MakeStringException(WUERR_WorkunitProtected, "%s: Workunit is protected",p->queryName());
  6204. switch (getState())
  6205. {
  6206. case WUStateAborted:
  6207. case WUStateCompleted:
  6208. case WUStateFailed:
  6209. case WUStateArchived:
  6210. break;
  6211. case WUStateCompiled:
  6212. if (getAction()==WUActionRun || getAction()==WUActionUnknown)
  6213. throw MakeStringException(WUERR_WorkunitActive, "%s: Workunit is active. Please abort before deleting this workunit.",p->queryName());
  6214. break;
  6215. case WUStateWait:
  6216. throw MakeStringException(WUERR_WorkunitScheduled, "%s: Workunit is scheduled",p->queryName());
  6217. default:
  6218. throw MakeStringException(WUERR_WorkunitActive, "%s: Workunit is active. Please abort before deleting this workunit.",p->queryName());
  6219. break;
  6220. }
  6221. if (getIsQueryService())
  6222. {
  6223. Owned<IPropertyTree> registry = getQueryRegistryRoot();
  6224. if (registry)
  6225. {
  6226. VStringBuffer xpath("QuerySet/Query[@wuid='%s']", p->queryName());
  6227. if (registry->hasProp(xpath.str()))
  6228. throw MakeStringException(WUERR_WorkunitPublished, "%s: Workunit is published",p->queryName());
  6229. }
  6230. }
  6231. try
  6232. {
  6233. if (deldll && !p->getPropBool("@isClone", false))
  6234. {
  6235. Owned<IConstWUQuery> q = getQuery();
  6236. if (q)
  6237. {
  6238. Owned<IConstWUAssociatedFileIterator> iter = &q->getAssociatedFiles();
  6239. SCMStringBuffer name;
  6240. ForEach(*iter)
  6241. {
  6242. IConstWUAssociatedFile & cur = iter->query();
  6243. cur.getNameTail(name);
  6244. if (!deleteExclusions || (NotFound == deleteExclusions->find(name.str())))
  6245. {
  6246. Owned<IDllEntry> entry = queryDllServer().getEntry(name.str());
  6247. if (entry.get())
  6248. asyncRemoveDll(name.str());
  6249. else
  6250. {
  6251. SCMStringBuffer ip, localPath;
  6252. cur.getName(localPath);
  6253. cur.getIp(ip);
  6254. asyncRemoveFile(ip.str(), localPath.str());
  6255. }
  6256. }
  6257. }
  6258. }
  6259. }
  6260. globalFactory->clearAborting(queryWuid());
  6261. deleteTempFiles(NULL, deleteOwned, true); // all, any remaining.
  6262. }
  6263. catch(IException *E)
  6264. {
  6265. StringBuffer s;
  6266. LOG(MCexception(E, MSGCLS_warning), E, s.append("Exception during cleanupAndDelete: ").append(p->queryName()).str());
  6267. E->Release();
  6268. }
  6269. catch (...)
  6270. {
  6271. WARNLOG("Unknown exception during cleanupAndDelete: %s", p->queryName());
  6272. }
  6273. }
  6274. void CLocalWorkUnit::setTimeScheduled(const IJlibDateTime &val)
  6275. {
  6276. SCMStringBuffer strval;
  6277. val.getGmtString(strval);
  6278. p->setProp("@timescheduled",strval.str());
  6279. }
  6280. IJlibDateTime & CLocalWorkUnit::getTimeScheduled(IJlibDateTime &val) const
  6281. {
  6282. StringBuffer str;
  6283. p->getProp("@timescheduled",str);
  6284. if(str.length())
  6285. val.setGmtString(str.str());
  6286. return val;
  6287. }
  6288. bool modifyAndWriteWorkUnitXML(char const * wuid, StringBuffer & buf, StringBuffer & extra, IFileIO * fileio)
  6289. {
  6290. // kludge in extra chunks of XML such as GraphProgress and GeneratedDlls
  6291. if (extra.length())
  6292. {
  6293. const char *bufStart = buf.str();
  6294. const char *bufPtr = bufStart+buf.length();
  6295. while (bufStart != bufPtr)
  6296. {
  6297. --bufPtr;
  6298. if (!isspace(*bufPtr))
  6299. break;
  6300. }
  6301. assertex('>' == *bufPtr);
  6302. size_t l = strlen(wuid);
  6303. assertex((size_t)(bufPtr-bufStart) > l+2); // e.g. at least </W20171111-111111>
  6304. bufPtr -= l+2; // skip back over </wuid
  6305. assertex(0 == memcmp(bufPtr, "</", 2) );
  6306. assertex(0 == memcmp(bufPtr+2, wuid, l));
  6307. buf.insert(bufPtr-bufStart, extra);
  6308. }
  6309. return (fileio->write(0,buf.length(),buf.str()) == buf.length());
  6310. }
  6311. bool CLocalWorkUnit::archiveWorkUnit(const char *base, bool del, bool ignoredllerrors, bool deleteOwned, bool exportAssociatedFiles)
  6312. {
  6313. CriticalBlock block(crit);
  6314. StringBuffer path(base);
  6315. if (!p)
  6316. return false;
  6317. const char *wuid = p->queryName();
  6318. if (!wuid||!*wuid)
  6319. return false;
  6320. addPathSepChar(path).append(wuid).append(".xml");
  6321. Owned<IFile> file = createIFile(path.str());
  6322. if (!file)
  6323. return false;
  6324. Owned<IFileIO> fileio = file->open(IFOcreate);
  6325. if (!fileio)
  6326. return false;
  6327. StringBuffer buf;
  6328. exportWorkUnitToXML(this, buf, false, false, true);
  6329. StringBuffer extraWorkUnitXML;
  6330. Owned<IPTree> graphProgress = getGraphProgressTree();
  6331. if (graphProgress)
  6332. {
  6333. toXML(graphProgress,extraWorkUnitXML,1,XML_Format);
  6334. graphProgress.clear();
  6335. }
  6336. Owned<IConstWUQuery> q = getQuery();
  6337. if (!q)
  6338. {
  6339. if (!modifyAndWriteWorkUnitXML(wuid, buf, extraWorkUnitXML, fileio))
  6340. return false;
  6341. if (del)
  6342. {
  6343. if (getState()==WUStateUnknown)
  6344. setState(WUStateArchived); // to allow delete
  6345. cleanupAndDelete(false,deleteOwned); // no query, may as well delete
  6346. }
  6347. return false;
  6348. }
  6349. StringArray deleteExclusions; // associated files not to delete, added if failure to copy
  6350. Owned<IConstWUAssociatedFileIterator> iter = &q->getAssociatedFiles();
  6351. Owned<IPropertyTree> generatedDlls = createPTree("GeneratedDlls");
  6352. ForEach(*iter)
  6353. {
  6354. IConstWUAssociatedFile & cur = iter->query();
  6355. SCMStringBuffer name;
  6356. cur.getNameTail(name);
  6357. if (name.length())
  6358. {
  6359. Owned<IDllEntry> entry = queryDllServer().getEntry(name.str());
  6360. SCMStringBuffer curPath, curIp;
  6361. cur.getName(curPath);
  6362. cur.getIp(curIp);
  6363. SocketEndpoint curEp(curIp.str());
  6364. RemoteFilename curRfn;
  6365. curRfn.setPath(curEp, curPath.str());
  6366. StringBuffer dst(base);
  6367. addPathSepChar(dst);
  6368. curRfn.getTail(dst);
  6369. Owned<IFile> dstFile = createIFile(dst.str());
  6370. if (entry.get())
  6371. {
  6372. Owned<IException> exception;
  6373. Owned<IDllLocation> loc;
  6374. Owned<IPropertyTree> generatedDllBranch = createPTree();
  6375. generatedDllBranch->setProp("@name", entry->queryName());
  6376. generatedDllBranch->setProp("@kind", entry->queryKind());
  6377. if (exportAssociatedFiles)
  6378. {
  6379. try
  6380. {
  6381. loc.setown(entry->getBestLocation()); //throws exception if no readable locations
  6382. }
  6383. catch(IException * e)
  6384. {
  6385. exception.setown(e);
  6386. loc.setown(entry->getBestLocationCandidate()); //this will be closest of the unreadable locations
  6387. }
  6388. RemoteFilename filename;
  6389. loc->getDllFilename(filename);
  6390. if (!exception)
  6391. {
  6392. Owned<IFile> srcfile = createIFile(filename);
  6393. try
  6394. {
  6395. if (dstFile->exists())
  6396. {
  6397. if (streq(srcfile->queryFilename(), dstFile->queryFilename()))
  6398. deleteExclusions.append(name.str()); // restored workunit, referencing archive location for query dll (no longer true post HPCC-11191 fix)
  6399. // still want to delete if already archived but there are source file copies
  6400. }
  6401. else
  6402. copyFile(dstFile, srcfile);
  6403. }
  6404. catch(IException * e)
  6405. {
  6406. exception.setown(e);
  6407. }
  6408. }
  6409. if (exception)
  6410. {
  6411. if (ignoredllerrors)
  6412. {
  6413. EXCLOG(exception.get(), "archiveWorkUnit (copying associated file)");
  6414. //copy failed, so don't delete the registered dll files
  6415. deleteExclusions.append(name.str());
  6416. }
  6417. else
  6418. throw exception.getClear();
  6419. }
  6420. }
  6421. // Record Associated path to restore back to
  6422. StringBuffer restorePath;
  6423. curRfn.getRemotePath(restorePath);
  6424. generatedDllBranch->setProp("@location", restorePath.str());
  6425. generatedDlls->addPropTree("GeneratedDll", generatedDllBranch.getClear());
  6426. }
  6427. else if (exportAssociatedFiles) // no generated dll entry
  6428. {
  6429. Owned<IFile> srcFile = createIFile(curRfn);
  6430. try
  6431. {
  6432. copyFile(dstFile, srcFile);
  6433. }
  6434. catch (IException *e)
  6435. {
  6436. VStringBuffer msg("Failed to archive associated file '%s' to destination '%s'", srcFile->queryFilename(), dstFile->queryFilename());
  6437. EXCLOG(e, msg.str());
  6438. e->Release();
  6439. deleteExclusions.append(name.str());
  6440. }
  6441. }
  6442. }
  6443. }
  6444. iter.clear();
  6445. if (generatedDlls->numChildren())
  6446. toXML(generatedDlls, extraWorkUnitXML, 1, XML_Format);
  6447. if (!modifyAndWriteWorkUnitXML(wuid, buf, extraWorkUnitXML, fileio))
  6448. return false;
  6449. if (del)
  6450. {
  6451. //setState(WUStateArchived); // this isn't useful as about to delete it!
  6452. q.clear();
  6453. cleanupAndDelete(true, deleteOwned, &deleteExclusions);
  6454. }
  6455. return true;
  6456. }
  6457. void CLocalWorkUnit::loadXML(const char *xml)
  6458. {
  6459. CriticalBlock block(crit);
  6460. clearCached(true);
  6461. assertex(xml);
  6462. p.setown(createPTreeFromXMLString(xml,ipt_lowmem));
  6463. }
  6464. void CLocalWorkUnit::serialize(MemoryBuffer &tgt)
  6465. {
  6466. CriticalBlock block(crit);
  6467. StringBuffer x;
  6468. tgt.append(exportWorkUnitToXML(this, x, false, false, false).str());
  6469. }
  6470. void CLocalWorkUnit::deserialize(MemoryBuffer &src)
  6471. {
  6472. CriticalBlock block(crit);
  6473. StringAttr value;
  6474. src.read(value);
  6475. loadXML(value);
  6476. }
  6477. void CLocalWorkUnit::requestAbort()
  6478. {
  6479. CriticalBlock block(crit);
  6480. abortWorkUnit(p->queryName());
  6481. }
  6482. void CLocalWorkUnit::unlockRemote()
  6483. {
  6484. CriticalBlock block(crit);
  6485. locked.unlock();
  6486. _unlockRemote();
  6487. }
  6488. IWorkUnit &CLocalWorkUnit::lockRemote(bool commit)
  6489. {
  6490. if (secMgr)
  6491. checkWuSecAccess(*this, secMgr.get(), secUser.get(), SecAccess_Write, "write lock", true, true);
  6492. locked.lock();
  6493. CriticalBlock block(crit);
  6494. if (commit)
  6495. {
  6496. try
  6497. {
  6498. _lockRemote();
  6499. }
  6500. catch (IException *E)
  6501. {
  6502. StringBuffer s;
  6503. IERRLOG("Failed to get write lock on workunit: %s", E->errorMessage(s).str());
  6504. locked.unlock();
  6505. throw;
  6506. }
  6507. }
  6508. return *new CLockedWorkUnit(LINK(this));
  6509. }
  6510. void CLocalWorkUnit::commit()
  6511. {
  6512. // Nothing to do if not backed by a persistent store
  6513. }
  6514. IWorkUnit& CLocalWorkUnit::lock()
  6515. {
  6516. return lockRemote(true);
  6517. }
  6518. IConstWorkUnit *CLocalWorkUnit::unlock()
  6519. {
  6520. unlockRemote();
  6521. return this;
  6522. }
  6523. const char *CLocalWorkUnit::queryWuid() const
  6524. {
  6525. CriticalBlock block(crit);
  6526. return p->queryName();
  6527. }
  6528. unsigned CLocalWorkUnit::getDebugAgentListenerPort() const
  6529. {
  6530. CriticalBlock block(crit);
  6531. return p->getPropInt("@DebugListenerPort", 0);
  6532. }
  6533. unsigned CLocalWorkUnit::getTotalThorTime() const
  6534. {
  6535. CriticalBlock block(crit);
  6536. if (p->hasProp("@totalThorTime"))
  6537. return (unsigned)nanoToMilli(extractTimeCollatable(p->queryProp("@totalThorTime"), nullptr));
  6538. const WuScopeFilter filter("stype[graph],nested[0],stat[TimeElapsed]");
  6539. StatsAggregation summary;
  6540. aggregateStatistic(summary, (IConstWorkUnit *)this, filter, StTimeElapsed);
  6541. return (unsigned)nanoToMilli(summary.getSum());
  6542. }
  6543. void CLocalWorkUnit::setDebugAgentListenerPort(unsigned port)
  6544. {
  6545. CriticalBlock block(crit);
  6546. p->setPropInt("@DebugListenerPort", port);
  6547. }
  6548. IStringVal& CLocalWorkUnit::getDebugAgentListenerIP(IStringVal &ip) const
  6549. {
  6550. CriticalBlock block(crit);
  6551. ip.set(p->queryProp("@DebugListenerIP"));
  6552. return ip;
  6553. }
  6554. void CLocalWorkUnit::setDebugAgentListenerIP(const char * ip)
  6555. {
  6556. CriticalBlock block(crit);
  6557. p->setProp("@DebugListenerIP", ip);
  6558. }
  6559. IStringVal & CLocalWorkUnit::getWorkunitDistributedAccessToken(IStringVal & datoken) const
  6560. {
  6561. CriticalBlock block(crit);
  6562. datoken.set(p->queryProp("@distributedAccessToken"));
  6563. return datoken;
  6564. }
  6565. bool CLocalWorkUnit::setDistributedAccessToken(const char * user)
  6566. {
  6567. CriticalBlock block(crit);
  6568. //If this format changes, you must update the isWorkunitDAToken() and extractFromWorkunitDAToken() methods
  6569. VStringBuffer datoken("HPCC[u=%s,w=%s]", user, queryWuid());
  6570. IDigitalSignatureManager * pDSM = queryDigitalSignatureManagerInstanceFromEnv();
  6571. if (pDSM && pDSM->isDigiSignerConfigured())
  6572. {
  6573. datoken.append(pDSM->queryKeyName()).append(';');
  6574. StringBuffer b64Signature;
  6575. if (pDSM->digiSign(b64Signature, datoken))
  6576. {
  6577. datoken.append(b64Signature.str());
  6578. }
  6579. else
  6580. {
  6581. ERRLOG("Cannot create workunit Distributed Access Token, digisign failed");
  6582. return false;
  6583. }
  6584. }
  6585. else
  6586. {
  6587. if (workUnitTraceLevel > 1)
  6588. WARNLOG("Cannot sign Distributed Access Token, digisign signing not configured");
  6589. datoken.append(";");
  6590. }
  6591. p->setProp("@distributedAccessToken", datoken);
  6592. return true;
  6593. }
  6594. bool CLocalWorkUnit::getRunningGraph(IStringVal &graphName, WUGraphIDType &subId) const
  6595. {
  6596. // Only implemented in derived classes
  6597. return false;
  6598. }
  6599. void CLocalWorkUnit::setJobName(const char *value)
  6600. {
  6601. CriticalBlock block(crit);
  6602. p->setProp("@jobName", value);
  6603. }
  6604. const char *CLocalWorkUnit::queryJobName() const
  6605. {
  6606. CriticalBlock block(crit);
  6607. const char *ret = p->queryProp("@jobName");
  6608. if (!ret)
  6609. ret = "";
  6610. return ret;
  6611. }
  6612. void CLocalWorkUnit::setClusterName(const char *value)
  6613. {
  6614. CriticalBlock block(crit);
  6615. p->setProp("@clusterName", value);
  6616. }
  6617. const char *CLocalWorkUnit::queryClusterName() const
  6618. {
  6619. CriticalBlock block(crit);
  6620. const char *ret = p->queryProp("@clusterName");
  6621. if (!ret)
  6622. ret = "";
  6623. return ret;
  6624. }
  6625. void CLocalWorkUnit::setAllowedClusters(const char *value)
  6626. {
  6627. setDebugValue("allowedclusters",value, true);
  6628. }
  6629. IStringVal& CLocalWorkUnit::getAllowedClusters(IStringVal &str) const
  6630. {
  6631. CriticalBlock block(crit);
  6632. getDebugValue("allowedclusters",str);
  6633. if (str.length()!=0)
  6634. return str;
  6635. str.set(p->queryProp("@clusterName"));
  6636. return str;
  6637. }
  6638. void CLocalWorkUnit::setAllowAutoQueueSwitch(bool val)
  6639. {
  6640. setDebugValueInt("allowautoqueueswitch",val?1:0,true);
  6641. }
  6642. bool CLocalWorkUnit::getAllowAutoQueueSwitch() const
  6643. {
  6644. CriticalBlock block(crit);
  6645. return getDebugValueBool("allowautoqueueswitch",false);
  6646. }
  6647. void CLocalWorkUnit::setLibraryInformation(const char * name, unsigned interfaceHash, unsigned definitionHash)
  6648. {
  6649. StringBuffer suffix;
  6650. if (name && *name)
  6651. setApplicationValue("LibraryModule", "name", name, true);
  6652. setApplicationValueInt("LibraryModule", "interfaceHash", interfaceHash, true);
  6653. setApplicationValueInt("LibraryModule", "definitionHash", definitionHash, true);
  6654. setApplicationValue("LibraryModule", "platform", appendLibrarySuffix(suffix).str(), true);
  6655. }
  6656. void CLocalWorkUnit::remoteCheckAccess(IUserDescriptor *user, bool writeaccess) const
  6657. {
  6658. unsigned auditflags = DALI_LDAP_AUDIT_REPORT|DALI_LDAP_READ_WANTED;
  6659. if (writeaccess)
  6660. auditflags |= DALI_LDAP_WRITE_WANTED;
  6661. SecAccessFlags perm = SecAccess_Full;
  6662. const char *scopename = p->queryProp("@scope");
  6663. if (scopename&&*scopename) {
  6664. if (!user)
  6665. user = queryUserDescriptor();
  6666. perm = querySessionManager().getPermissionsLDAP("workunit",scopename,user,auditflags);
  6667. if (perm<0) {
  6668. if (perm == SecAccess_Unavailable)
  6669. perm = SecAccess_Full;
  6670. else
  6671. perm = SecAccess_None;
  6672. }
  6673. }
  6674. if (!HASREADPERMISSION(perm))
  6675. throw MakeStringException(WUERR_WorkunitAccessDenied, "Read access denied for workunit %s", queryWuid());
  6676. if (writeaccess && !HASWRITEPERMISSION(perm))
  6677. throw MakeStringException(WUERR_WorkunitAccessDenied, "Write access denied for workunit %s", queryWuid());
  6678. }
  6679. void CLocalWorkUnit::setUser(const char * value)
  6680. {
  6681. CriticalBlock block(crit);
  6682. p->setProp("@submitID", value);
  6683. }
  6684. const char *CLocalWorkUnit::queryUser() const
  6685. {
  6686. CriticalBlock block(crit);
  6687. const char *ret = p->queryProp("@submitID");
  6688. if (!ret)
  6689. ret = "";
  6690. return ret;
  6691. }
  6692. void CLocalWorkUnit::setWuScope(const char * value)
  6693. {
  6694. if (value && *value)
  6695. {
  6696. if (checkWuScopeSecAccess(value, secMgr.get(), secUser.get(), SecAccess_Write, "Change Scope", true, true))
  6697. {
  6698. CriticalBlock block(crit);
  6699. p->setProp("@scope", value);
  6700. }
  6701. }
  6702. }
  6703. const char *CLocalWorkUnit::queryWuScope() const
  6704. {
  6705. CriticalBlock block(crit);
  6706. const char *ret = p->queryProp("@scope");
  6707. if (!ret)
  6708. ret = "";
  6709. return ret;
  6710. }
  6711. void CLocalWorkUnit::setPriority(WUPriorityClass cls)
  6712. {
  6713. CriticalBlock block(crit);
  6714. setEnum(p, "@priorityClass", cls, priorityClasses);
  6715. }
  6716. WUPriorityClass CLocalWorkUnit::getPriority() const
  6717. {
  6718. CriticalBlock block(crit);
  6719. return (WUPriorityClass) getEnum(p, "@priorityClass", priorityClasses);
  6720. }
  6721. const char *CLocalWorkUnit::queryPriorityDesc() const
  6722. {
  6723. return getEnumText(getPriority(), priorityClasses);
  6724. }
  6725. void CLocalWorkUnit::setState(WUState value)
  6726. {
  6727. if (value==WUStateAborted || value==WUStatePaused || value==WUStateCompleted || value==WUStateFailed || value==WUStateSubmitted || value==WUStateWait)
  6728. {
  6729. if (globalFactory)
  6730. globalFactory->clearAborting(queryWuid());
  6731. }
  6732. CriticalBlock block(crit);
  6733. setEnum(p, "@state", value, states); // For historical reasons, we use state to store the state
  6734. setEnum(p, "State", value, states); // But we can only subscribe to elements, not attributes
  6735. if (getDebugValueBool("monitorWorkunit", false))
  6736. {
  6737. switch(value)
  6738. {
  6739. case WUStateAborted:
  6740. FLLOG(MCoperatorWarning, "Workunit %s aborted", p->queryName());
  6741. break;
  6742. case WUStateCompleted:
  6743. FLLOG(MCoperatorProgress, "Workunit %s completed", p->queryName());
  6744. break;
  6745. case WUStateFailed:
  6746. FLLOG(MCoperatorProgress, "Workunit %s failed", p->queryName());
  6747. break;
  6748. }
  6749. }
  6750. p->removeProp("@stateEx");
  6751. }
  6752. void CLocalWorkUnit::setStateEx(const char * text)
  6753. {
  6754. CriticalBlock block(crit);
  6755. p->setProp("@stateEx", text);
  6756. }
  6757. void CLocalWorkUnit::setAgentSession(__int64 sessionId)
  6758. {
  6759. CriticalBlock block(crit);
  6760. p->setPropInt64("@agentSession", sessionId);
  6761. }
  6762. bool CLocalWorkUnit::getIsQueryService() const
  6763. {
  6764. CriticalBlock block(crit);
  6765. return p->getPropBool("@isQueryService", false);
  6766. }
  6767. void CLocalWorkUnit::setIsQueryService(bool value)
  6768. {
  6769. CriticalBlock block(crit);
  6770. p->setPropBool("@isQueryService", value);
  6771. }
  6772. void CLocalWorkUnit::checkAgentRunning(WUState & state)
  6773. {
  6774. if (queryDaliServerVersion().compare("2.1")<0)
  6775. return;
  6776. switch(state)
  6777. {
  6778. case WUStateRunning:
  6779. case WUStateDebugPaused:
  6780. case WUStateDebugRunning:
  6781. case WUStateBlocked:
  6782. case WUStateAborting:
  6783. case WUStateCompiling:
  6784. case WUStatePaused:
  6785. {
  6786. SessionId agent = getAgentSession();
  6787. if((agent>0) && querySessionManager().sessionStopped(agent, 0))
  6788. {
  6789. forceReload();
  6790. state = (WUState) getEnum(p, "@state", states);
  6791. bool isecl=state==WUStateCompiling;
  6792. if (aborting())
  6793. state = WUStateAborted;
  6794. else if (state==WUStateRunning || state==WUStatePaused || state==WUStateDebugPaused || state==WUStateDebugRunning || state==WUStateBlocked || state==WUStateCompiling)
  6795. state = WUStateFailed;
  6796. else
  6797. return;
  6798. WARNLOG("checkAgentRunning terminated: %" I64F "d state = %d",(__int64)agent,(int)state);
  6799. Owned<IWorkUnit> w = &lock();
  6800. w->setState(state);
  6801. Owned<IWUException> e = w->createException();
  6802. WUAction action = w->getAction();
  6803. switch (action)
  6804. {
  6805. case WUActionPause:
  6806. case WUActionPauseNow:
  6807. case WUActionResume:
  6808. w->setAction(WUActionUnknown);
  6809. }
  6810. if(isecl)
  6811. {
  6812. e->setExceptionCode(1001);
  6813. e->setExceptionMessage("EclServer terminated unexpectedly");
  6814. }
  6815. else
  6816. {
  6817. e->setExceptionCode(1000);
  6818. e->setExceptionMessage("Workunit terminated unexpectedly");
  6819. }
  6820. }
  6821. }
  6822. }
  6823. }
  6824. WUState CLocalWorkUnit::getState() const
  6825. {
  6826. CriticalBlock block(crit);
  6827. WUState state = (WUState) getEnum(p, "@state", states);
  6828. switch (state)
  6829. {
  6830. case WUStateRunning:
  6831. case WUStateDebugPaused:
  6832. case WUStateDebugRunning:
  6833. case WUStateBlocked:
  6834. case WUStateCompiling:
  6835. if (aborting())
  6836. state = WUStateAborting;
  6837. break;
  6838. case WUStateSubmitted:
  6839. if (aborting())
  6840. state = WUStateAborted;
  6841. break;
  6842. }
  6843. const_cast<CLocalWorkUnit *>(this)->checkAgentRunning(state); //need const_cast as will change state if agent has died
  6844. return state;
  6845. }
  6846. IStringVal& CLocalWorkUnit::getStateEx(IStringVal & str) const
  6847. {
  6848. CriticalBlock block(crit);
  6849. str.set(p->queryProp("@stateEx"));
  6850. return str;
  6851. }
  6852. __int64 CLocalWorkUnit::getAgentSession() const
  6853. {
  6854. CriticalBlock block(crit);
  6855. return p->getPropInt64("@agentSession", -1);
  6856. }
  6857. unsigned CLocalWorkUnit::getAgentPID() const
  6858. {
  6859. CriticalBlock block(crit);
  6860. return p->getPropInt("@agentPID", -1);
  6861. }
  6862. const char * CLocalWorkUnit::queryStateDesc() const
  6863. {
  6864. // MORE - not sure about this - may prefer a separate interface
  6865. CriticalBlock block(crit);
  6866. try
  6867. {
  6868. return getEnumText(getState(), states);
  6869. }
  6870. catch (...)
  6871. {
  6872. return "???";
  6873. }
  6874. }
  6875. void CLocalWorkUnit::setAction(WUAction value)
  6876. {
  6877. CriticalBlock block(crit);
  6878. setEnum(p, "Action", value, actions);
  6879. }
  6880. WUAction CLocalWorkUnit::getAction() const
  6881. {
  6882. CriticalBlock block(crit);
  6883. return (WUAction) getEnum(p, "Action", actions);
  6884. }
  6885. const char *CLocalWorkUnit::queryActionDesc() const
  6886. {
  6887. CriticalBlock block(crit);
  6888. return p->queryProp("Action");
  6889. }
  6890. bool CLocalWorkUnit::hasApplicationValue(const char *app, const char *propname) const
  6891. {
  6892. StringBuffer prop("Application/");
  6893. prop.append(app).append('/').append(propname);
  6894. CriticalBlock block(crit);
  6895. return p->hasProp(prop);
  6896. }
  6897. IStringVal& CLocalWorkUnit::getApplicationValue(const char *app, const char *propname, IStringVal &str) const
  6898. {
  6899. StringBuffer prop("Application/");
  6900. prop.append(app).append('/').append(propname);
  6901. CriticalBlock block(crit);
  6902. str.set(p->queryProp(prop.str()));
  6903. return str;
  6904. }
  6905. int CLocalWorkUnit::getApplicationValueInt(const char *app, const char *propname, int defVal) const
  6906. {
  6907. StringBuffer prop("Application/");
  6908. prop.append(app).append('/').append(propname);
  6909. CriticalBlock block(crit);
  6910. return p->getPropInt(prop.str(), defVal);
  6911. }
  6912. IConstWUAppValueIterator& CLocalWorkUnit::getApplicationValues() const
  6913. {
  6914. CriticalBlock block(crit);
  6915. appvalues.load(p,"Application/*");
  6916. return *new CArrayIteratorOf<IConstWUAppValue,IConstWUAppValueIterator> (appvalues, 0, (IConstWorkUnit *) this);
  6917. }
  6918. void CLocalWorkUnit::setApplicationValue(const char *app, const char *propname, const char *value, bool overwrite)
  6919. {
  6920. CriticalBlock block(crit);
  6921. StringBuffer prop("Application/");
  6922. prop.append(app).append('/').append(propname);
  6923. if (overwrite || !p->hasProp(prop.str()))
  6924. {
  6925. StringBuffer sp;
  6926. p->setProp(sp.append("Application").str(), "");
  6927. p->setProp(sp.append('/').append(app).str(), "");
  6928. p->setProp(prop.str(), value);
  6929. }
  6930. }
  6931. void CLocalWorkUnit::setApplicationValueInt(const char *app, const char *propname, int value, bool overwrite)
  6932. {
  6933. VStringBuffer s("%d", value);
  6934. setApplicationValue(app, propname, s, overwrite);
  6935. }
  6936. void CLocalWorkUnit::setPriorityLevel(int level)
  6937. {
  6938. CriticalBlock block(crit);
  6939. p->setPropInt("PriorityFlag", level);
  6940. }
  6941. int CLocalWorkUnit::getPriorityLevel() const
  6942. {
  6943. CriticalBlock block(crit);
  6944. return p->getPropInt("PriorityFlag");
  6945. }
  6946. int calcPriorityValue(const IPropertyTree * p)
  6947. {
  6948. int priority = p->getPropInt("PriorityFlag");
  6949. switch((WUPriorityClass) getEnum(p, "@priorityClass", priorityClasses))
  6950. {
  6951. case PriorityClassLow:
  6952. priority -= 100;
  6953. break;
  6954. case PriorityClassHigh:
  6955. priority += 100;
  6956. break;
  6957. }
  6958. return priority;
  6959. }
  6960. int CLocalWorkUnit::getPriorityValue() const
  6961. {
  6962. CriticalBlock block(crit);
  6963. return calcPriorityValue(p);
  6964. }
  6965. void CLocalWorkUnit::setRescheduleFlag(bool value)
  6966. {
  6967. CriticalBlock block(crit);
  6968. p->setPropInt("RescheduleFlag", (int) value);
  6969. }
  6970. bool CLocalWorkUnit::getRescheduleFlag() const
  6971. {
  6972. CriticalBlock block(crit);
  6973. return p->getPropInt("RescheduleFlag") != 0;
  6974. }
  6975. class NullIStringIterator : implements IStringIterator, public CInterface
  6976. {
  6977. public:
  6978. IMPLEMENT_IINTERFACE;
  6979. bool first() { return false; }
  6980. bool next() { return false; }
  6981. bool isValid() { return false; }
  6982. IStringVal & str(IStringVal & str) { return str; }
  6983. };
  6984. ClusterType getClusterType(const char * platform, ClusterType dft)
  6985. {
  6986. if (stricmp(platform, "thor") == 0)
  6987. return ThorLCRCluster;
  6988. if (stricmp(platform, "thorlcr") == 0)
  6989. return ThorLCRCluster;
  6990. if (stricmp(platform, "hthor") == 0)
  6991. return HThorCluster;
  6992. if (stricmp(platform, "roxie") == 0)
  6993. return RoxieCluster;
  6994. return dft;
  6995. }
  6996. const char *clusterTypeString(ClusterType clusterType, bool lcrSensitive)
  6997. {
  6998. switch (clusterType)
  6999. {
  7000. case ThorLCRCluster:
  7001. if (lcrSensitive)
  7002. return "thorlcr";
  7003. return "thor";
  7004. case RoxieCluster:
  7005. return "roxie";
  7006. case HThorCluster:
  7007. return "hthor";
  7008. }
  7009. throwUnexpected();
  7010. }
  7011. bool extractFromWorkunitDAToken(const char * distributedAccessToken, StringBuffer * wuid, StringBuffer * user, StringBuffer * privKey)
  7012. {
  7013. if (!isWorkunitDAToken(distributedAccessToken))
  7014. {
  7015. DBGLOG("Not a valid workunit distributed access token");
  7016. return false;
  7017. }
  7018. //Extract the string between [ and ]
  7019. const char * pEndBracket = strchr(distributedAccessToken + 5, ']');
  7020. StringBuffer tokenValues;
  7021. tokenValues.append(pEndBracket - distributedAccessToken - 5, distributedAccessToken + 5);
  7022. StringArray nameValues;
  7023. nameValues.appendList(tokenValues.str(), ",",true);
  7024. if (user || wuid)
  7025. {
  7026. for (int x = 0; x < nameValues.ordinality(); x++)
  7027. {
  7028. if (user && 0==strncmp(nameValues[x],"u=",2))//Extract user
  7029. user->append(nameValues[x] + 2);
  7030. if (wuid && 0==strncmp(nameValues[x],"w=",2))//Extract wuid
  7031. wuid->append(nameValues[x] + 2);
  7032. }
  7033. }
  7034. if (privKey)
  7035. {
  7036. const char * finger = pEndBracket;
  7037. ++finger;
  7038. while (*finger && *finger != ';')
  7039. privKey->append(1, finger++);
  7040. }
  7041. return true;
  7042. }
  7043. //Verifies the given signed workunit distributed access token was created
  7044. //and signed by the given wuid and user, and that the workunit is still active
  7045. //Returns:
  7046. // 0 : Success, token is valid and workunit is active
  7047. // 1 : Signature does not verify (wuid/username don't match, or signature does not verify)
  7048. // 2 : Workunit not active
  7049. // Throws if unable to open workunit
  7050. wuTokenStates verifyWorkunitDAToken(const char * ctxUser, const char * daToken)
  7051. {
  7052. #ifdef _CONTAINERIZED
  7053. if (!getComponentConfigSP()->getPropBool("@wuTokens", false))
  7054. return wuTokenInvalid;
  7055. #endif
  7056. if (isEmptyString(daToken))
  7057. {
  7058. ERRLOG("verifyWorkunitDAToken : Token must be provided");
  7059. return wuTokenInvalid;
  7060. }
  7061. StringBuffer tokWuid;
  7062. StringBuffer tokUser;
  7063. if (!extractFromWorkunitDAToken(daToken, &tokWuid, &tokUser, nullptr))//get the wuid and user
  7064. {
  7065. //Not a valid workunit distributed access token
  7066. return wuTokenInvalid;
  7067. }
  7068. //Validate signature
  7069. IDigitalSignatureManager * pDSM = queryDigitalSignatureManagerInstanceFromEnv();
  7070. if (pDSM && pDSM->isDigiVerifierConfigured())
  7071. {
  7072. const char * finger;
  7073. StringBuffer token;//receives copy of everything up until signature
  7074. for (finger = daToken; *finger && *finger != ';'; finger++)
  7075. token.append(1, finger);
  7076. token.append(1, finger);//append ;
  7077. StringBuffer sig(++finger);
  7078. if (!pDSM->digiVerify(sig, token))
  7079. {
  7080. ERRLOG("verifyWorkunitDAToken : workunit distributed access token does not verify");
  7081. return wuTokenInvalid;
  7082. }
  7083. }
  7084. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  7085. Owned<IConstWorkUnit> cw = factory->openWorkUnit(tokWuid.str());
  7086. if(!cw)
  7087. {
  7088. throw MakeStringException(WUERR_WorkunitAccessDenied,"verifyWorkunitDAToken : Cannot open workunit %s",tokWuid.str());
  7089. }
  7090. //Verify user matches
  7091. bool wuUserExist = !isEmptyString(cw->queryUser());
  7092. bool tokUserExist = !isEmptyString(tokUser.str());
  7093. bool ctxUserExist = !isEmptyString(ctxUser);
  7094. if (wuUserExist && tokUserExist)
  7095. {
  7096. //if both users are found, they must match
  7097. if (!streq(tokUser.str(), cw->queryUser()))
  7098. {
  7099. ERRLOG("verifyWorkunitDAToken : Token user (%s) does not match WU user (%s)", tokUser.str(), cw->queryUser());
  7100. return wuTokenInvalid;//Possible Internal error
  7101. }
  7102. else if (ctxUserExist && !streq(tokUser.str(), ctxUser))//ctxUser will be empty if security not enabled
  7103. {
  7104. ERRLOG("verifyWorkunitDAToken : Token user (%s) does not match Context user (%s)", cw->queryUser(), ctxUser);
  7105. return wuTokenInvalid;
  7106. }
  7107. }
  7108. else if (!wuUserExist && !tokUserExist)//both users will be empty if no security enabled
  7109. {
  7110. if (ctxUserExist)
  7111. {
  7112. ERRLOG("verifyWorkunitDAToken : Security enabled but WU user and Token user not specified");
  7113. return wuTokenInvalid;
  7114. }
  7115. //both users empty and no context user means if no security enabled
  7116. }
  7117. else
  7118. {
  7119. //one user found, but not the other, treat as an error
  7120. ERRLOG("verifyWorkunitDAToken : WU user %s and Token user %s must be provided", wuUserExist ? cw->queryUser() : "(NULL)", tokUserExist ? tokUser.str() : "(NULL)");
  7121. return wuTokenInvalid;
  7122. }
  7123. // no need to compare tokWuid with workunit wuid, because it will always match
  7124. bool wuActive;
  7125. switch (cw->getState())
  7126. {
  7127. case WUStateRunning:
  7128. case WUStateDebugRunning:
  7129. case WUStateBlocked:
  7130. case WUStateAborting:
  7131. case WUStateUploadingFiles:
  7132. #ifdef _DEBUG
  7133. DBGLOG("verifyWorkunitDAToken : Workunit token validated for %s %s, state is '%s'", cw->queryWuid(), cw->queryUser(), getWorkunitStateStr(cw->getState()));
  7134. #endif
  7135. wuActive = true;
  7136. break;
  7137. default:
  7138. ERRLOG("verifyWorkunitDAToken : Workunit %s not active, state is '%s'", cw->queryWuid(), getWorkunitStateStr(cw->getState()));
  7139. wuActive = false;
  7140. break;
  7141. }
  7142. return wuActive ? wuTokenValid : wuTokenWorkunitInactive;
  7143. }
  7144. bool CLocalWorkUnit::resolveFilePrefix(StringBuffer & prefix, const char * queue) const
  7145. {
  7146. if (hasApplicationValue("prefix", queue))
  7147. {
  7148. getApplicationValue("prefix", queue, StringBufferAdaptor(prefix));
  7149. return true;
  7150. }
  7151. #ifndef _CONTAINERIZED
  7152. Owned<IConstWUClusterInfo> ci = getTargetClusterInfo(queue);
  7153. if (ci)
  7154. {
  7155. ci->getScope(StringBufferAdaptor(prefix));
  7156. return true;
  7157. }
  7158. #endif
  7159. return false;
  7160. }
  7161. IStringVal& CLocalWorkUnit::getScope(IStringVal &str) const
  7162. {
  7163. StringBuffer prefix;
  7164. CriticalBlock block(crit);
  7165. if (p->hasProp("Debug/ForceScope"))
  7166. {
  7167. prefix.append(p->queryProp("Debug/ForceScope")).toLowerCase();
  7168. }
  7169. else
  7170. {
  7171. resolveFilePrefix(prefix, p->queryProp("@clusterName"));
  7172. }
  7173. str.set(prefix.str());
  7174. return str;
  7175. }
  7176. //Queries
  7177. void CLocalWorkUnit::setCodeVersion(unsigned codeVersion, const char * buildVersion, const char * eclVersion)
  7178. {
  7179. CriticalBlock block(crit);
  7180. p->setPropInt("@codeVersion", codeVersion);
  7181. p->setProp("@buildVersion", buildVersion);
  7182. p->setProp("@eclVersion", eclVersion);
  7183. }
  7184. unsigned CLocalWorkUnit::getCodeVersion() const
  7185. {
  7186. CriticalBlock block(crit);
  7187. return p->getPropInt("@codeVersion");
  7188. }
  7189. unsigned CLocalWorkUnit::getWuidVersion() const
  7190. {
  7191. CriticalBlock block(crit);
  7192. return p->getPropInt("@wuidVersion");
  7193. }
  7194. void CLocalWorkUnit::getBuildVersion(IStringVal & buildVersion, IStringVal & eclVersion) const
  7195. {
  7196. CriticalBlock block(crit);
  7197. buildVersion.set(p->queryProp("@buildVersion"));
  7198. eclVersion.set(p->queryProp("@eclVersion"));
  7199. }
  7200. void CLocalWorkUnit::setCloneable(bool value)
  7201. {
  7202. CriticalBlock block(crit);
  7203. p->setPropInt("@cloneable", value);
  7204. }
  7205. void CLocalWorkUnit::setIsClone(bool value)
  7206. {
  7207. CriticalBlock block(crit);
  7208. p->setPropInt("@isClone", value);
  7209. }
  7210. bool CLocalWorkUnit::getCloneable() const
  7211. {
  7212. CriticalBlock block(crit);
  7213. return p->getPropBool("@cloneable", false);
  7214. }
  7215. IUserDescriptor *CLocalWorkUnit::queryUserDescriptor() const
  7216. {
  7217. CriticalBlock block(crit);
  7218. if (!userDesc)
  7219. {
  7220. userDesc.setown(createUserDescriptor());
  7221. SCMStringBuffer token;
  7222. getWorkunitDistributedAccessToken(token);
  7223. userDesc->set(queryUser(), token.str());//use token as password
  7224. }
  7225. return userDesc;
  7226. }
  7227. bool CLocalWorkUnit::isProtected() const
  7228. {
  7229. CriticalBlock block(crit);
  7230. return p->getPropBool("@protected", false);
  7231. }
  7232. bool CLocalWorkUnit::isPausing() const
  7233. {
  7234. CriticalBlock block(crit);
  7235. if (WUActionPause == getAction())
  7236. {
  7237. switch (getState())
  7238. {
  7239. case WUStateRunning:
  7240. case WUStateAborting:
  7241. return true;
  7242. }
  7243. }
  7244. return false;
  7245. }
  7246. void CLocalWorkUnit::protect(bool protectMode)
  7247. {
  7248. CriticalBlock block(crit);
  7249. p->setPropBool("@protected", protectMode);
  7250. }
  7251. void CLocalWorkUnit::setResultLimit(unsigned value)
  7252. {
  7253. CriticalBlock block(crit);
  7254. p->setPropInt("resultLimit", value);
  7255. }
  7256. unsigned CLocalWorkUnit::getResultLimit() const
  7257. {
  7258. CriticalBlock block(crit);
  7259. return p->getPropInt("resultLimit");
  7260. }
  7261. IStringVal & CLocalWorkUnit::getSnapshot(IStringVal & str) const
  7262. {
  7263. CriticalBlock block(crit);
  7264. str.set(p->queryProp("SNAPSHOT"));
  7265. return str;
  7266. }
  7267. void CLocalWorkUnit::setSnapshot(const char * val)
  7268. {
  7269. CriticalBlock block(crit);
  7270. p->setProp("SNAPSHOT", val);
  7271. }
  7272. const static EnumMapping warningSeverityMap[] =
  7273. {
  7274. { SeverityInformation, "info" },
  7275. { SeverityWarning, "warning" },
  7276. { SeverityError, "error" },
  7277. { SeverityAlert, "alert" },
  7278. { SeverityIgnore, "ignore" },
  7279. { SeverityFatal, "fatal" },
  7280. { SeverityUnknown, NULL }
  7281. };
  7282. ErrorSeverity CLocalWorkUnit::getWarningSeverity(unsigned code, ErrorSeverity defaultSeverity) const
  7283. {
  7284. StringBuffer xpath;
  7285. xpath.append("OnWarnings/OnWarning[@code='").append(code).append("']");
  7286. CriticalBlock block(crit);
  7287. IPropertyTree * mapping = p->queryPropTree(xpath);
  7288. if (mapping)
  7289. return (ErrorSeverity) getEnum(mapping, "@severity", warningSeverityMap);
  7290. return defaultSeverity;
  7291. }
  7292. void CLocalWorkUnit::setWarningSeverity(unsigned code, ErrorSeverity severity)
  7293. {
  7294. StringBuffer xpath;
  7295. xpath.append("OnWarnings/OnWarning[@code='").append(code).append("']");
  7296. CriticalBlock block(crit);
  7297. IPropertyTree * mapping = p->queryPropTree(xpath);
  7298. if (!mapping)
  7299. {
  7300. IPropertyTree * onWarnings = ensurePTree(p, "OnWarnings");
  7301. mapping = onWarnings->addPropTree("OnWarning");
  7302. mapping->setPropInt("@code", code);
  7303. }
  7304. setEnum(mapping, "@severity", severity, warningSeverityMap);
  7305. }
  7306. static int comparePropTrees(IInterface * const *ll, IInterface * const *rr)
  7307. {
  7308. IPropertyTree *l = (IPropertyTree *) *ll;
  7309. IPropertyTree *r = (IPropertyTree *) *rr;
  7310. return stricmp(l->queryName(), r->queryName());
  7311. };
  7312. unsigned CLocalWorkUnit::calculateHash(unsigned crc)
  7313. {
  7314. // Any other values in the WU that could affect generated code should be crc'ed here
  7315. IPropertyTree *tree = p->queryBranch("Debug");
  7316. if (tree)
  7317. {
  7318. Owned<IPropertyTreeIterator> sub = tree->getElements("*");
  7319. ICopyArrayOf<IPropertyTree> subs;
  7320. for(sub->first(); sub->isValid(); sub->next())
  7321. subs.append(sub->query());
  7322. subs.sort(comparePropTrees);
  7323. ForEachItemIn(idx, subs)
  7324. {
  7325. const char *name = subs.item(idx).queryName();
  7326. const char *val = subs.item(idx).queryProp(NULL);
  7327. crc = crc32(name, (size32_t)strlen(name), crc);
  7328. if (val)
  7329. crc = crc32(val, (size32_t)strlen(val), crc);
  7330. }
  7331. }
  7332. Owned<IConstWUPluginIterator> plugins = &getPlugins();
  7333. for (plugins->first();plugins->isValid();plugins->next())
  7334. {
  7335. IConstWUPlugin &thisplugin = plugins->query();
  7336. SCMStringBuffer version;
  7337. thisplugin.getPluginVersion(version);
  7338. crc = crc32(version.str(), version.length(), crc);
  7339. }
  7340. return crc;
  7341. }
  7342. static void updateProp(IPropertyTree * to, const IPropertyTree * from, const char * xpath)
  7343. {
  7344. if (!to->hasProp(xpath) && from->hasProp(xpath))
  7345. to->setProp(xpath, from->queryProp(xpath));
  7346. }
  7347. static void setProp(IPropertyTree * to, const IPropertyTree * from, const char * xpath)
  7348. {
  7349. if (from->hasProp(xpath))
  7350. to->setProp(xpath, from->queryProp(xpath));
  7351. }
  7352. static void copyTree(IPropertyTree * to, const IPropertyTree * from, const char * xpath)
  7353. {
  7354. IPropertyTree * match = from->getBranch(xpath);
  7355. if (match)
  7356. to->setPropTree(xpath, match);
  7357. }
  7358. IPropertyTree *CLocalWorkUnit::queryPTree() const
  7359. {
  7360. return p;
  7361. }
  7362. void CLocalWorkUnit::copyWorkUnit(IConstWorkUnit *cached, bool copyStats, bool all)
  7363. {
  7364. CLocalWorkUnit *from = QUERYINTERFACE(cached, CLocalWorkUnit);
  7365. if (!from)
  7366. {
  7367. CLockedWorkUnit *fl = QUERYINTERFACE(cached, CLockedWorkUnit);
  7368. if (!fl)
  7369. throw MakeStringException(WUERR_InternalUnknownImplementation, "Cached workunit not created using workunit dll");
  7370. from = fl->c;
  7371. }
  7372. // Need to copy the query, the results, and the graphs from the cached query.
  7373. // The cache is made before the query is executed so there is no need to clear them.
  7374. if (!cached->getCloneable())
  7375. throw MakeStringException(WUERR_CannotCloneWorkunit, "Source work unit not marked as clonable");
  7376. const IPropertyTree * fromP = from->p;
  7377. IPropertyTree *pt;
  7378. CriticalBlock block(crit);
  7379. clearCached(false);
  7380. query.clear();
  7381. updateProp(p, fromP, "@jobName");
  7382. copyTree(p, fromP, "Query");
  7383. pt = fromP->getBranch("Application");
  7384. if (pt)
  7385. synchronizePTree(ensurePTree(p, "Application"), pt, false, false);
  7386. pt = fromP->queryBranch("Debug");
  7387. if (pt)
  7388. {
  7389. IPropertyTree *curDebug = p->queryPropTree("Debug");
  7390. if (curDebug)
  7391. {
  7392. Owned<IPropertyTreeIterator> elems = pt->getElements("*");
  7393. ForEach(*elems)
  7394. {
  7395. IPropertyTree *elem = &elems->query();
  7396. if (!curDebug->hasProp(elem->queryName()))
  7397. curDebug->setPropTree(elem->queryName(),LINK(elem));
  7398. }
  7399. }
  7400. else
  7401. p->setPropTree("Debug", LINK(pt));
  7402. }
  7403. copyTree(p, fromP, "OnWarnings");
  7404. copyTree(p, fromP, "Plugins");
  7405. copyTree(p, fromP, "Libraries");
  7406. copyTree(p, fromP, "Results");
  7407. copyTree(p, fromP, "Graphs");
  7408. copyTree(p, fromP, "Workflow");
  7409. copyTree(p, fromP, "WebServicesInfo");
  7410. if (copyStats)
  7411. {
  7412. // Merge timing info from both branches
  7413. pt = fromP->getBranch("Statistics");
  7414. if (pt)
  7415. {
  7416. IPropertyTree *tgtStatistics = ensurePTree(p, "Statistics");
  7417. mergePTree(tgtStatistics, pt);
  7418. pt->Release();
  7419. }
  7420. }
  7421. updateProp(p, fromP, "@clusterName");
  7422. updateProp(p, fromP, "allowedclusters");
  7423. updateProp(p, fromP, "@submitID");
  7424. updateProp(p, fromP, "SNAPSHOT");
  7425. setProp(p, fromP, "@eventScheduledCount");
  7426. //MORE: This is very adhoc. All options that should be cloned should really be in a common branch
  7427. if (all)
  7428. {
  7429. setProp(p, fromP, "PriorityFlag");
  7430. setProp(p, fromP, "@priorityClass");
  7431. setProp(p, fromP, "@protected");
  7432. setProp(p, fromP, "@clusterName");
  7433. updateProp(p, fromP, "@scope");
  7434. }
  7435. //Variables may have been set up as parameters to the query - so need to preserve any values that were supplied.
  7436. pt = fromP->getBranch("Variables");
  7437. if (pt)
  7438. {
  7439. IPropertyTree *ptTgtVariables = ensurePTree(p, "Variables");
  7440. Owned<IPropertyTreeIterator> ptiVariable = pt->getElements("Variable");
  7441. for (ptiVariable->first(); ptiVariable->isValid(); ptiVariable->next())
  7442. {
  7443. IPropertyTree *ptSrcVariable = &ptiVariable->query();
  7444. const char *name = ptSrcVariable->queryProp("@name");
  7445. assertex(name);
  7446. StringBuffer xpath;
  7447. xpath.append("Variable[@name='").append(name).append("']");
  7448. IPropertyTree *ptTgtVariable = ptTgtVariables->queryPropTree(xpath.str());
  7449. IPropertyTree *merged = createPTreeFromIPT(ptSrcVariable); // clone entire source info...
  7450. merged->removeProp("Value"); // except value and status
  7451. merged->setProp("@status", "undefined");
  7452. if (!merged->getPropBool("@isScalar"))
  7453. merged->removeProp("totalRowCount");
  7454. merged->removeProp("rowCount");
  7455. // If there are any other fields that get set ONLY by eclagent, strip them out here...
  7456. if (ptTgtVariable)
  7457. {
  7458. // copy status and Value from what is already set in target
  7459. merged->setProp("@status", ptTgtVariable->queryProp("@status"));
  7460. MemoryBuffer value;
  7461. if (ptTgtVariable->getPropBin("Value", value))
  7462. merged->setPropBin("Value", value.length(), value.toByteArray());
  7463. ptTgtVariable->removeProp(xpath.str());
  7464. // If there are any other fields in a variable that get set by ws_ecl before submitting, copy them across here...
  7465. }
  7466. ptTgtVariables->addPropTree("Variable", merged);
  7467. }
  7468. pt->Release();
  7469. }
  7470. p->setProp("@codeVersion", fromP->queryProp("@codeVersion"));
  7471. p->setProp("@buildVersion", fromP->queryProp("@buildVersion"));
  7472. p->setProp("@eclVersion", fromP->queryProp("@eclVersion"));
  7473. p->setProp("@totalThorTime", fromP->queryProp("@totalThorTime"));
  7474. p->setProp("@hash", fromP->queryProp("@hash"));
  7475. p->setProp("@costExecute", fromP->queryProp("@costExecute"));
  7476. p->setProp("@costFileAccess", fromP->queryProp("@costFileAccess"));
  7477. p->setPropBool("@cloneable", true);
  7478. p->setPropBool("@isClone", true);
  7479. resetWorkflow(); // the source Workflow section may have had some parts already executed...
  7480. Owned<IPropertyTreeIterator> results = p->getElements("Results/Result");
  7481. ForEach(*results)
  7482. {
  7483. CLocalWUResult result(LINK(&results->query()));
  7484. result.setResultStatus(ResultStatusUndefined);
  7485. }
  7486. copyTree(p, fromP, "usedsources"); // field usage
  7487. }
  7488. bool CLocalWorkUnit::hasDebugValue(const char *propname) const
  7489. {
  7490. StringBuffer lower;
  7491. lower.append(propname).toLowerCase();
  7492. CriticalBlock block(crit);
  7493. StringBuffer prop("Debug/");
  7494. return p->hasProp(prop.append(lower));
  7495. }
  7496. IStringVal& CLocalWorkUnit::getDebugValue(const char *propname, IStringVal &str) const
  7497. {
  7498. StringBuffer lower;
  7499. lower.append(propname).toLowerCase();
  7500. CriticalBlock block(crit);
  7501. StringBuffer prop("Debug/");
  7502. str.set(p->queryProp(prop.append(lower).str()));
  7503. return str;
  7504. }
  7505. IStringIterator& CLocalWorkUnit::getDebugValues() const
  7506. {
  7507. return getDebugValues(NULL);
  7508. }
  7509. IStringIterator& CLocalWorkUnit::getDebugValues(const char *prop) const
  7510. {
  7511. CriticalBlock block(crit);
  7512. StringBuffer path("Debug/");
  7513. if (prop)
  7514. {
  7515. StringBuffer lower;
  7516. lower.append(prop).toLowerCase();
  7517. path.append(lower);
  7518. }
  7519. else
  7520. path.append("*");
  7521. return *new CStringPTreeTagIterator(p->getElements(path.str()));
  7522. }
  7523. int CLocalWorkUnit::getDebugValueInt(const char *propname, int defVal) const
  7524. {
  7525. StringBuffer lower;
  7526. lower.append(propname).toLowerCase();
  7527. CriticalBlock block(crit);
  7528. StringBuffer prop("Debug/");
  7529. prop.append(lower);
  7530. return p->getPropInt(prop.str(), defVal);
  7531. }
  7532. __int64 CLocalWorkUnit::getDebugValueInt64(const char *propname, __int64 defVal) const
  7533. {
  7534. StringBuffer lower;
  7535. lower.append(propname).toLowerCase();
  7536. CriticalBlock block(crit);
  7537. StringBuffer prop("Debug/");
  7538. prop.append(lower);
  7539. return p->getPropInt64(prop.str(), defVal);
  7540. }
  7541. double CLocalWorkUnit::getDebugValueReal(const char *propname, double defVal) const
  7542. {
  7543. StringBuffer lower;
  7544. lower.append(propname).toLowerCase();
  7545. CriticalBlock block(crit);
  7546. StringBuffer prop("Debug/");
  7547. prop.append(lower);
  7548. return p->getPropReal(prop.str(), defVal);
  7549. }
  7550. bool CLocalWorkUnit::getDebugValueBool(const char * propname, bool defVal) const
  7551. {
  7552. StringBuffer lower;
  7553. lower.append(propname).toLowerCase();
  7554. CriticalBlock block(crit);
  7555. StringBuffer prop("Debug/");
  7556. prop.append(lower);
  7557. return p->getPropBool(prop.str(), defVal);
  7558. }
  7559. IStringIterator *CLocalWorkUnit::getLogs(const char *type, const char *instance) const
  7560. {
  7561. VStringBuffer xpath("Process/%s/", type);
  7562. if (instance)
  7563. xpath.append(instance);
  7564. else
  7565. xpath.append("*");
  7566. CriticalBlock block(crit);
  7567. if (p->getPropInt("@wuidVersion") < 1) // legacy wuid
  7568. {
  7569. // NB: instance unused
  7570. if (streq("EclAgent", type))
  7571. return new CStringPTreeIterator(p->getElements("Debug/eclagentlog"));
  7572. else if (streq("Thor", type))
  7573. return new CStringPTreeIterator(p->getElements("Debug/thorlog*"));
  7574. VStringBuffer xpath("Debug/%s", type);
  7575. return new CStringPTreeIterator(p->getElements(xpath.str()));
  7576. }
  7577. else
  7578. return new CStringPTreeAttrIterator(p->getElements(xpath.str()), "@log");
  7579. }
  7580. IPropertyTreeIterator* CLocalWorkUnit::getProcesses(const char *type, const char *instance) const
  7581. {
  7582. VStringBuffer xpath("Process/%s/", type);
  7583. if (instance)
  7584. xpath.append(instance);
  7585. else
  7586. xpath.append("*");
  7587. CriticalBlock block(crit);
  7588. return p->getElements(xpath.str());
  7589. }
  7590. IStringIterator *CLocalWorkUnit::getProcesses(const char *type) const
  7591. {
  7592. VStringBuffer xpath("Process/%s/*", type);
  7593. CriticalBlock block(crit);
  7594. return new CStringPTreeTagIterator(p->getElements(xpath.str()));
  7595. }
  7596. void CLocalWorkUnit::addProcess(const char *type, const char *instance, unsigned pid,
  7597. unsigned max, const char *pattern, bool singleLog, const char *log)
  7598. {
  7599. VStringBuffer processType("Process/%s", type);
  7600. VStringBuffer xpath("%s/%s", processType.str(), instance);
  7601. if (log)
  7602. xpath.appendf("[@log=\"%s\"]", log);
  7603. CriticalBlock block(crit);
  7604. if (!p->hasProp(xpath))
  7605. {
  7606. IPropertyTree *node = ensurePTree(p, processType.str());
  7607. node = node->addPropTree(instance);
  7608. node->setProp("@log", log);
  7609. node->setPropInt("@pid", pid);
  7610. if (max > 0)
  7611. node->setPropInt("@max", max);
  7612. if (!isEmptyString(pattern))
  7613. node->setProp("@pattern", pattern);
  7614. if (singleLog)
  7615. node->setPropBool("@singleLog", true);
  7616. }
  7617. }
  7618. void CLocalWorkUnit::setDebugValue(const char *propname, const char *value, bool overwrite)
  7619. {
  7620. StringBuffer lower;
  7621. lower.append(propname).toLowerCase();
  7622. CriticalBlock block(crit);
  7623. StringBuffer prop("Debug/");
  7624. prop.append(lower);
  7625. if (overwrite || !p->hasProp(prop.str()))
  7626. {
  7627. // MORE - not sure this line should be needed....
  7628. p->setProp("Debug", "");
  7629. p->setProp(prop.str(), value);
  7630. }
  7631. }
  7632. void CLocalWorkUnit::setDebugValueInt(const char *propname, int value, bool overwrite)
  7633. {
  7634. StringBuffer lower;
  7635. lower.append(propname).toLowerCase();
  7636. CriticalBlock block(crit);
  7637. StringBuffer prop("Debug/");
  7638. prop.append(lower);
  7639. if (overwrite || !p->hasProp(prop.str()))
  7640. {
  7641. // MORE - not sure this line should be needed....
  7642. p->setProp("Debug", "");
  7643. p->setPropInt(prop.str(), value);
  7644. }
  7645. }
  7646. void CLocalWorkUnit::setTracingValue(const char *propname, const char *value)
  7647. {
  7648. CriticalBlock block(crit);
  7649. // MORE - not sure this line should be needed....
  7650. p->setProp("Tracing", "");
  7651. StringBuffer prop("Tracing/");
  7652. p->setProp(prop.append(propname).str(), value);
  7653. }
  7654. void CLocalWorkUnit::setTracingValueInt(const char *propname, int value)
  7655. {
  7656. CriticalBlock block(crit);
  7657. StringBuffer prop("Tracing/");
  7658. p->setPropInt(prop.append(propname).str(), value);
  7659. }
  7660. void CLocalWorkUnit::setTracingValueInt64(const char *propname, __int64 value)
  7661. {
  7662. CriticalBlock block(crit);
  7663. VStringBuffer prop("Tracing/%s", propname);
  7664. p->setPropInt64(prop.str(), value);
  7665. }
  7666. IConstWUQuery* CLocalWorkUnit::getQuery() const
  7667. {
  7668. // For this to be legally called, we must have the read-able interface. So we are already locked for (at least) read.
  7669. CriticalBlock block(crit);
  7670. if (!query)
  7671. {
  7672. IPropertyTree *s = queryMergedTree()->getPropTree("Query");
  7673. if (s)
  7674. query.setown(new CLocalWUQuery(s)); // NB takes ownership of 's'
  7675. }
  7676. return query.getLink();
  7677. }
  7678. IWUQuery* CLocalWorkUnit::updateQuery()
  7679. {
  7680. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  7681. CriticalBlock block(crit);
  7682. if (!query)
  7683. {
  7684. IPropertyTree *s = queryMergedTree()->queryPropTree("Query");
  7685. if (!s)
  7686. s = p->addPropTree("Query", createPTreeFromXMLString("<Query fetchEntire='1'/>")); // Is this really desirable (the fetchEntire) ?
  7687. s->Link();
  7688. query.setown(new CLocalWUQuery(s));
  7689. }
  7690. return query.getLink();
  7691. }
  7692. void CLocalWorkUnit::loadPlugins() const
  7693. {
  7694. CriticalBlock block(crit);
  7695. if (!pluginsCached)
  7696. {
  7697. assertex(plugins.length() == 0);
  7698. Owned<IPropertyTreeIterator> r = p->getElements("Plugins/Plugin");
  7699. for (r->first(); r->isValid(); r->next())
  7700. {
  7701. IPropertyTree *rp = &r->query();
  7702. rp->Link();
  7703. plugins.append(*new CLocalWUPlugin(rp));
  7704. }
  7705. pluginsCached = true;
  7706. }
  7707. }
  7708. IConstWUPluginIterator& CLocalWorkUnit::getPlugins() const
  7709. {
  7710. CriticalBlock block(crit);
  7711. loadPlugins();
  7712. return *new CArrayIteratorOf<IConstWUPlugin,IConstWUPluginIterator> (plugins, 0, (IConstWorkUnit *) this);
  7713. }
  7714. void CLocalWorkUnit::loadLibraries() const
  7715. {
  7716. CriticalBlock block(crit);
  7717. if (!librariesCached)
  7718. {
  7719. assertex(libraries.length() == 0);
  7720. Owned<IPropertyTreeIterator> r = p->getElements("Libraries/Library");
  7721. ForEach(*r)
  7722. {
  7723. IPropertyTree *rp = &r->query();
  7724. rp->Link();
  7725. libraries.append(*new CLocalWULibrary(rp));
  7726. }
  7727. librariesCached = true;
  7728. }
  7729. }
  7730. IConstWULibraryIterator& CLocalWorkUnit::getLibraries() const
  7731. {
  7732. CriticalBlock block(crit);
  7733. loadLibraries();
  7734. return *new CArrayIteratorOf<IConstWULibrary,IConstWULibraryIterator> (libraries, 0, (IConstWorkUnit *) this);
  7735. }
  7736. IConstWULibrary * CLocalWorkUnit::getLibraryByName(const char * search) const
  7737. {
  7738. CriticalBlock block(crit);
  7739. loadLibraries();
  7740. ForEachItemIn(idx, libraries)
  7741. {
  7742. SCMStringBuffer name;
  7743. IConstWULibrary &cur = libraries.item(idx);
  7744. cur.getName(name);
  7745. if (stricmp(name.str(), search)==0)
  7746. return &OLINK(cur);
  7747. }
  7748. return NULL;
  7749. }
  7750. StringBuffer &formatGraphTimerLabel(StringBuffer &str, const char *graphName, unsigned subGraphNum, unsigned __int64 subId)
  7751. {
  7752. str.append("Graph ").append(graphName);
  7753. if (subGraphNum) str.append(" - ").append(subGraphNum).append(" (").append(subId).append(")");
  7754. else if (subId) str.append(" - id(").append(subId).append(")");
  7755. return str;
  7756. }
  7757. StringBuffer &formatGraphTimerScope(StringBuffer &str, unsigned wfid, const char *graphName, unsigned subGraphNum, unsigned __int64 subId)
  7758. {
  7759. if (wfid)
  7760. str.append(WorkflowScopePrefix).append(wfid).append(":");
  7761. str.append(graphName);
  7762. if (subId) str.append(":sg").append(subId);
  7763. return str;
  7764. }
  7765. bool parseGraphTimerLabel(const char *label, StringAttr &graphName, unsigned & graphNum, unsigned &subGraphNum, unsigned &subId)
  7766. {
  7767. // expects format: "Graph <graphname>[ - <subgraphnum> (<subgraphid>)]"
  7768. unsigned len = (size32_t)strlen(label);
  7769. if (len < 6 || (0 != memcmp(label, "Graph ", 6)))
  7770. return false;
  7771. graphNum = 0;
  7772. subGraphNum = 0;
  7773. subId = 0;
  7774. const char *finger = label+6;
  7775. const char *finger2 = strchr(finger, '-');
  7776. if (NULL == finger2) // just graphName
  7777. graphName.set(finger);
  7778. else
  7779. {
  7780. graphName.set(finger, (size32_t)((finger2-1)-finger));
  7781. finger = finger2+2; // skip '-' and space
  7782. finger2 = strchr(finger, ' ');
  7783. if (finger2)
  7784. {
  7785. subGraphNum = atoi_l(finger, (size32_t)(finger2-finger));
  7786. finger = finger2+2; // skip space and '('
  7787. finger2 = strchr(finger, ')');
  7788. if (finger2)
  7789. subId = atoi_l(finger, (size32_t)(finger2-finger));
  7790. }
  7791. else if (((len-(finger-label))>3) && 0 == memcmp(finger, "id(", 3)) // subgraph id only, new format.
  7792. {
  7793. finger += 3;
  7794. finger2 = strchr(finger, ')');
  7795. if (finger2)
  7796. subId = atoi_l(finger, (size32_t)(finger2-finger));
  7797. }
  7798. }
  7799. if (graphName && !memicmp(graphName, "graph", 5))
  7800. graphNum = atoi(graphName + 5);
  7801. return true;
  7802. }
  7803. bool parseGraphScope(const char *scope, StringAttr &graphName, unsigned & graphNum, unsigned &subGraphId)
  7804. {
  7805. if (!MATCHES_CONST_PREFIX(scope, GraphScopePrefix))
  7806. return false;
  7807. graphNum = atoi(scope + strlen(GraphScopePrefix));
  7808. subGraphId = 0;
  7809. const char * colon = strchr(scope, ':');
  7810. if (!colon)
  7811. {
  7812. graphName.set(scope);
  7813. return true;
  7814. }
  7815. const char * subgraph = colon+1;
  7816. graphName.set(scope, (size32_t)(colon - scope));
  7817. if (MATCHES_CONST_PREFIX(subgraph, SubGraphScopePrefix))
  7818. subGraphId = atoi(subgraph+strlen(SubGraphScopePrefix));
  7819. return true;
  7820. }
  7821. void CLocalWorkUnit::setStatistic(StatisticCreatorType creatorType, const char * creator, StatisticScopeType scopeType, const char * scope, StatisticKind kind, const char * optDescription, unsigned __int64 value, unsigned __int64 count, unsigned __int64 maxValue, StatsMergeAction mergeAction)
  7822. {
  7823. if (!scope) scope = GLOBAL_SCOPE;
  7824. const char * kindName = queryStatisticName(kind);
  7825. StatisticMeasure measure = queryMeasure(kind);
  7826. //creator. scope and name must all be present, and must not contain semi colons.
  7827. assertex(creator && scope);
  7828. CriticalBlock block(crit);
  7829. IPropertyTree * stats = p->queryPropTree("Statistics");
  7830. if (!stats)
  7831. stats = p->addPropTree("Statistics");
  7832. IPropertyTree * statTree = NULL;
  7833. if (mergeAction != StatsMergeAppend)
  7834. {
  7835. StringBuffer xpath;
  7836. xpath.append("Statistic[@creator='").append(creator).append("'][@scope='").append(scope).append("'][@kind='").append(kindName).append("']");
  7837. statTree = stats->queryPropTree(xpath.str());
  7838. }
  7839. if (!statTree)
  7840. {
  7841. /* NB: Sasha archive uses this structure directly
  7842. * if it changes, the code in saarch.cpp needs updating
  7843. */
  7844. statTree = stats->addPropTree("Statistic");
  7845. statTree->setProp("@creator", creator);
  7846. statTree->setProp("@scope", scope);
  7847. statTree->setProp("@kind", kindName);
  7848. //These items are primarily here to facilitate filtering.
  7849. statTree->setProp("@unit", queryMeasureName(measure));
  7850. statTree->setProp("@c", queryCreatorTypeName(creatorType));
  7851. statTree->setProp("@s", queryScopeTypeName(scopeType));
  7852. statTree->setPropInt64("@ts", getTimeStampNowValue());
  7853. if (optDescription)
  7854. statTree->setProp("@desc", optDescription);
  7855. if (statistics.cached)
  7856. statistics.append(statTree); // links statTree
  7857. mergeAction = StatsMergeAppend;
  7858. }
  7859. if (mergeAction != StatsMergeAppend) // RKC->GH Is this right??
  7860. {
  7861. unsigned __int64 oldValue = statTree->getPropInt64("@value", 0);
  7862. unsigned __int64 oldCount = statTree->getPropInt64("@count", 0);
  7863. unsigned __int64 oldMax = statTree->getPropInt64("@max", 0);
  7864. if (oldMax < oldValue)
  7865. oldMax = oldValue;
  7866. statTree->setPropInt64("@value", mergeStatisticValue(oldValue, value, mergeAction));
  7867. statTree->setPropInt64("@count", count + oldCount);
  7868. if (maxValue > oldMax)
  7869. statTree->setPropInt64("@max", maxValue);
  7870. }
  7871. else
  7872. {
  7873. statTree->setPropInt64("@value", value);
  7874. statTree->setPropInt64("@count", count);
  7875. if (maxValue)
  7876. statTree->setPropInt64("@max", maxValue);
  7877. else
  7878. statTree->removeProp("@max");
  7879. }
  7880. //Whenever a graph time is updated recalculate the total time spent in thor, and save it
  7881. if ((scopeType == SSTgraph) && (kind == StTimeElapsed))
  7882. {
  7883. _loadStatistics();
  7884. stat_type totalTime = 0;
  7885. ForEachItemIn(i, statistics)
  7886. {
  7887. IConstWUStatistic & cur = statistics.item(i);
  7888. if ((cur.getScopeType() == SSTgraph) && (cur.getKind() == StTimeElapsed))
  7889. totalTime += cur.getValue();
  7890. }
  7891. StringBuffer t;
  7892. formatTimeCollatable(t, totalTime, false);
  7893. p->setProp("@totalThorTime", t);
  7894. }
  7895. if (scopeType == SSTglobal)
  7896. {
  7897. if (kind == StCostExecute)
  7898. p->setPropInt64("@costExecute", value);
  7899. else if (kind == StCostFileAccess)
  7900. p->setPropInt64("@costFileAccess", value);
  7901. }
  7902. if (kind == StCostCompile)
  7903. p->setPropInt64("@costCompile", value);
  7904. }
  7905. void CLocalWorkUnit::_loadStatistics() const
  7906. {
  7907. statistics.load(p,"Statistics/*");
  7908. }
  7909. bool CLocalWorkUnit::getStatistic(stat_type & value, const char * scope, StatisticKind kind) const
  7910. {
  7911. //MORE: Optimize this....
  7912. WuScopeFilter filter;
  7913. filter.addScope(scope).setIncludeNesting(0).addRequiredStat(kind).addOutputStatistic(kind).finishedFilter();
  7914. Owned<IConstWUScopeIterator> stats = &getScopeIterator(filter);
  7915. if (stats->first())
  7916. return stats->getStat(kind, value);
  7917. return false;
  7918. }
  7919. IConstWUScopeIterator & CLocalWorkUnit::getScopeIterator(const WuScopeFilter & filter) const
  7920. {
  7921. assertex(filter.isOptimized());
  7922. WuScopeSourceFlags sources = filter.querySources();
  7923. Owned<CompoundStatisticsScopeIterator> compoundIter = new CompoundStatisticsScopeIterator(filter);
  7924. if (sources & SSFsearchGlobalStats)
  7925. {
  7926. CriticalBlock block(crit);
  7927. statistics.loadBranch(p,"Statistics");
  7928. Owned<IConstWUScopeIterator> localStats(new WorkUnitStatisticsScopeIterator(statistics, filter.queryIterFilter()));
  7929. compoundIter->addIter(localStats);
  7930. }
  7931. if (sources & SSFsearchGraphStats)
  7932. {
  7933. const char * wuid = p->queryName();
  7934. Owned<IConstWUScopeIterator> scopeIter(new CConstGraphProgressScopeIterator(wuid, filter.queryIterFilter(), filter.queryMinVersion()));
  7935. compoundIter->addIter(scopeIter);
  7936. }
  7937. if (sources & SSFsearchGraph)
  7938. {
  7939. Owned<IConstWUScopeIterator> graphIter(new GraphScopeIterator(this, filter.queryIterFilter()));
  7940. compoundIter->addIter(graphIter);
  7941. }
  7942. if (sources & SSFsearchWorkflow)
  7943. {
  7944. Owned<IConstWorkflowItemIterator> iter = getWorkflowItems();
  7945. if (iter)
  7946. {
  7947. Owned<IConstWUScopeIterator> workflowIter(new WorkflowStatisticsScopeIterator(iter));
  7948. compoundIter->addIter(workflowIter);
  7949. }
  7950. }
  7951. if (sources & SSFsearchExceptions)
  7952. {
  7953. Owned<IConstWUScopeIterator> notesIter(new NotesIterator(this,filter.queryIterFilter()));
  7954. compoundIter->addIter(notesIter);
  7955. }
  7956. return *compoundIter.getClear();
  7957. }
  7958. IWUPlugin* CLocalWorkUnit::updatePluginByName(const char *qname)
  7959. {
  7960. CriticalBlock block(crit);
  7961. IConstWUPlugin *existing = getPluginByName(qname);
  7962. if (existing)
  7963. return (IWUPlugin *) existing;
  7964. if (!plugins.length())
  7965. p->addPropTree("Plugins");
  7966. IPropertyTree *pl = p->queryPropTree("Plugins");
  7967. IPropertyTree *s = pl->addPropTree("Plugin");
  7968. s->Link();
  7969. IWUPlugin* q = new CLocalWUPlugin(s);
  7970. q->Link();
  7971. plugins.append(*q);
  7972. q->setPluginName(qname);
  7973. return q;
  7974. }
  7975. IConstWUPlugin* CLocalWorkUnit::getPluginByName(const char *qname) const
  7976. {
  7977. CriticalBlock block(crit);
  7978. loadPlugins();
  7979. ForEachItemIn(idx, plugins)
  7980. {
  7981. SCMStringBuffer name;
  7982. IConstWUPlugin &cur = plugins.item(idx);
  7983. cur.getPluginName(name);
  7984. if (stricmp(name.str(), qname)==0)
  7985. {
  7986. cur.Link();
  7987. return &cur;
  7988. }
  7989. }
  7990. return NULL;
  7991. }
  7992. IWULibrary* CLocalWorkUnit::updateLibraryByName(const char *qname)
  7993. {
  7994. CriticalBlock block(crit);
  7995. IConstWULibrary *existing = getLibraryByName(qname);
  7996. if (existing)
  7997. return (IWULibrary *) existing;
  7998. if (!libraries.length())
  7999. p->addPropTree("Libraries");
  8000. IPropertyTree *pl = p->queryPropTree("Libraries");
  8001. IPropertyTree *s = pl->addPropTree("Library");
  8002. s->Link();
  8003. IWULibrary* q = new CLocalWULibrary(s);
  8004. q->Link();
  8005. libraries.append(*q);
  8006. q->setName(qname);
  8007. return q;
  8008. }
  8009. void CLocalWorkUnit::_loadExceptions() const
  8010. {
  8011. assertex(exceptions.length() == 0);
  8012. Owned<IPropertyTreeIterator> r = p->getElements("Exceptions/Exception");
  8013. for (r->first(); r->isValid(); r->next())
  8014. {
  8015. IPropertyTree *rp = &r->query();
  8016. rp->Link();
  8017. exceptions.append(*new CLocalWUException(rp));
  8018. }
  8019. }
  8020. void CLocalWorkUnit::loadExceptions() const
  8021. {
  8022. CriticalBlock block(crit);
  8023. if (!exceptionsCached)
  8024. {
  8025. _loadExceptions();
  8026. exceptionsCached = true;
  8027. }
  8028. }
  8029. IConstWUExceptionIterator& CLocalWorkUnit::getExceptions() const
  8030. {
  8031. CriticalBlock block(crit);
  8032. loadExceptions();
  8033. return *new CArrayIteratorOf<IConstWUException,IConstWUExceptionIterator> (exceptions, 0, (IConstWorkUnit *) this);
  8034. }
  8035. unsigned CLocalWorkUnit::getExceptionCount() const
  8036. {
  8037. CriticalBlock block(crit);
  8038. loadExceptions();
  8039. return exceptions.length();
  8040. }
  8041. void CLocalWorkUnit::clearExceptions(const char *source)
  8042. {
  8043. CriticalBlock block(crit);
  8044. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  8045. loadExceptions();
  8046. ForEachItemInRev(idx, exceptions)
  8047. {
  8048. IWUException &e = exceptions.item(idx);
  8049. SCMStringBuffer s;
  8050. e.getExceptionSource(s);
  8051. if (source)
  8052. {
  8053. if (!strieq(s.s, source))
  8054. continue;
  8055. }
  8056. else
  8057. {
  8058. if (strieq(s.s, "eclcc") || strieq(s.s, "eclccserver") || strieq(s.s, "eclserver") )
  8059. break;
  8060. }
  8061. VStringBuffer xpath("Exceptions/Exception[@sequence='%d']", e.getSequence());
  8062. p->removeProp(xpath);
  8063. exceptions.remove(idx);
  8064. }
  8065. if (exceptions.length() == 0)
  8066. p->removeProp("Exceptions");
  8067. }
  8068. IWUException* CLocalWorkUnit::createException()
  8069. {
  8070. CriticalBlock block(crit);
  8071. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  8072. loadExceptions();
  8073. if (!exceptions.length())
  8074. p->addPropTree("Exceptions");
  8075. IPropertyTree *r = p->queryPropTree("Exceptions");
  8076. IPropertyTree *s = r->addPropTree("Exception");
  8077. s->setPropInt("@sequence", exceptions.ordinality());
  8078. IWUException* q = new CLocalWUException(LINK(s));
  8079. exceptions.append(*LINK(q));
  8080. Owned<IJlibDateTime> now = createDateTimeNow();
  8081. SCMStringBuffer temp;
  8082. now->getString(temp);
  8083. q->setTimeStamp(temp.str());
  8084. return q;
  8085. }
  8086. IConstWUWebServicesInfo* CLocalWorkUnit::getWebServicesInfo() const
  8087. {
  8088. // For this to be legally called, we must have the read-able interface. So we are already locked for (at least) read.
  8089. CriticalBlock block(crit);
  8090. if (!webServicesInfoCached)
  8091. {
  8092. assertex(!webServicesInfo);
  8093. IPropertyTree *s = p->getPropTree("WebServicesInfo");
  8094. if (s)
  8095. webServicesInfo.setown(new CLocalWUWebServicesInfo(s)); // NB takes ownership of 's'
  8096. webServicesInfoCached = true;
  8097. }
  8098. return webServicesInfo.getLink();
  8099. }
  8100. IWUWebServicesInfo* CLocalWorkUnit::updateWebServicesInfo(bool create)
  8101. {
  8102. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  8103. CriticalBlock block(crit);
  8104. if (!webServicesInfoCached)
  8105. {
  8106. IPropertyTree *s = p->queryPropTree("WebServicesInfo");
  8107. if (!s)
  8108. {
  8109. if (create)
  8110. s = p->addPropTree("WebServicesInfo");
  8111. else
  8112. return NULL;
  8113. }
  8114. s->Link();
  8115. webServicesInfo.setown(new CLocalWUWebServicesInfo(s));
  8116. webServicesInfoCached = true;
  8117. }
  8118. return webServicesInfo.getLink();
  8119. }
  8120. static int compareResults(IInterface * const *ll, IInterface * const *rr)
  8121. {
  8122. CLocalWUResult *l = (CLocalWUResult *) *ll;
  8123. CLocalWUResult *r = (CLocalWUResult *) *rr;
  8124. return l->getResultSequence() - r->getResultSequence();
  8125. }
  8126. void CLocalWorkUnit::_loadResults() const
  8127. {
  8128. Owned<IPropertyTreeIterator> r = p->getElements("Results/Result");
  8129. for (r->first(); r->isValid(); r->next())
  8130. {
  8131. IPropertyTree *rp = &r->query();
  8132. rp->Link();
  8133. results.append(*new CLocalWUResult(rp));
  8134. }
  8135. }
  8136. void CLocalWorkUnit::loadResults() const
  8137. {
  8138. if (!resultsCached)
  8139. {
  8140. assertex(results.length() == 0);
  8141. _loadResults();
  8142. results.sort(compareResults);
  8143. resultsCached = true;
  8144. }
  8145. }
  8146. void CLocalWorkUnit::_loadVariables() const
  8147. {
  8148. Owned<IPropertyTreeIterator> r = p->getElements("Variables/Variable");
  8149. for (r->first(); r->isValid(); r->next())
  8150. {
  8151. IPropertyTree *rp = &r->query();
  8152. rp->Link();
  8153. variables.append(*new CLocalWUResult(rp));
  8154. }
  8155. }
  8156. void CLocalWorkUnit::loadVariables() const
  8157. {
  8158. if (!variablesCached)
  8159. {
  8160. assertex(variables.length() == 0);
  8161. _loadVariables();
  8162. variablesCached = true;
  8163. }
  8164. }
  8165. void CLocalWorkUnit::_loadTemporaries() const
  8166. {
  8167. Owned<IPropertyTreeIterator> r = p->getElements("Temporaries/Variable");
  8168. for (r->first(); r->isValid(); r->next())
  8169. {
  8170. IPropertyTree *rp = &r->query();
  8171. rp->Link();
  8172. temporaries.append(*new CLocalWUResult(rp));
  8173. }
  8174. }
  8175. void CLocalWorkUnit::loadTemporaries() const
  8176. {
  8177. if (!temporariesCached)
  8178. {
  8179. assertex(temporaries.length() == 0);
  8180. _loadTemporaries();
  8181. temporariesCached = true;
  8182. }
  8183. }
  8184. void CLocalWorkUnit::deleteTemporaries()
  8185. {
  8186. CriticalBlock block(crit);
  8187. if (temporariesCached)
  8188. {
  8189. temporaries.kill();
  8190. temporariesCached = false;
  8191. }
  8192. p->removeProp("Temporaries");
  8193. }
  8194. IWUResult* CLocalWorkUnit::createResult()
  8195. {
  8196. CriticalBlock block(crit);
  8197. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  8198. loadResults();
  8199. if (!results.length())
  8200. p->addPropTree("Results");
  8201. IPropertyTree *r = p->queryPropTree("Results");
  8202. IPropertyTree *s = r->addPropTree("Result");
  8203. s->Link();
  8204. IWUResult* q = new CLocalWUResult(s);
  8205. q->Link();
  8206. results.append(*q);
  8207. return q;
  8208. }
  8209. IWUResult* CLocalWorkUnit::updateResultByName(const char *qname)
  8210. {
  8211. CriticalBlock block(crit);
  8212. IConstWUResult *existing = getResultByName(qname);
  8213. if (existing)
  8214. return (IWUResult *) existing;
  8215. IWUResult* q = createResult();
  8216. q->setResultName(qname);
  8217. return q;
  8218. }
  8219. IWUResult* CLocalWorkUnit::updateResultBySequence(unsigned seq)
  8220. {
  8221. CriticalBlock block(crit);
  8222. IConstWUResult *existing = getResultBySequence(seq);
  8223. if (existing)
  8224. return (IWUResult *) existing;
  8225. IWUResult* q = createResult();
  8226. q->setResultSequence(seq);
  8227. return q;
  8228. }
  8229. IConstWUResultIterator& CLocalWorkUnit::getResults() const
  8230. {
  8231. CriticalBlock block(crit);
  8232. loadResults();
  8233. return *new CArrayIteratorOf<IConstWUResult,IConstWUResultIterator> (results, 0, (IConstWorkUnit *) this);
  8234. }
  8235. IConstWUResult* CLocalWorkUnit::getResultByName(const char *qname) const
  8236. {
  8237. CriticalBlock block(crit);
  8238. loadResults();
  8239. ForEachItemIn(idx, results)
  8240. {
  8241. SCMStringBuffer name;
  8242. IConstWUResult &cur = results.item(idx);
  8243. cur.getResultName(name);
  8244. if (stricmp(name.str(), qname)==0)
  8245. {
  8246. cur.Link();
  8247. return &cur;
  8248. }
  8249. }
  8250. return NULL;
  8251. }
  8252. IConstWUResult* CLocalWorkUnit::getQueryResultByName(const char *qname) const
  8253. {
  8254. CriticalBlock block(crit);
  8255. loadResults();
  8256. ForEachItemIn(idx, results)
  8257. {
  8258. IConstWUResult &cur = results.item(idx);
  8259. if (!isSpecialResultSequence(cur.getResultSequence()))
  8260. {
  8261. SCMStringBuffer name;
  8262. cur.getResultName(name);
  8263. if (stricmp(name.str(), qname)==0)
  8264. {
  8265. cur.Link();
  8266. return &cur;
  8267. }
  8268. }
  8269. }
  8270. return NULL;
  8271. }
  8272. IConstWUResult* CLocalWorkUnit::getResultBySequence(unsigned seq) const
  8273. {
  8274. CriticalBlock block(crit);
  8275. loadResults();
  8276. ForEachItemIn(idx, results)
  8277. {
  8278. IConstWUResult &cur = results.item(idx);
  8279. if (cur.getResultSequence() == seq)
  8280. {
  8281. cur.Link();
  8282. return &cur;
  8283. }
  8284. }
  8285. return NULL;
  8286. }
  8287. IConstWUResultIterator& CLocalWorkUnit::getVariables() const
  8288. {
  8289. CriticalBlock block(crit);
  8290. loadVariables();
  8291. return *new CArrayIteratorOf<IConstWUResult,IConstWUResultIterator> (variables, 0, (IConstWorkUnit *) this);
  8292. }
  8293. IConstWUResult* CLocalWorkUnit::getGlobalByName(const char *qname) const
  8294. {
  8295. CriticalBlock block(crit);
  8296. if (strcmp(p->queryName(), GLOBAL_WORKUNIT)==0)
  8297. return getVariableByName(qname);
  8298. Owned <IWorkUnit> global = globalFactory->getGlobalWorkUnit(secMgr, secUser);
  8299. return global->getVariableByName(qname);
  8300. }
  8301. IWUResult* CLocalWorkUnit::updateGlobalByName(const char *qname)
  8302. {
  8303. CriticalBlock block(crit);
  8304. if (strcmp(p->queryName(), GLOBAL_WORKUNIT)==0)
  8305. return updateVariableByName(qname);
  8306. Owned <IWorkUnit> global = globalFactory->getGlobalWorkUnit(secMgr, secUser);
  8307. return global->updateVariableByName(qname);
  8308. }
  8309. IConstWUResult* CLocalWorkUnit::getVariableByName(const char *qname) const
  8310. {
  8311. CriticalBlock block(crit);
  8312. loadVariables();
  8313. ForEachItemIn(idx, variables)
  8314. {
  8315. SCMStringBuffer name;
  8316. IConstWUResult &cur = variables.item(idx);
  8317. cur.getResultName(name);
  8318. if (stricmp(name.str(), qname)==0)
  8319. {
  8320. cur.Link();
  8321. return &cur;
  8322. }
  8323. }
  8324. return NULL;
  8325. }
  8326. IConstWUResult* CLocalWorkUnit::getTemporaryByName(const char *qname) const
  8327. {
  8328. CriticalBlock block(crit);
  8329. loadTemporaries();
  8330. ForEachItemIn(idx, temporaries)
  8331. {
  8332. SCMStringBuffer name;
  8333. IConstWUResult &cur = temporaries.item(idx);
  8334. cur.getResultName(name);
  8335. if (stricmp(name.str(), qname)==0)
  8336. {
  8337. cur.Link();
  8338. return &cur;
  8339. }
  8340. }
  8341. return NULL;
  8342. }
  8343. IConstWUResultIterator& CLocalWorkUnit::getTemporaries() const
  8344. {
  8345. CriticalBlock block(crit);
  8346. loadTemporaries();
  8347. return *new CArrayIteratorOf<IConstWUResult,IConstWUResultIterator> (temporaries, 0, (IConstWorkUnit *) this);
  8348. }
  8349. IWUResult* CLocalWorkUnit::updateTemporaryByName(const char *qname)
  8350. {
  8351. CriticalBlock block(crit);
  8352. IConstWUResult *existing = getTemporaryByName(qname);
  8353. if (existing)
  8354. return (IWUResult *) existing;
  8355. IPropertyTree *vars = (temporaries.length()) ? p->queryPropTree("Temporaries") : p->addPropTree("Temporaries");
  8356. IPropertyTree *s = vars->addPropTree("Variable");
  8357. s->Link();
  8358. IWUResult* q = new CLocalWUResult(s);
  8359. q->Link();
  8360. temporaries.append(*q);
  8361. q->setResultName(qname);
  8362. q->setResultSequence(ResultSequenceInternal);
  8363. return q;
  8364. }
  8365. IWUResult* CLocalWorkUnit::updateVariableByName(const char *qname)
  8366. {
  8367. CriticalBlock block(crit);
  8368. IConstWUResult *existing = getVariableByName(qname);
  8369. if (existing)
  8370. return (IWUResult *) existing;
  8371. if (!variables.length())
  8372. p->addPropTree("Variables");
  8373. IPropertyTree *vars = p->queryPropTree("Variables");
  8374. IPropertyTree *s = vars->addPropTree("Variable");
  8375. s->Link();
  8376. IWUResult* q = new CLocalWUResult(s);
  8377. q->Link();
  8378. variables.append(*q);
  8379. q->setResultName(qname);
  8380. q->setResultSequence(ResultSequenceStored);
  8381. return q;
  8382. }
  8383. void CLocalWorkUnit::deleteTempFiles(const char *graph, bool deleteOwned, bool deleteJobOwned)
  8384. {
  8385. CriticalBlock block(crit);
  8386. IPropertyTree *files = p->queryPropTree("Files");
  8387. if (!files) return;
  8388. Owned<IPropertyTreeIterator> iter = files->getElements("File");
  8389. ICopyArrayOf<IPropertyTree> toRemove;
  8390. ForEach (*iter)
  8391. {
  8392. IPropertyTree &file = iter->query();
  8393. WUFileKind fileKind = (WUFileKind) file.getPropInt("@kind", WUFileStandard);
  8394. if(file.getPropBool("@temporary")) fileKind = WUFileTemporary; // @temporary, legacy check
  8395. bool needDelete;
  8396. switch(fileKind)
  8397. {
  8398. case WUFileTemporary:
  8399. if(graph==NULL)
  8400. needDelete = true;
  8401. else
  8402. {
  8403. const char *graphOwner = file.queryProp("@graph");
  8404. needDelete = ((graphOwner==NULL) || (strcmp(graph, graphOwner)==0));
  8405. }
  8406. break;
  8407. case WUFileJobOwned:
  8408. needDelete = ((graph==NULL) && deleteJobOwned);
  8409. break;
  8410. case WUFileOwned:
  8411. needDelete = ((graph==NULL) && deleteOwned);
  8412. break;
  8413. default:
  8414. needDelete = false;
  8415. }
  8416. if(needDelete)
  8417. {
  8418. const char *name = file.queryProp("@name");
  8419. LOG(MCdebugProgress, unknownJob, "Removing workunit file %s from DFS", name);
  8420. queryDistributedFileDirectory().removeEntry(name, queryUserDescriptor());
  8421. toRemove.append(file);
  8422. }
  8423. }
  8424. ForEachItemIn(r, toRemove) files->removeTree(&toRemove.item(r));
  8425. }
  8426. static void _noteFileRead(IDistributedFile *file, IPropertyTree *filesRead)
  8427. {
  8428. IDistributedSuperFile *super = file->querySuperFile();
  8429. StringBuffer fname;
  8430. file->getLogicalName(fname);
  8431. if (fname.length())
  8432. {
  8433. StringBuffer path("File[@name=\"");
  8434. path.append(fname).append("\"]");
  8435. IPropertyTree *fileTree = filesRead->queryPropTree(path.str());
  8436. if (fileTree)
  8437. fileTree->setPropInt("@useCount", fileTree->getPropInt("@useCount")+1);
  8438. else
  8439. {
  8440. StringBuffer cluster;
  8441. file->getClusterName(0,cluster);
  8442. fileTree = createPTree();
  8443. fileTree->setProp("@name", fname.str());
  8444. fileTree->setProp("@cluster", cluster.str());
  8445. fileTree->setPropInt("@useCount", 1);
  8446. fileTree = filesRead->addPropTree("File", fileTree);
  8447. }
  8448. if (super)
  8449. {
  8450. fileTree->setPropBool("@super", true);
  8451. Owned<IDistributedFileIterator> iter = super->getSubFileIterator(false);
  8452. ForEach (*iter)
  8453. {
  8454. IDistributedFile &file = iter->query();
  8455. StringBuffer fname;
  8456. file.getLogicalName(fname);
  8457. fileTree->addPropTree("Subfile")->setProp("@name", fname.str());
  8458. _noteFileRead(&file, filesRead);
  8459. }
  8460. }
  8461. }
  8462. }
  8463. void CLocalWorkUnit::_loadFilesRead() const
  8464. {
  8465. // Nothing to do
  8466. }
  8467. void CLocalWorkUnit::noteFileRead(IDistributedFile *file)
  8468. {
  8469. if (file)
  8470. {
  8471. CriticalBlock block(crit);
  8472. IPropertyTree *files = p->queryPropTree("FilesRead");
  8473. if (!files)
  8474. files = p->addPropTree("FilesRead");
  8475. _noteFileRead(file, files);
  8476. }
  8477. }
  8478. void CLocalWorkUnit::noteFieldUsage(IPropertyTree * fieldUsage)
  8479. {
  8480. if (fieldUsage)
  8481. {
  8482. CriticalBlock block(crit);
  8483. p->addPropTree("usedsources", fieldUsage);
  8484. }
  8485. }
  8486. void CLocalWorkUnit::_loadFilesWritten() const
  8487. {
  8488. // Nothing to do
  8489. }
  8490. static void addFile(IPropertyTree *files, const char *fileName, const char *cluster, unsigned usageCount, WUFileKind fileKind, const char *graphOwner)
  8491. {
  8492. StringBuffer path("File[@name=\"");
  8493. path.append(fileName).append("\"]");
  8494. if (cluster)
  8495. path.append("[@cluster=\"").append(cluster).append("\"]");
  8496. IPropertyTree *file = files->queryPropTree(path.str());
  8497. if (file) files->removeTree(file);
  8498. file = createPTree();
  8499. file->setProp("@name", fileName);
  8500. if (cluster)
  8501. file->setProp("@cluster", cluster);
  8502. if (graphOwner)
  8503. file->setProp("@graph", graphOwner);
  8504. file->setPropInt("@kind", (unsigned)fileKind);
  8505. if (WUFileTemporary == fileKind)
  8506. file->setPropInt("@usageCount", usageCount);
  8507. files->addPropTree("File", file);
  8508. }
  8509. void CLocalWorkUnit::addFile(const char *fileName, StringArray *clusters, unsigned usageCount, WUFileKind fileKind, const char *graphOwner)
  8510. {
  8511. CriticalBlock block(crit);
  8512. IPropertyTree *files = p->queryPropTree("Files");
  8513. if (!files)
  8514. files = p->addPropTree("Files");
  8515. if (!clusters)
  8516. ::addFile(files, fileName, NULL, usageCount, fileKind, graphOwner);
  8517. else
  8518. {
  8519. ForEachItemIn(c, *clusters)
  8520. ::addFile(files, fileName, clusters->item(c), usageCount, fileKind, graphOwner);
  8521. }
  8522. }
  8523. void CLocalWorkUnit::releaseFile(const char *fileName)
  8524. {
  8525. StringBuffer path("File[@name=\"");
  8526. path.append(fileName).append("\"]");
  8527. CriticalBlock block(crit);
  8528. IPropertyTree *files = p->queryPropTree("Files");
  8529. if (!files) return;
  8530. Owned<IPropertyTreeIterator> fiter = files->getElements(path.str());
  8531. if (fiter->first())
  8532. {
  8533. while (true)
  8534. {
  8535. IPropertyTree *file = &fiter->query();
  8536. unsigned usageCount = file->getPropInt("@usageCount");
  8537. bool more = fiter->next();
  8538. if (usageCount > 1)
  8539. file->setPropInt("@usageCount", usageCount-1);
  8540. else
  8541. {
  8542. StringAttr name(file->queryProp("@name"));
  8543. files->removeTree(file);
  8544. if (!name.isEmpty()&&(1 == usageCount))
  8545. {
  8546. if (queryDistributedFileDirectory().removeEntry(fileName, queryUserDescriptor()))
  8547. LOG(MCdebugProgress, unknownJob, "Removed (released) file %s from DFS", name.get());
  8548. }
  8549. }
  8550. if (!more)
  8551. break;
  8552. }
  8553. }
  8554. }
  8555. void CLocalWorkUnit::clearGraphProgress() const
  8556. {
  8557. }
  8558. void CLocalWorkUnit::resetBeforeGeneration()
  8559. {
  8560. CriticalBlock block(crit);
  8561. //Remove all associated files
  8562. Owned<IWUQuery> q = updateQuery();
  8563. q->removeAssociatedFiles();
  8564. //Remove any pre-existing workflow information
  8565. workflowIterator.clear();
  8566. p->removeProp("Workflow");
  8567. }
  8568. unsigned CLocalWorkUnit::queryFileUsage(const char *fileName) const
  8569. {
  8570. StringBuffer path("Files/File[@name=\"");
  8571. path.append(fileName).append("\"]/@usageCount");
  8572. CriticalBlock block(crit);
  8573. return p->getPropInt(path.str());
  8574. }
  8575. IConstWUFileUsageIterator * CLocalWorkUnit::getFieldUsage() const
  8576. {
  8577. CriticalBlock block(crit);
  8578. IPropertyTree* fieldUsageTree = p->queryPropTree("usedsources");
  8579. if (!fieldUsageTree)
  8580. return NULL;
  8581. IPropertyTreeIterator* iter = fieldUsageTree->getElements("*");
  8582. return new CConstWUFileUsageIterator(iter);
  8583. }
  8584. bool isFilenameResolved(StringBuffer& filename)
  8585. {
  8586. size32_t length = filename.length();
  8587. // With current implementation, if filename is surrounded by single quotes, it means that the filename was resolved at compile time.
  8588. if (filename.length() >= 2 && filename.charAt(0) == '\'' && filename.charAt(length-1) == '\'')
  8589. return true;
  8590. else
  8591. return false;
  8592. }
  8593. bool CLocalWorkUnit::getFieldUsageArray(StringArray & filenames, StringArray & columnnames, const char * clusterName) const
  8594. {
  8595. bool scopeLoaded = false;
  8596. StringBuffer defaultScope;
  8597. Owned<IConstWUFileUsageIterator> files = getFieldUsage();
  8598. if (!files)
  8599. return false; // this query was not compiled with recordFieldUsage option.
  8600. ForEach(*files)
  8601. {
  8602. Owned<IConstWUFileUsage> file = files->get();
  8603. StringBuffer filename(file->queryName());
  8604. size32_t length = filename.length();
  8605. if (length == 0)
  8606. throw MakeStringException(WUERR_InvalidFieldUsage, "Invalid FieldUsage found in WU. Cannot enforce view security.");
  8607. StringBuffer normalizedFilename;
  8608. // Two cases to handle:
  8609. // 1. Filename was known at compile time, and is surrounded in single quotes (i.e. 'filename').
  8610. // 2. Filename could not be resolved at compile time (i.e. filename is an input to a query),
  8611. // and is a raw expression WITHOUT surrounding single quotes (i.e. STORED('input_filename')).
  8612. if (isFilenameResolved(filename))
  8613. {
  8614. // filename cannot be empty (i.e. empty single quotes '')
  8615. if (length == 2)
  8616. throw MakeStringException(WUERR_InvalidFieldUsage, "Invalid FieldUsage found in WU. Cannot enforce view security.");
  8617. // Remove surrounding single quotes
  8618. StringAttr cleanFilename(filename.str()+1, length-2);
  8619. // When a filename doesn't start with a tilde (~), it means scope is omitted and is relying on a default scope.
  8620. // We need to load a default scope from config and prefix the filename with it.
  8621. if (cleanFilename.str()[0] != '~')
  8622. {
  8623. // loading a default scope from config is expensive, and should be only done once and be reused later.
  8624. if (!scopeLoaded)
  8625. {
  8626. //MORE: This should actually depend on the cluster that was active when the file was read!
  8627. if (!resolveFilePrefix(defaultScope, clusterName))
  8628. throw MakeStringException(WUERR_InvalidCluster, "Unknown cluster %s", clusterName);
  8629. scopeLoaded = true;
  8630. }
  8631. normalizedFilename.append(defaultScope.str());
  8632. normalizedFilename.append(cleanFilename.str());
  8633. }
  8634. else
  8635. {
  8636. normalizedFilename.append(cleanFilename);
  8637. }
  8638. }
  8639. else
  8640. {
  8641. // When filename is an unresolved expression, simply treat the expression as a "non-existent" filename.
  8642. // It will have an effect of this query accessing a non-existent filename, and will be denied access unconditionally.
  8643. normalizedFilename.append(filename.str());
  8644. }
  8645. Owned<IConstWUFieldUsageIterator> fields = file->getFields();
  8646. ForEach(*fields)
  8647. {
  8648. Owned<IConstWUFieldUsage> field = fields->get();
  8649. filenames.append(normalizedFilename.str());
  8650. columnnames.append(field->queryName());
  8651. }
  8652. }
  8653. return true;
  8654. }
  8655. IPropertyTreeIterator & CLocalWorkUnit::getFileIterator() const
  8656. {
  8657. CriticalBlock block(crit);
  8658. _loadFilesWritten();
  8659. return * p->getElements("Files/File");
  8660. }
  8661. IPropertyTreeIterator & CLocalWorkUnit::getFilesReadIterator() const
  8662. {
  8663. CriticalBlock block(crit);
  8664. _loadFilesRead();
  8665. return * p->getElements("FilesRead/File");
  8666. }
  8667. //=================================================================================================
  8668. bool CLocalWorkUnit::switchThorQueue(const char *cluster, IQueueSwitcher *qs)
  8669. {
  8670. CriticalBlock block(crit);
  8671. if (qs->isAuto()&&!getAllowAutoQueueSwitch())
  8672. return false;
  8673. const char * currentcluster = queryClusterName();
  8674. const char *wuid = p->queryName();
  8675. StringBuffer curqname;
  8676. getClusterThorQueueName(curqname, currentcluster);
  8677. void *qi = qs->getQ(curqname.str(),wuid);
  8678. if (!qi)
  8679. return false;
  8680. setClusterName(cluster);
  8681. StringBuffer newqname;
  8682. getClusterThorQueueName(newqname, cluster);
  8683. qs->putQ(newqname.str(),wuid,qi);
  8684. return true;
  8685. }
  8686. //=================================================================================================
  8687. IPropertyTree *CLocalWorkUnit::getUnpackedTree(bool includeProgress) const
  8688. {
  8689. Owned<IPropertyTree> ret = createPTreeFromIPT(queryMergedTree());
  8690. Owned<IConstWUGraphIterator> graphIter = &getGraphs(GraphTypeAny);
  8691. ForEach(*graphIter)
  8692. {
  8693. IConstWUGraph &graph = graphIter->query();
  8694. Owned<IPropertyTree> graphTree = graph.getXGMMLTree(includeProgress, false);
  8695. SCMStringBuffer gName;
  8696. graph.getName(gName);
  8697. StringBuffer xpath("Graphs/Graph[@name=\"");
  8698. xpath.append(gName.s).append("\"]/xgmml");
  8699. IPropertyTree *xgmml = ret->queryPropTree(xpath.str());
  8700. if (xgmml) // don't know of any reason it shouldn't exist
  8701. {
  8702. xgmml->removeProp("graphBin");
  8703. xgmml->setPropTree("graph", graphTree.getClear());
  8704. }
  8705. }
  8706. return ret.getClear();
  8707. }
  8708. IPropertyTree *CLocalWorkUnit::queryMergedTree() const
  8709. {
  8710. if (indirectTree)
  8711. return indirectTree;
  8712. if (!p->hasProp("@clonedFromWorkunit"))
  8713. return p;
  8714. else
  8715. {
  8716. indirectTree.setown(createPTreeFromIPT(p));
  8717. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  8718. Owned<IConstWorkUnit> donor = factory->openWorkUnit(p->queryProp("@clonedFromWorkunit"));
  8719. if (donor)
  8720. {
  8721. copyTree(indirectTree, queryExtendedWU(donor)->queryPTree(), "Graphs");
  8722. copyTree(indirectTree, queryExtendedWU(donor)->queryPTree(), "Query");
  8723. }
  8724. return indirectTree;
  8725. }
  8726. }
  8727. void CLocalWorkUnit::_loadGraphs(bool heavy) const
  8728. {
  8729. Owned<IPropertyTreeIterator> iter = queryMergedTree()->getElements("Graphs/Graph");
  8730. ForEach(*iter)
  8731. {
  8732. IPropertyTree &graph = iter->query();
  8733. graphs.append(*new CLocalWUGraph(*this, LINK(&graph)));
  8734. }
  8735. }
  8736. void CLocalWorkUnit::loadGraphs(bool heavy) const
  8737. {
  8738. if (graphsCached < (heavy ? 2 : 1))
  8739. {
  8740. graphs.kill();
  8741. _loadGraphs(heavy);
  8742. graphsCached = (heavy ? 2 : 1);
  8743. }
  8744. }
  8745. EnumMapping graphTypes[] = {
  8746. { GraphTypeAny, "unknown" },
  8747. { GraphTypeProgress, "progress" },
  8748. { GraphTypeEcl, "ECL" },
  8749. { GraphTypeActivities, "activities" },
  8750. { GraphTypeSubProgress, "subgraph" },
  8751. { GraphTypeSize, NULL },
  8752. };
  8753. WUGraphType getGraphTypeFromString(const char* type)
  8754. {
  8755. return (WUGraphType) getEnum(type, graphTypes);
  8756. }
  8757. CLocalWUGraph::CLocalWUGraph(const CLocalWorkUnit &_owner, IPropertyTree *props) : p(props), owner(_owner)
  8758. {
  8759. wuidVersion = owner.getWuidVersion();
  8760. }
  8761. IStringVal& CLocalWUGraph::getName(IStringVal &str) const
  8762. {
  8763. str.set(p->queryProp("@name"));
  8764. return str;
  8765. }
  8766. IStringVal& CLocalWUGraph::getLabel(IStringVal &str) const
  8767. {
  8768. if (wuidVersion >= 2)
  8769. {
  8770. str.set(p->queryProp("@label"));
  8771. return str;
  8772. }
  8773. else
  8774. {
  8775. Owned<IPropertyTree> xgmml = getXGMMLTree(false, false);
  8776. str.set(xgmml->queryProp("@label"));
  8777. return str;
  8778. }
  8779. }
  8780. WUGraphState CLocalWUGraph::getState() const
  8781. {
  8782. return owner.queryGraphState(p->queryProp("@name"));
  8783. }
  8784. IStringVal& CLocalWUGraph::getXGMML(IStringVal &str, bool mergeProgress, bool doFormatStats) const
  8785. {
  8786. Owned<IPropertyTree> xgmml = getXGMMLTree(mergeProgress, doFormatStats);
  8787. if (xgmml)
  8788. {
  8789. StringBuffer x;
  8790. toXML(xgmml, x);
  8791. str.set(x.str());
  8792. }
  8793. return str;
  8794. }
  8795. unsigned CLocalWorkUnit::getGraphCount() const
  8796. {
  8797. CriticalBlock block(crit);
  8798. if (queryMergedTree()->hasProp("Graphs"))
  8799. {
  8800. return queryMergedTree()->queryPropTree("Graphs")->numChildren();
  8801. }
  8802. return 0;
  8803. }
  8804. unsigned CLocalWorkUnit::getSourceFileCount() const
  8805. {
  8806. CriticalBlock block(crit);
  8807. _loadFilesRead();
  8808. if (p->hasProp("FilesRead"))
  8809. {
  8810. return p->queryPropTree("FilesRead")->numChildren();
  8811. }
  8812. return 0;
  8813. }
  8814. unsigned CLocalWorkUnit::getResultCount() const
  8815. {
  8816. CriticalBlock block(crit);
  8817. if (p->hasProp("Results"))
  8818. {
  8819. return p->queryPropTree("Results")->numChildren();
  8820. }
  8821. return 0;
  8822. }
  8823. unsigned CLocalWorkUnit::getVariableCount() const
  8824. {
  8825. CriticalBlock block(crit);
  8826. if (p->hasProp("Variables"))
  8827. {
  8828. return p->queryPropTree("Variables")->numChildren();
  8829. }
  8830. return 0;
  8831. }
  8832. unsigned CLocalWorkUnit::getApplicationValueCount() const
  8833. {
  8834. CriticalBlock block(crit);
  8835. if (p->hasProp("Application"))
  8836. {
  8837. return p->queryPropTree("Application")->numChildren();
  8838. }
  8839. return 0;
  8840. }
  8841. StringBuffer &appendPTreeOpenTag(StringBuffer &s, IPropertyTree *tree, const char *name, unsigned indent, bool hidePasswords)
  8842. {
  8843. appendXMLOpenTag(s, name, NULL, false);
  8844. Owned<IAttributeIterator> attrs = tree->getAttributes(true);
  8845. if (attrs->first())
  8846. {
  8847. unsigned attributeindent = indent + (size32_t) strlen(name);
  8848. unsigned count = attrs->count();
  8849. bool doindent = false;
  8850. ForEach(*attrs)
  8851. {
  8852. if (hidePasswords && streq(attrs->queryName(), "@token"))
  8853. continue;
  8854. #ifndef _DEBUG
  8855. if (hidePasswords && streq(attrs->queryName(), "@distributedAccessToken"))
  8856. continue;
  8857. #endif
  8858. if (doindent)
  8859. s.append('\n').appendN(attributeindent, ' ');
  8860. else if (count > 3)
  8861. doindent = true;
  8862. appendXMLAttr(s, attrs->queryName()+1, attrs->queryValue());
  8863. }
  8864. }
  8865. s.append('>');
  8866. return s;
  8867. }
  8868. IStringVal &CLocalWorkUnit::getXmlParams(IStringVal &str, bool hidePasswords) const
  8869. {
  8870. CriticalBlock block(crit);
  8871. IPropertyTree *paramTree = p->queryPropTree("Parameters");
  8872. if (!paramTree)
  8873. return str;
  8874. StringBuffer xml;
  8875. if (!hidePasswords)
  8876. toXML(paramTree, xml);
  8877. else
  8878. {
  8879. appendPTreeOpenTag(xml.append(' '), paramTree, "Parameters", 0, false).append('\n');
  8880. Owned<IPropertyTreeIterator> elems = paramTree->getElements("*");
  8881. ForEach(*elems)
  8882. {
  8883. const char *paramname = elems->query().queryName();
  8884. VStringBuffer xpath("Variables/Variable[@name='%s']/Format/@password", paramname);
  8885. if (p->getPropBool(xpath))
  8886. appendXMLTag(xml.append(" "), paramname, "***").append('\n');
  8887. else
  8888. toXML(&elems->query(), xml, 2);
  8889. }
  8890. appendXMLCloseTag(xml.append(' '), "Parameters").append('\n');
  8891. }
  8892. str.set(xml);
  8893. return str;
  8894. }
  8895. const IPropertyTree *CLocalWorkUnit::getXmlParams() const
  8896. {
  8897. CriticalBlock block(crit);
  8898. return p->getPropTree("Parameters");
  8899. }
  8900. void CLocalWorkUnit::setXmlParams(const char *params)
  8901. {
  8902. CriticalBlock block(crit);
  8903. p->setPropTree("Parameters", createPTreeFromXMLString(params));
  8904. }
  8905. void CLocalWorkUnit::setXmlParams(IPropertyTree *tree)
  8906. {
  8907. CriticalBlock block(crit);
  8908. p->setPropTree("Parameters", tree);
  8909. }
  8910. unsigned __int64 CLocalWorkUnit::getHash() const
  8911. {
  8912. CriticalBlock block(crit);
  8913. return p->getPropInt64("@hash");
  8914. }
  8915. void CLocalWorkUnit::setHash(unsigned __int64 hash)
  8916. {
  8917. CriticalBlock block(crit);
  8918. p->setPropInt64("@hash", hash);
  8919. }
  8920. // getGraphs / getGraphsMeta
  8921. // These are basically the same except for the amount of preloading they do, and the type of the iterator they return...
  8922. // If a type other than any is requested, a postfilter is needed.
  8923. template <class T, class U> class CFilteredGraphIteratorOf : public CInterfaceOf<T>
  8924. {
  8925. WUGraphType type;
  8926. Owned<T> base;
  8927. bool match()
  8928. {
  8929. return base->query().getType()==type;
  8930. }
  8931. public:
  8932. CFilteredGraphIteratorOf<T,U>(T *_base, WUGraphType _type)
  8933. : base(_base), type(_type)
  8934. {
  8935. }
  8936. bool first()
  8937. {
  8938. if (!base->first())
  8939. return false;
  8940. if (match())
  8941. return true;
  8942. return next();
  8943. }
  8944. bool next()
  8945. {
  8946. while (base->next())
  8947. if (match())
  8948. return true;
  8949. return false;
  8950. }
  8951. virtual bool isValid()
  8952. {
  8953. return base->isValid();
  8954. }
  8955. U & query()
  8956. {
  8957. return base->query();
  8958. }
  8959. };
  8960. IConstWUGraphMetaIterator& CLocalWorkUnit::getGraphsMeta(WUGraphType type) const
  8961. {
  8962. /* NB: this method should be 'cheap', loadGraphs() creates IConstWUGraph interfaces to the graphs
  8963. * it does not actually pull the graph data. We only use IConstWUGraphMeta here, which never probes the xgmml.
  8964. */
  8965. CriticalBlock block(crit);
  8966. loadGraphs(false);
  8967. IConstWUGraphMetaIterator *giter = new CArrayIteratorOf<IConstWUGraph,IConstWUGraphMetaIterator> (graphs, 0, (IConstWorkUnit *) this);
  8968. if (type!=GraphTypeAny)
  8969. giter = new CFilteredGraphIteratorOf<IConstWUGraphMetaIterator, IConstWUGraphMeta>(giter,type);
  8970. return *giter;
  8971. }
  8972. IConstWUGraphIterator& CLocalWorkUnit::getGraphs(WUGraphType type) const
  8973. {
  8974. CriticalBlock block(crit);
  8975. loadGraphs(true);
  8976. IConstWUGraphIterator *giter = new CArrayIteratorOf<IConstWUGraph,IConstWUGraphIterator> (graphs, 0, (IConstWorkUnit *) this);
  8977. if (type!=GraphTypeAny)
  8978. giter = new CFilteredGraphIteratorOf<IConstWUGraphIterator, IConstWUGraph>(giter, type);
  8979. return *giter;
  8980. }
  8981. IConstWUGraph* CLocalWorkUnit::getGraph(const char *qname) const
  8982. {
  8983. CriticalBlock block(crit);
  8984. VStringBuffer xpath("Graphs/Graph[@name='%s']", qname);
  8985. // NOTE - this would go wrong if we had other graphs of same name but different type. Ignore for now.
  8986. IPTree *graph = queryMergedTree()->queryPropTree(xpath);
  8987. if (graph)
  8988. return new CLocalWUGraph(*this, LINK(graph));
  8989. return NULL;
  8990. }
  8991. void CLocalWorkUnit::createGraph(const char * name, const char *label, WUGraphType type, IPropertyTree *xgmml, unsigned wfid)
  8992. {
  8993. CriticalBlock block(crit);
  8994. if (!graphs.length())
  8995. p->addPropTree("Graphs");
  8996. IPropertyTree *r = p->queryPropTree("Graphs");
  8997. IPropertyTree *s = r->addPropTree("Graph");
  8998. CLocalWUGraph *q = new CLocalWUGraph(*this, LINK(s));
  8999. q->setName(name);
  9000. q->setLabel(label);
  9001. q->setType(type);
  9002. q->setWfid(wfid);
  9003. q->setXGMMLTree(xgmml);
  9004. graphs.append(*q);
  9005. }
  9006. IConstWUGraphProgress *CLocalWorkUnit::getGraphProgress(const char *graphName) const
  9007. {
  9008. IPTree *graphProgress = p->queryPropTree("GraphProgress");
  9009. if (graphProgress)
  9010. {
  9011. IPTree *progress = graphProgress->queryPropTree(graphName);
  9012. if (progress)
  9013. return new CConstGraphProgress(queryWuid(), graphName, progress);
  9014. }
  9015. return nullptr;
  9016. }
  9017. WUGraphState CLocalWorkUnit::queryGraphState(const char *graphName) const
  9018. {
  9019. throwUnexpected(); // Should only be used for persisted workunits
  9020. }
  9021. WUGraphState CLocalWorkUnit::queryNodeState(const char *graphName, WUGraphIDType nodeId) const
  9022. {
  9023. throwUnexpected(); // Should only be used for persisted workunits
  9024. }
  9025. void CLocalWorkUnit::setGraphState(const char *graphName, unsigned wfid, WUGraphState state) const
  9026. {
  9027. throwUnexpected(); // Should only be used for persisted workunits
  9028. }
  9029. void CLocalWorkUnit::setNodeState(const char *graphName, WUGraphIDType nodeId, WUGraphState state) const
  9030. {
  9031. throwUnexpected(); // Should only be used for persisted workunits
  9032. }
  9033. IWUGraphStats *CLocalWorkUnit::updateStats(const char *graphName, StatisticCreatorType creatorType, const char * creator, unsigned _wfid, unsigned subgraph, bool merge) const
  9034. {
  9035. return new CLocalWuGraphStats(LINK(p), creatorType, creator, _wfid, graphName, subgraph, merge);
  9036. }
  9037. void CLocalWUGraph::setName(const char *str)
  9038. {
  9039. p->setProp("@name", str);
  9040. }
  9041. void CLocalWUGraph::setLabel(const char *str)
  9042. {
  9043. p->setProp("@label", str);
  9044. }
  9045. void CLocalWUGraph::setWfid(unsigned wfid)
  9046. {
  9047. p->setPropInt("@wfid", wfid);
  9048. }
  9049. void CLocalWUGraph::setXGMML(const char *str)
  9050. {
  9051. setXGMMLTree(createPTreeFromXMLString(str,ipt_lowmem));
  9052. }
  9053. void CLocalWUGraph::setXGMMLTree(IPropertyTree *_graph)
  9054. {
  9055. assertex(strcmp(_graph->queryName(), "graph")==0);
  9056. IPropertyTree *xgmml = p->setPropTree("xgmml");
  9057. MemoryBuffer mb;
  9058. _graph->serialize(mb);
  9059. // Note - we could compress further but that would introduce compatibility concerns, so don't bother
  9060. // Cassandra workunit code actually lzw compresses the parent anyway
  9061. xgmml->setPropBin("graphBin", mb.length(), mb.toByteArray());
  9062. graph.setown(_graph);
  9063. }
  9064. static void expandAttributes(IPropertyTree & targetNode, IPropertyTree & progressNode)
  9065. {
  9066. Owned<IAttributeIterator> aIter = progressNode.getAttributes();
  9067. ForEach (*aIter)
  9068. {
  9069. const char *aName = aIter->queryName()+1;
  9070. if (0 != stricmp("id", aName)) // "id" reserved.
  9071. {
  9072. IPropertyTree *att = targetNode.addPropTree("att");
  9073. att->setProp("@name", aName);
  9074. att->setProp("@value", aIter->queryValue());
  9075. }
  9076. }
  9077. }
  9078. void CLocalWUGraph::mergeProgress(IPropertyTree &rootNode, IPropertyTree &progressTree, const unsigned &progressV) const
  9079. {
  9080. IPropertyTree *graphNode = rootNode.queryPropTree("att/graph");
  9081. if (!graphNode) return;
  9082. unsigned nodeId = rootNode.getPropInt("@id");
  9083. StringBuffer progressNodePath("node[@id=\"");
  9084. progressNodePath.append(nodeId).append("\"]");
  9085. IPropertyTree *progressNode = progressTree.queryPropTree(progressNodePath.str());
  9086. if (progressNode)
  9087. {
  9088. expandAttributes(*graphNode, *progressNode);
  9089. Owned<IPropertyTreeIterator> edges = progressNode->getElements("edge");
  9090. ForEach (*edges)
  9091. {
  9092. IPropertyTree &edge = edges->query();
  9093. StringBuffer edgePath("edge[@id=\"");
  9094. edgePath.append(edge.queryProp("@id")).append("\"]");
  9095. IPropertyTree *graphEdge = graphNode->queryPropTree(edgePath.str());
  9096. if (graphEdge)
  9097. {
  9098. if (progressV < 1)
  9099. mergePTree(graphEdge, &edge);
  9100. else
  9101. { // must translate to XGMML format
  9102. expandAttributes(*graphEdge, edge);
  9103. // This is really only here, so that our progress format can use non-attribute values, which have different efficiency qualifies (e.g. can be external by dali)
  9104. Owned<IPropertyTreeIterator> iter = edge.getElements("*");
  9105. ForEach (*iter)
  9106. {
  9107. IPropertyTree &t = iter->query();
  9108. IPropertyTree *att = graphEdge->addPropTree("att");
  9109. att->setProp("@name", t.queryName());
  9110. att->setProp("@value", t.queryProp(NULL));
  9111. }
  9112. }
  9113. }
  9114. }
  9115. Owned<IPropertyTreeIterator> nodes = progressNode->getElements("node");
  9116. ForEach (*nodes)
  9117. {
  9118. IPropertyTree &node = nodes->query();
  9119. StringBuffer nodePath("node[@id=\"");
  9120. nodePath.append(node.queryProp("@id")).append("\"]");
  9121. IPropertyTree *_node = graphNode->queryPropTree(nodePath.str());
  9122. if (_node)
  9123. {
  9124. if (progressV < 1)
  9125. mergePTree(_node, &node);
  9126. else
  9127. { // must translate to XGMML format
  9128. expandAttributes(*_node, node);
  9129. }
  9130. }
  9131. }
  9132. }
  9133. Owned<IPropertyTreeIterator> iter = graphNode->getElements("node");
  9134. ForEach (*iter)
  9135. mergeProgress(iter->query(), progressTree, progressV);
  9136. }
  9137. IPropertyTree * CLocalWUGraph::getXGMMLTreeRaw() const
  9138. {
  9139. return p->getPropTree("xgmml");
  9140. }
  9141. IPropertyTree * CLocalWUGraph::getXGMMLTree(bool doMergeProgress, bool doFormatStats) const
  9142. {
  9143. {
  9144. CriticalBlock block(owner.crit);
  9145. if (!graph)
  9146. {
  9147. // NB: although graphBin introduced in wuidVersion==2,
  9148. // daliadmin can retrospectively compress existing graphs, so need to check for all versions
  9149. MemoryBuffer mb;
  9150. if (p->getPropBin("xgmml/graphBin", mb))
  9151. graph.setown(createPTree(mb, ipt_lowmem));
  9152. else
  9153. graph.setown(p->getBranch("xgmml/graph"));
  9154. if (!graph)
  9155. return NULL;
  9156. }
  9157. }
  9158. if (!doMergeProgress)
  9159. return graph.getLink();
  9160. else
  9161. {
  9162. Owned<IPropertyTree> copy = createPTreeFromIPT(graph, ipt_lowmem);
  9163. Owned<IConstWUGraphProgress> progress = owner.getGraphProgress(p->queryProp("@name"));
  9164. if (progress)
  9165. {
  9166. //MORE: Eventually this should directly access the new stats structure
  9167. unsigned progressV = progress->queryFormatVersion();
  9168. Owned<IPropertyTree> progressTree = progress->getProgressTree(doFormatStats);
  9169. Owned<IPropertyTreeIterator> nodeIterator = copy->getElements("node");
  9170. ForEach (*nodeIterator)
  9171. mergeProgress(nodeIterator->query(), *progressTree, progressV);
  9172. }
  9173. return copy.getClear();
  9174. }
  9175. }
  9176. WUGraphType CLocalWUGraph::getType() const
  9177. {
  9178. return (WUGraphType) getEnum(p, "@type", graphTypes);
  9179. }
  9180. IStringVal & CLocalWUGraph::getTypeName(IStringVal &str) const
  9181. {
  9182. str.set(p->queryProp("@type"));
  9183. if (!str.length())
  9184. str.set("unknown");
  9185. return str;
  9186. }
  9187. unsigned CLocalWUGraph::getWfid() const
  9188. {
  9189. return p->getPropInt("@wfid", 0);
  9190. }
  9191. void CLocalWUGraph::setType(WUGraphType _type)
  9192. {
  9193. setEnum(p, "@type", _type, graphTypes);
  9194. }
  9195. //=================================================================================================
  9196. EnumMapping queryFileTypes[] = {
  9197. { FileTypeCpp, "cpp" },
  9198. { FileTypeDll, "dll" },
  9199. { FileTypeResText, "res" },
  9200. { FileTypeHintXml, "hint" },
  9201. { FileTypeXml, "xml" },
  9202. { FileTypeLog, "log" },
  9203. { FileTypePostMortem, "postmortem" },
  9204. { FileTypeSize, NULL },
  9205. };
  9206. CLocalWUAssociated::CLocalWUAssociated(IPropertyTree *props) : p(props)
  9207. {
  9208. }
  9209. WUFileType CLocalWUAssociated::getType() const
  9210. {
  9211. return (WUFileType)getEnum(p, "@type", queryFileTypes);
  9212. }
  9213. IStringVal & CLocalWUAssociated::getDescription(IStringVal & str) const
  9214. {
  9215. str.set(p->queryProp("@desc"));
  9216. return str;
  9217. }
  9218. IStringVal & CLocalWUAssociated::getIp(IStringVal & str) const
  9219. {
  9220. str.set(p->queryProp("@ip"));
  9221. return str;
  9222. }
  9223. IStringVal & CLocalWUAssociated::getName(IStringVal & str) const
  9224. {
  9225. str.set(p->queryProp("@filename"));
  9226. return str;
  9227. }
  9228. IStringVal & CLocalWUAssociated::getNameTail(IStringVal & str) const
  9229. {
  9230. str.set(pathTail(p->queryProp("@filename")));
  9231. return str;
  9232. }
  9233. unsigned CLocalWUAssociated::getCrc() const
  9234. {
  9235. return p->getPropInt("@crc", 0);
  9236. }
  9237. unsigned CLocalWUAssociated::getMinActivityId() const
  9238. {
  9239. return p->getPropInt("@minActivity", 0);
  9240. }
  9241. unsigned CLocalWUAssociated::getMaxActivityId() const
  9242. {
  9243. return p->getPropInt("@maxActivity", 0);
  9244. }
  9245. //=================================================================================================
  9246. CLocalWUQuery::CLocalWUQuery(IPropertyTree *props) : p(props)
  9247. {
  9248. associatedCached = false;
  9249. }
  9250. EnumMapping queryTypes[] = {
  9251. { QueryTypeUnknown, "unknown" },
  9252. { QueryTypeEcl, "ECL" },
  9253. { QueryTypeSql, "SQL" },
  9254. { QueryTypeXml, "XML" },
  9255. { QueryTypeAttribute, "Attribute" },
  9256. { QueryTypeSize, NULL },
  9257. };
  9258. WUQueryType CLocalWUQuery::getQueryType() const
  9259. {
  9260. return (WUQueryType) getEnum(p, "@type", queryTypes);
  9261. }
  9262. void CLocalWUQuery::setQueryType(WUQueryType qt)
  9263. {
  9264. setEnum(p, "@type", qt, queryTypes);
  9265. }
  9266. IStringVal& CLocalWUQuery::getQueryText(IStringVal &str) const
  9267. {
  9268. const char *text = p->queryProp("Text");
  9269. if (!text)
  9270. text = p->queryProp("ShortText");
  9271. str.set(text);
  9272. return str;
  9273. }
  9274. IStringVal& CLocalWUQuery::getQueryShortText(IStringVal &str) const
  9275. {
  9276. const char * text = p->queryProp("ShortText");
  9277. if (text)
  9278. str.set(text);
  9279. else
  9280. {
  9281. text = p->queryProp("Text");
  9282. if (isArchiveQuery(text))
  9283. {
  9284. Owned<IPropertyTree> xml = createPTreeFromXMLString(text, ipt_caseInsensitive|ipt_lowmem);
  9285. const char * path = xml->queryProp("Query/@attributePath");
  9286. if (path)
  9287. {
  9288. IPropertyTree * resolved = resolveDefinitionInArchive(xml, path);
  9289. if (resolved)
  9290. str.set(resolved->queryProp(NULL));
  9291. }
  9292. else
  9293. str.set(xml->queryProp("Query"));
  9294. }
  9295. else
  9296. str.set(text);
  9297. }
  9298. return str;
  9299. }
  9300. bool CLocalWUQuery::isArchive() const
  9301. {
  9302. if (p->hasProp("@isArchive"))
  9303. return p->getPropBool("@isArchive");
  9304. const char *text = p->queryProp("Text");
  9305. return isArchiveQuery(text);
  9306. }
  9307. IStringVal& CLocalWUQuery::getQueryName(IStringVal &str) const
  9308. {
  9309. str.set(p->queryProp("@name"));
  9310. return str;
  9311. }
  9312. IStringVal & CLocalWUQuery::getQueryMainDefinition(IStringVal & str) const
  9313. {
  9314. str.set(p->queryProp("@main"));
  9315. return str;
  9316. }
  9317. IStringVal& CLocalWUQuery::getQueryDllName(IStringVal &str) const
  9318. {
  9319. Owned<IConstWUAssociatedFile> entry = getAssociatedFile(FileTypeDll, 0);
  9320. if (entry)
  9321. entry->getNameTail(str);
  9322. return str;
  9323. }
  9324. IStringVal& CLocalWUQuery::getQueryCppName(IStringVal &str) const
  9325. {
  9326. Owned<IConstWUAssociatedFile> entry = getAssociatedFile(FileTypeCpp, 0);
  9327. if (entry)
  9328. entry->getName(str);
  9329. return str;
  9330. }
  9331. IStringVal& CLocalWUQuery::getQueryResTxtName(IStringVal &str) const
  9332. {
  9333. Owned<IConstWUAssociatedFile> entry = getAssociatedFile(FileTypeResText, 0);
  9334. if (entry)
  9335. entry->getName(str);
  9336. return str;
  9337. }
  9338. unsigned CLocalWUQuery::getQueryDllCrc() const
  9339. {
  9340. Owned<IConstWUAssociatedFile> entry = getAssociatedFile(FileTypeDll, 0);
  9341. if (entry)
  9342. return entry->getCrc();
  9343. return 0;
  9344. }
  9345. void CLocalWUQuery::setQueryText(const char *text)
  9346. {
  9347. bool isArchive = isArchiveQuery(text);
  9348. if (isArchive)
  9349. {
  9350. p->setProp("Text", text);
  9351. Owned<IPropertyTree> xml = createPTreeFromXMLString(text, ipt_caseInsensitive|ipt_lowmem);
  9352. const char * path = xml->queryProp("Query/@attributePath");
  9353. if (path)
  9354. {
  9355. IPropertyTree * resolved = resolveDefinitionInArchive(xml, path);
  9356. if (resolved)
  9357. p->setProp("ShortText", resolved->queryProp(NULL));
  9358. }
  9359. else
  9360. p->setProp("ShortText", xml->queryProp("Query"));
  9361. }
  9362. else
  9363. {
  9364. p->setProp("Text", text); // At some point in the future we may be able to remove this,
  9365. // but as long as there may be new workunits compiled by old systems, we can't
  9366. p->setProp("ShortText", text);
  9367. }
  9368. p->setPropBool("@isArchive", isArchive);
  9369. if (isArchive)
  9370. p->setPropBool("@hasArchive", true); //preserved if setQueryText is called multiple times. Should setting this be more explicit?
  9371. }
  9372. void CLocalWUQuery::setQueryName(const char *qname)
  9373. {
  9374. p->setProp("@name", qname);
  9375. }
  9376. void CLocalWUQuery::setQueryMainDefinition(const char * str)
  9377. {
  9378. p->setProp("@main", str);
  9379. }
  9380. void CLocalWUQuery::addAssociatedFile(WUFileType type, const char * name, const char * ip, const char * desc, unsigned crc, unsigned minActivity, unsigned maxActivity)
  9381. {
  9382. CriticalBlock block(crit);
  9383. loadAssociated();
  9384. StringBuffer xpath;
  9385. xpath.append("Associated/File[@filename=\"").append(name).append("\"]");
  9386. if (p->hasProp(xpath))
  9387. return;
  9388. if (!associated.length())
  9389. p->addPropTree("Associated");
  9390. IPropertyTree *pl = p->queryPropTree("Associated");
  9391. IPropertyTree *s = pl->addPropTree("File");
  9392. setEnum(s, "@type", type, queryFileTypes);
  9393. s->setProp("@filename", name);
  9394. s->setProp("@ip", ip);
  9395. s->setProp("@desc", desc);
  9396. if (crc)
  9397. s->setPropInt("@crc", crc);
  9398. if (minActivity)
  9399. s->setPropInt("@minActivity", minActivity);
  9400. if (maxActivity)
  9401. s->setPropInt("@maxActivity", maxActivity);
  9402. IConstWUAssociatedFile * q = new CLocalWUAssociated(LINK(s));
  9403. associated.append(*q);
  9404. }
  9405. void CLocalWUQuery::removeAssociatedFile(WUFileType type, const char * name, const char * desc)
  9406. {
  9407. CriticalBlock block(crit);
  9408. associatedCached = false;
  9409. associated.kill();
  9410. StringBuffer xpath;
  9411. xpath.append("Associated/File");
  9412. if (type)
  9413. xpath.append("[@type=\"").append(getEnumText(type, queryFileTypes)).append("\"]");
  9414. if (name)
  9415. xpath.append("[@filename=\"").append(name).append("\"]");
  9416. if (desc)
  9417. xpath.append("[@desc=\"").append(desc).append("\"]");
  9418. p->removeProp(xpath.str());
  9419. }
  9420. void CLocalWUQuery::removeAssociatedFiles()
  9421. {
  9422. associatedCached = false;
  9423. associated.kill();
  9424. p->removeProp("Associated");
  9425. }
  9426. IConstWUAssociatedFile * CLocalWUQuery::getAssociatedFile(WUFileType type, unsigned index) const
  9427. {
  9428. CriticalBlock block(crit);
  9429. loadAssociated();
  9430. ForEachItemIn(idx, associated)
  9431. {
  9432. CLocalWUAssociated &cur = static_cast<CLocalWUAssociated &>(associated.item(idx));
  9433. if (cur.getType() == type)
  9434. {
  9435. if (index-- == 0)
  9436. return &OLINK(cur);
  9437. }
  9438. }
  9439. return NULL;
  9440. }
  9441. void CLocalWUQuery::addSpecialCaseAssociated(WUFileType type, const char * propname, unsigned crc) const
  9442. {
  9443. const char * name = p->queryProp(propname);
  9444. if (name)
  9445. {
  9446. IPropertyTree *s = createPTree("File");
  9447. setEnum(s, "@type", type, queryFileTypes);
  9448. s->setProp("@filename", name);
  9449. if (crc)
  9450. s->setPropInt("@crc", crc);
  9451. associated.append(*new CLocalWUAssociated(s));
  9452. }
  9453. }
  9454. void CLocalWUQuery::loadAssociated() const
  9455. {
  9456. CriticalBlock block(crit);
  9457. if (!associatedCached)
  9458. {
  9459. assertex(associated.length() == 0);
  9460. addSpecialCaseAssociated(FileTypeDll, "DllName", p->getPropInt("DllCrc", 0));
  9461. addSpecialCaseAssociated(FileTypeCpp, "CppName", 0);
  9462. addSpecialCaseAssociated(FileTypeResText, "ResTxtName", 0);
  9463. Owned<IPropertyTreeIterator> r = p->getElements("Associated/File");
  9464. for (r->first(); r->isValid(); r->next())
  9465. {
  9466. IPropertyTree *rp = &r->query();
  9467. rp->Link();
  9468. associated.append(*new CLocalWUAssociated(rp));
  9469. }
  9470. associatedCached = true;
  9471. }
  9472. }
  9473. IConstWUAssociatedFileIterator& CLocalWUQuery::getAssociatedFiles() const
  9474. {
  9475. CriticalBlock block(crit);
  9476. loadAssociated();
  9477. return *new CArrayIteratorOf<IConstWUAssociatedFile,IConstWUAssociatedFileIterator> (associated, 0, (IConstWUQuery *) this);
  9478. }
  9479. //========================================================================================
  9480. CLocalWUWebServicesInfo::CLocalWUWebServicesInfo(IPropertyTree *props) : p(props)
  9481. {
  9482. }
  9483. IStringVal& CLocalWUWebServicesInfo::getModuleName(IStringVal &str) const
  9484. {
  9485. str.set(p->queryProp("@module"));
  9486. return str;
  9487. }
  9488. IStringVal& CLocalWUWebServicesInfo::getAttributeName(IStringVal &str) const
  9489. {
  9490. str.set(p->queryProp("@attribute"));
  9491. return str;
  9492. }
  9493. IStringVal& CLocalWUWebServicesInfo::getDefaultName(IStringVal &str) const
  9494. {
  9495. str.set(p->queryProp("@defaultName"));
  9496. return str;
  9497. }
  9498. unsigned CLocalWUWebServicesInfo::getWebServicesCRC() const
  9499. {
  9500. return (unsigned) p->getPropInt("@crc");
  9501. }
  9502. IStringVal& CLocalWUWebServicesInfo::getInfo(const char *name, IStringVal &str) const
  9503. {
  9504. if (!name)
  9505. {
  9506. StringBuffer ws_info;
  9507. ws_info.appendf("<%s ", p->queryName());
  9508. Owned<IAttributeIterator> attrs = p->getAttributes();
  9509. for(attrs->first(); attrs->isValid(); attrs->next())
  9510. {
  9511. const char *name = attrs->queryName()+1;
  9512. const char *value = attrs->queryValue();
  9513. ws_info.appendf("%s='%s' ", name, value);
  9514. }
  9515. ws_info.append("> \n");
  9516. Owned<IPropertyTreeIterator> info = p->getElements("*");
  9517. ForEach(*info)
  9518. {
  9519. IPropertyTree &item = info->query();
  9520. const char *name = item.queryName();
  9521. if (name)
  9522. {
  9523. MemoryBuffer mb;
  9524. bool isbin = p->isBinary(name);
  9525. if (isbin)
  9526. {
  9527. p->getPropBin(name,mb);
  9528. if (mb.length())
  9529. {
  9530. unsigned len = 0;
  9531. mb.read(len);
  9532. StringBuffer encodedString;
  9533. StringBuffer val(len, (const char *) mb.readDirect(len));
  9534. encodeXML(val, encodedString);
  9535. ws_info.appendf("<%s>%s</%s>", name, encodedString.str(), name);
  9536. }
  9537. }
  9538. else
  9539. {
  9540. StringBuffer tmp;
  9541. toXML(&item, tmp);
  9542. ws_info.append(tmp.str());
  9543. }
  9544. }
  9545. }
  9546. ws_info.appendf("</%s>", p->queryName());
  9547. str.setLen(ws_info.str(), ws_info.length());
  9548. }
  9549. else
  9550. {
  9551. MemoryBuffer mb;
  9552. p->getPropBin(name,mb);
  9553. if (mb.length())
  9554. {
  9555. unsigned len;
  9556. mb.read(len);
  9557. str.setLen((const char *) mb.readDirect(len), len);
  9558. }
  9559. }
  9560. return str;
  9561. }
  9562. IStringVal& CLocalWUWebServicesInfo::getText(const char *name, IStringVal &str) const
  9563. {
  9564. str.set(p->queryProp(name));
  9565. return str;
  9566. }
  9567. void CLocalWUWebServicesInfo::setModuleName(const char *mname)
  9568. {
  9569. p->setProp("@module", mname);
  9570. }
  9571. void CLocalWUWebServicesInfo::setAttributeName(const char *aname)
  9572. {
  9573. p->setProp("@attribute", aname);
  9574. }
  9575. void CLocalWUWebServicesInfo::setDefaultName(const char *dname)
  9576. {
  9577. p->setProp("@defaultName", dname);
  9578. }
  9579. void CLocalWUWebServicesInfo::setWebServicesCRC(unsigned crc)
  9580. {
  9581. p->setPropInt("@crc", crc);
  9582. }
  9583. void CLocalWUWebServicesInfo::setInfo(const char *name, const char *info)
  9584. {
  9585. MemoryBuffer m;
  9586. unsigned len = (size32_t)strlen(info);
  9587. serializeLPString(len, info, m);
  9588. p->setPropBin(name, m.length(), m.toByteArray());
  9589. }
  9590. void CLocalWUWebServicesInfo::setText(const char *name, const char *info)
  9591. {
  9592. p->setProp(name, info);
  9593. }
  9594. //========================================================================================
  9595. CLocalWUResult::CLocalWUResult(IPropertyTree *props) : p(props)
  9596. {
  9597. }
  9598. EnumMapping resultStatuses[] = {
  9599. { ResultStatusUndefined, "undefined" },
  9600. { ResultStatusCalculated, "calculated" },
  9601. { ResultStatusSupplied, "supplied" },
  9602. { ResultStatusFailed, "failed" },
  9603. { ResultStatusPartial, "partial" },
  9604. { ResultStatusSize, NULL }
  9605. };
  9606. WUResultStatus CLocalWUResult::getResultStatus() const
  9607. {
  9608. return (WUResultStatus ) getEnum(p, "@status", resultStatuses);
  9609. }
  9610. IStringVal& CLocalWUResult::getResultName(IStringVal &str) const
  9611. {
  9612. str.set(p->queryProp("@name"));
  9613. return str;
  9614. }
  9615. int CLocalWUResult::getResultSequence() const
  9616. {
  9617. return p->getPropInt("@sequence", -1);
  9618. }
  9619. bool CLocalWUResult::isResultScalar() const
  9620. {
  9621. return p->getPropInt("@isScalar", 1) != 0;
  9622. }
  9623. bool findSize(int size, IntArray &sizes)
  9624. {
  9625. ForEachItemIn(idx, sizes)
  9626. {
  9627. if (sizes.item(idx)==size)
  9628. return true;
  9629. }
  9630. return false;
  9631. }
  9632. void CLocalWUResult::getSchema(IArrayOf<ITypeInfo> &types, StringAttrArray &names, IStringVal * eclText) const
  9633. {
  9634. MemoryBuffer schema;
  9635. p->getPropBin("SchemaRaw", schema);
  9636. if (schema.length())
  9637. {
  9638. for (;;)
  9639. {
  9640. StringAttr name;
  9641. schema.read(name);
  9642. if (*schema.readDirect(0)==type_void)
  9643. break;
  9644. names.append(*new StringAttrItem(name));
  9645. types.append(*deserializeType(schema)); // MORE - nested records!
  9646. }
  9647. schema.skip(1);
  9648. if (schema.length() != schema.getPos())
  9649. {
  9650. unsigned eclLen;
  9651. schema.read(eclLen);
  9652. const char * schemaData = (const char *)schema.readDirect(eclLen);
  9653. if (eclText)
  9654. {
  9655. eclText->setLen(schemaData, eclLen);
  9656. if ((eclLen == 0) && names.ordinality())
  9657. {
  9658. const char * firstName = names.item(0).text;
  9659. StringBuffer temp;
  9660. temp.append("RECORD ");
  9661. types.item(0).getECLType(temp);
  9662. temp.append(" value{NAMED('").append(firstName).append("')}").append("; END;");
  9663. eclText->set(temp.str());
  9664. }
  9665. }
  9666. }
  9667. }
  9668. }
  9669. void readRow(StringBuffer &out, MemoryBuffer &in, TypeInfoArray &types, StringAttrArray &names)
  9670. {
  9671. ForEachItemIn(idx, types)
  9672. {
  9673. StringAttrItem &name = names.item(idx);
  9674. ITypeInfo &type = types.item(idx);
  9675. unsigned size = type.getSize();
  9676. switch(type.getTypeCode())
  9677. {
  9678. case type_data:
  9679. if (size==UNKNOWN_LENGTH)
  9680. {
  9681. if (in.remaining() < sizeof(int))
  9682. throw MakeStringException(WUERR_CorruptResult, "corrupt workunit information");
  9683. in.read(size);
  9684. }
  9685. outputXmlData(size, in.readDirect(size), name.text, out);
  9686. break;
  9687. case type_string:
  9688. if (size==UNKNOWN_LENGTH)
  9689. {
  9690. if (in.remaining() < sizeof(int))
  9691. throw MakeStringException(WUERR_CorruptResult, "corrupt workunit information");
  9692. in.read(size);
  9693. }
  9694. outputXmlString(size, (const char *) in.readDirect(size), name.text, out);
  9695. break;
  9696. case type_varstring:
  9697. {
  9698. if (size == UNKNOWN_LENGTH)
  9699. size = (size32_t)strlen((const char *) in.readDirect(0))+1;
  9700. const char * text = (const char *) in.readDirect(size);
  9701. outputXmlString((size32_t)strlen(text), text, name.text, out);
  9702. break;
  9703. }
  9704. case type_unicode:
  9705. {
  9706. unsigned len = type.getStringLen();
  9707. if (size==UNKNOWN_LENGTH)
  9708. in.read(len);
  9709. outputXmlUnicode(len, (UChar const *) in.readDirect(len*2), name.text, out);
  9710. }
  9711. break;
  9712. case type_utf8:
  9713. {
  9714. unsigned len = type.getStringLen();
  9715. if (size==UNKNOWN_LENGTH)
  9716. {
  9717. in.read(len);
  9718. size = rtlUtf8Size(len, in.readDirect(0));
  9719. }
  9720. outputXmlUtf8(len, (const char *) in.readDirect(size), name.text, out);
  9721. }
  9722. break;
  9723. case type_qstring:
  9724. {
  9725. unsigned len = type.getStringLen();
  9726. if (size==UNKNOWN_LENGTH)
  9727. in.read(len);
  9728. unsigned outlen;
  9729. char *outstr;
  9730. rtlQStrToStrX(outlen, outstr, len, (const char *) in.readDirect(rtlQStrSize(len)));
  9731. outputXmlString(outlen, outstr, name.text, out);
  9732. free(outstr);
  9733. break;
  9734. }
  9735. case type_int:
  9736. case type_swapint:
  9737. if (type.isSigned())
  9738. {
  9739. const unsigned char *raw = (const unsigned char *) in.readDirect(size);
  9740. unsigned __int64 cval8 = 0;
  9741. //MORE: I think this is wrong - swapped doesn't mean little/big/
  9742. if (type.isSwappedEndian())
  9743. {
  9744. unsigned idx = 0;
  9745. if (raw[idx] & 0x80)
  9746. cval8 = (__int64)-1;
  9747. while (size--)
  9748. cval8 = (cval8 << 8) | raw[idx++];
  9749. }
  9750. else
  9751. {
  9752. if (raw[size-1] & 0x80)
  9753. cval8 = (__int64)-1;
  9754. while (size--)
  9755. cval8 = (cval8 << 8) | raw[size];
  9756. }
  9757. outputXmlInt((__int64) cval8, name.text, out);
  9758. }
  9759. else
  9760. {
  9761. const unsigned char *raw = (const unsigned char *) in.readDirect(size);
  9762. unsigned __int64 cval8 = 0;
  9763. if (type.isSwappedEndian())
  9764. {
  9765. unsigned idx = 0;
  9766. while (size--)
  9767. cval8 = (cval8 << 8) | raw[idx++];
  9768. }
  9769. else
  9770. {
  9771. while (size--)
  9772. cval8 = (cval8 << 8) | raw[size];
  9773. }
  9774. outputXmlUInt(cval8, name.text, out);
  9775. }
  9776. break;
  9777. case type_boolean:
  9778. bool cvalb;
  9779. in.read(cvalb);
  9780. outputXmlBool(cvalb, name.text, out);
  9781. break;
  9782. case type_decimal:
  9783. if (type.isSigned())
  9784. outputXmlDecimal(in.readDirect(size), size, type.getPrecision(), name.text, out);
  9785. else
  9786. outputXmlUDecimal(in.readDirect(size), size, type.getPrecision(), name.text, out);
  9787. break;
  9788. case type_real:
  9789. double cvald;
  9790. switch(size)
  9791. {
  9792. case 4:
  9793. float cvalf;
  9794. in.read(cvalf);
  9795. cvald = cvalf;
  9796. break;
  9797. case 8:
  9798. in.read(cvald);
  9799. break;
  9800. default:
  9801. throwUnexpected();
  9802. }
  9803. outputXmlReal(cvald, name.text, out);
  9804. break;
  9805. default:
  9806. assertex(!"unexpected type in raw record");
  9807. break;
  9808. }
  9809. }
  9810. }
  9811. IStringVal& CLocalWUResult::getResultXml(IStringVal &str, bool hidePassword) const
  9812. {
  9813. TypeInfoArray types;
  9814. StringAttrArray names;
  9815. getSchema(types, names);
  9816. StringBuffer xml;
  9817. const char * name = p->queryProp("@name");
  9818. if (name)
  9819. xml.appendf("<Dataset name=\'%s\'>\n", name);
  9820. else
  9821. xml.append("<Dataset>\n");
  9822. if (hidePassword && p->getPropBool("Format/@password"))
  9823. {
  9824. xml.append(" <Row>");
  9825. appendXMLTag(xml, name, "****");
  9826. xml.append("</Row>\n");
  9827. }
  9828. else if (p->hasProp("Value"))
  9829. {
  9830. MemoryBuffer raw;
  9831. p->getPropBin("Value", raw);
  9832. unsigned __int64 numrows = getResultRowCount();
  9833. while (numrows--)
  9834. {
  9835. xml.append(" <Row>");
  9836. readRow(xml, raw, types, names);
  9837. xml.append("</Row>\n");
  9838. }
  9839. }
  9840. else if (p->hasProp("xmlValue"))
  9841. {
  9842. xml.append(" <Row>");
  9843. appendXMLTag(xml, name, p->queryProp("xmlValue"));
  9844. xml.append("</Row>\n");
  9845. }
  9846. xml.append("</Dataset>\n");
  9847. str.set(xml.str());
  9848. return str;
  9849. }
  9850. IProperties *CLocalWUResult::queryResultXmlns()
  9851. {
  9852. CriticalBlock block(crit);
  9853. if (xmlns)
  9854. return xmlns;
  9855. xmlns.setown(createProperties());
  9856. Owned<IAttributeIterator> it = p->getAttributes();
  9857. unsigned prefixLen = strlen("@xmlns");
  9858. ForEach(*it)
  9859. {
  9860. const char *name = it->queryName();
  9861. if (!strncmp("@xmlns", name, prefixLen))
  9862. {
  9863. if (name[prefixLen]==':') //normal case
  9864. xmlns->setProp(name+prefixLen+1, it->queryValue());
  9865. else if (!name[prefixLen]) //special case, unprefixed namespace
  9866. xmlns->setProp("xmlns", it->queryValue());
  9867. }
  9868. }
  9869. return xmlns;
  9870. }
  9871. unsigned CLocalWUResult::getResultFetchSize() const
  9872. {
  9873. return p->getPropInt("fetchSize", 100);
  9874. }
  9875. __int64 CLocalWUResult::getResultTotalRowCount() const
  9876. {
  9877. return p->getPropInt64("totalRowCount", -1);
  9878. }
  9879. __int64 CLocalWUResult::getResultRowCount() const
  9880. {
  9881. return p->getPropInt64("rowCount", 0);
  9882. }
  9883. void CLocalWUResult::getResultDataset(IStringVal & ecl, IStringVal & defs) const
  9884. {
  9885. ecl.set(p->queryProp("datasetEcl"));
  9886. defs.set(p->queryProp("datasetEclDefs"));
  9887. }
  9888. IStringVal& CLocalWUResult::getResultLogicalName(IStringVal & val) const
  9889. {
  9890. val.set(p->queryProp("logicalName"));
  9891. return val;
  9892. }
  9893. IStringVal& CLocalWUResult::getResultKeyField(IStringVal & ecl) const
  9894. {
  9895. ecl.set(p->queryProp("keyField"));
  9896. return ecl;
  9897. }
  9898. unsigned CLocalWUResult::getResultRequestedRows() const
  9899. {
  9900. return p->getPropInt("requestedRows", 1);
  9901. }
  9902. IStringVal& CLocalWUResult::getResultEclSchema(IStringVal & str) const
  9903. {
  9904. TypeInfoArray types;
  9905. StringAttrArray names;
  9906. getSchema(types, names, &str);
  9907. return str;
  9908. }
  9909. IStringVal& CLocalWUResult::getResultRecordSizeEntry(IStringVal & str) const
  9910. {
  9911. str.set(p->queryProp("@recordSizeEntry"));
  9912. return str;
  9913. }
  9914. IStringVal& CLocalWUResult::getResultTransformerEntry(IStringVal & str) const
  9915. {
  9916. str.set(p->queryProp("@transformerEntry"));
  9917. return str;
  9918. }
  9919. __int64 CLocalWUResult::getResultRowLimit() const
  9920. {
  9921. return p->getPropInt64("@rowLimit");
  9922. }
  9923. IStringVal& CLocalWUResult::getResultFilename(IStringVal & str) const
  9924. {
  9925. str.set(p->queryProp("@tempFilename"));
  9926. return str;
  9927. }
  9928. IStringVal& CLocalWUResult::getResultFieldOpt(const char *name, IStringVal &str) const
  9929. {
  9930. str.clear();
  9931. if (!name || !*name)
  9932. return str;
  9933. IPropertyTree *format = p->queryPropTree("Format");
  9934. if (!format)
  9935. return str;
  9936. VStringBuffer xpath("@%s", name);
  9937. str.set(format->queryProp(xpath));
  9938. return str;
  9939. }
  9940. void CLocalWUResult::getResultWriteLocation(IStringVal & _graph, unsigned & _activityId) const
  9941. {
  9942. _graph.set(p->queryProp("@graph"));
  9943. _activityId = p->getPropInt("@activity", 0);
  9944. }
  9945. void CLocalWUResult::setResultStatus(WUResultStatus status)
  9946. {
  9947. setEnum(p, "@status", status, resultStatuses);
  9948. if (status==ResultStatusUndefined)
  9949. {
  9950. p->removeProp("Value");
  9951. p->removeProp("totalRowCount");
  9952. p->removeProp("rowCount");
  9953. p->removeProp("@format");
  9954. p->removeProp("@tempFileNmae");
  9955. p->removeProp("logicalName");
  9956. }
  9957. }
  9958. void CLocalWUResult::setResultName(const char *s)
  9959. {
  9960. p->setProp("@name", s);
  9961. }
  9962. void CLocalWUResult::setResultSequence(unsigned seq)
  9963. {
  9964. p->setPropInt("@sequence", seq);
  9965. }
  9966. void CLocalWUResult::setResultSchemaRaw(unsigned size, const void *schema)
  9967. {
  9968. p->setPropBin("SchemaRaw", size, schema);
  9969. }
  9970. void CLocalWUResult::setResultXmlns(const char *prefix, const char *uri)
  9971. {
  9972. StringBuffer xpath("@xmlns");
  9973. if (prefix && *prefix)
  9974. xpath.append(':').append(prefix);
  9975. p->setProp(xpath, uri);
  9976. }
  9977. void CLocalWUResult::setResultFieldOpt(const char *name, const char *value)
  9978. {
  9979. if (!name || !*name)
  9980. return;
  9981. IPropertyTree *format = ensurePTree(p, "Format");
  9982. VStringBuffer xpath("@%s", name);
  9983. format->setProp(xpath, value);
  9984. }
  9985. void CLocalWUResult::setResultWriteLocation(const char * _graph, unsigned _activityId)
  9986. {
  9987. p->setProp("@graph", _graph);
  9988. p->setPropInt("@activity", _activityId);
  9989. }
  9990. void CLocalWUResult::setResultScalar(bool isScalar)
  9991. {
  9992. p->setPropInt("@isScalar", (int) isScalar);
  9993. if (isScalar)
  9994. setResultTotalRowCount(1);
  9995. }
  9996. void CLocalWUResult::setResultRaw(unsigned len, const void *data, WUResultFormat format)
  9997. {
  9998. p->setPropBin("Value", len, data);
  9999. setResultStatus(ResultStatusSupplied);
  10000. setResultFormat(format);
  10001. }
  10002. void CLocalWUResult::setResultFormat(WUResultFormat format)
  10003. {
  10004. switch (format)
  10005. {
  10006. case ResultFormatXml:
  10007. p->setProp("@format","xml");
  10008. break;
  10009. case ResultFormatXmlSet:
  10010. p->setProp("@format","xmlSet");
  10011. break;
  10012. case ResultFormatCsv:
  10013. p->setProp("@format","csv");
  10014. break;
  10015. default:
  10016. p->removeProp("@format");
  10017. break;
  10018. }
  10019. }
  10020. void CLocalWUResult::setResultXML(const char *val)
  10021. {
  10022. p->setProp("xmlValue", val);
  10023. }
  10024. void CLocalWUResult::addResultRaw(unsigned len, const void *data, WUResultFormat format)
  10025. {
  10026. p->appendPropBin("Value", len, data);
  10027. setResultStatus(ResultStatusPartial);
  10028. const char *existingFormat = p->queryProp("@format");
  10029. const char *formatStr = NULL;
  10030. switch (format)
  10031. {
  10032. case ResultFormatXml:
  10033. formatStr = "xml";
  10034. break;
  10035. case ResultFormatXmlSet:
  10036. formatStr = "xmlSet";
  10037. break;
  10038. case ResultFormatCsv:
  10039. formatStr = "csv";
  10040. break;
  10041. default:
  10042. existingFormat = nullptr;
  10043. p->removeProp("@format");
  10044. break;
  10045. }
  10046. if (format)
  10047. {
  10048. if (existingFormat)
  10049. {
  10050. if (0 != stricmp(formatStr, existingFormat))
  10051. throw MakeStringException(WUERR_ResultFormatMismatch, "addResult format %s, does not match existing format %s", formatStr, existingFormat);
  10052. }
  10053. else
  10054. p->setProp("@format", formatStr);
  10055. }
  10056. }
  10057. void CLocalWUResult::setResultFetchSize(unsigned rows)
  10058. {
  10059. p->setPropInt("fetchSize", rows);
  10060. }
  10061. void CLocalWUResult::setResultTotalRowCount(__int64 rows)
  10062. {
  10063. p->setPropInt64("totalRowCount", rows);
  10064. }
  10065. void CLocalWUResult::setResultRowCount(__int64 rows)
  10066. {
  10067. p->setPropInt64("rowCount", rows);
  10068. }
  10069. void CLocalWUResult::setResultDataset(const char *ecl, const char *defs)
  10070. {
  10071. p->setProp("datasetEcl", ecl);
  10072. p->setProp("datasetEclDefs", defs);
  10073. }
  10074. void CLocalWUResult::setResultLogicalName(const char *logicalName)
  10075. {
  10076. p->setProp("logicalName", logicalName);
  10077. }
  10078. void CLocalWUResult::setResultKeyField(const char *ecl)
  10079. {
  10080. p->setProp("keyField", ecl);
  10081. }
  10082. void CLocalWUResult::setResultRequestedRows(unsigned rows)
  10083. {
  10084. p->setPropInt("requestedRows", rows);
  10085. }
  10086. void CLocalWUResult::setResultRecordSizeEntry(const char * entry)
  10087. {
  10088. p->setProp("@recordSizeEntry", entry);
  10089. }
  10090. void CLocalWUResult::setResultTransformerEntry(const char * entry)
  10091. {
  10092. p->setProp("@transformerEntry", entry);
  10093. }
  10094. void CLocalWUResult::setResultRowLimit(__int64 value)
  10095. {
  10096. p->setPropInt64("@rowLimit", value);
  10097. }
  10098. void CLocalWUResult::setResultFilename(const char * name)
  10099. {
  10100. p->setProp("@tempFilename", name);
  10101. }
  10102. // MORE - it's an undetected error if we call getResult... of a type that does not match schema
  10103. __int64 CLocalWUResult::getResultInt() const
  10104. {
  10105. __int64 result = 0;
  10106. MemoryBuffer s;
  10107. p->getPropBin("Value", s);
  10108. if (s.length())
  10109. s.read(result);
  10110. else
  10111. {
  10112. // NOTE - we use this rather than getPropInt64 since it handles uint64 values up to MAX_UINT better (for our purposes)
  10113. const char *val = p->queryProp("xmlValue");
  10114. if (val)
  10115. result = rtlStrToInt8(strlen(val), val);
  10116. }
  10117. return result;
  10118. }
  10119. bool CLocalWUResult::getResultBool() const
  10120. {
  10121. bool result = false;
  10122. MemoryBuffer s;
  10123. p->getPropBin("Value", s);
  10124. if (s.length())
  10125. s.read(result);
  10126. else
  10127. result = p->getPropBool("xmlValue");
  10128. return result;
  10129. }
  10130. double CLocalWUResult::getResultReal() const
  10131. {
  10132. double result = 0;
  10133. MemoryBuffer s;
  10134. p->getPropBin("Value", s);
  10135. if (s.length())
  10136. s.read(result);
  10137. else
  10138. {
  10139. const char *xmlVal = p->queryProp("xmlValue");
  10140. if (xmlVal)
  10141. result = atof(xmlVal);
  10142. }
  10143. return result;
  10144. }
  10145. void CLocalWUResult::getResultDecimal(void * val, unsigned len, unsigned precision, bool isSigned) const
  10146. {
  10147. MemoryBuffer s;
  10148. p->getPropBin("Value", s);
  10149. if (s.length())
  10150. {
  10151. assertex(s.length() == len);
  10152. s.read(len, val);
  10153. }
  10154. else
  10155. {
  10156. const char *xmlVal = p->queryProp("xmlValue");
  10157. if (xmlVal)
  10158. {
  10159. Decimal d;
  10160. d.setString(strlen(xmlVal), xmlVal);
  10161. if (isSigned)
  10162. d.getDecimal(len, precision, val);
  10163. else
  10164. d.getUDecimal(len, precision, val);
  10165. }
  10166. else
  10167. memset(val, 0, len);
  10168. }
  10169. }
  10170. IStringVal& CLocalWUResult::getResultString(IStringVal & str, bool hidePassword) const
  10171. {
  10172. if (hidePassword && p->getPropBool("@password"))
  10173. {
  10174. str.set("****");
  10175. return str;
  10176. }
  10177. MemoryBuffer s;
  10178. p->getPropBin("Value", s);
  10179. if (s.length())
  10180. {
  10181. unsigned len;
  10182. s.read(len);
  10183. str.setLen((const char *) s.readDirect(len), len);
  10184. }
  10185. else
  10186. {
  10187. p->getPropBin("xmlValue", s);
  10188. if (p->isBinary("xmlValue"))
  10189. str.setLen(s.toByteArray(), s.length());
  10190. else
  10191. {
  10192. char *ascii = rtlUtf8ToVStr(rtlUtf8Length(s.length(), s.toByteArray()), s.toByteArray());
  10193. str.set(ascii);
  10194. rtlFree(ascii);
  10195. }
  10196. }
  10197. return str;
  10198. }
  10199. WUResultFormat CLocalWUResult::getResultFormat() const
  10200. {
  10201. const char * format = p->queryProp("@format");
  10202. if (!format)
  10203. return ResultFormatRaw;
  10204. else if (strcmp(format, "xml") == 0)
  10205. return ResultFormatXml;
  10206. else if (strcmp(format, "xmlSet") == 0)
  10207. return ResultFormatXmlSet;
  10208. else if (strcmp(format, "csv") == 0)
  10209. return ResultFormatCsv;
  10210. else
  10211. throw MakeStringException(WUERR_InvalidResultFormat, "Unrecognised result format %s", format);
  10212. }
  10213. IDataVal& CLocalWUResult::getResultRaw(IDataVal & data, IXmlToRawTransformer * xmlTransformer, ICsvToRawTransformer * csvTransformer) const
  10214. {
  10215. MemoryBuffer s;
  10216. p->getPropBin("Value", s);
  10217. unsigned len = s.length();
  10218. if (len)
  10219. {
  10220. WUResultFormat format = getResultFormat();
  10221. if (format == ResultFormatXml || format == ResultFormatXmlSet)
  10222. {
  10223. if (!xmlTransformer)
  10224. throw MakeStringException(WUERR_MissingFormatTranslator, "No transformer supplied to translate XML format result");
  10225. xmlTransformer->transform(data, len, s.readDirect(len), format == ResultFormatXml);
  10226. }
  10227. else if (format == ResultFormatCsv)
  10228. {
  10229. if (!csvTransformer)
  10230. throw MakeStringException(WUERR_MissingFormatTranslator, "No transformer supplied to translate Csv format result");
  10231. csvTransformer->transform(data, len, s.readDirect(len), true);
  10232. }
  10233. else
  10234. data.setLen(s.readDirect(len), len);
  10235. }
  10236. else
  10237. data.clear();
  10238. return data;
  10239. }
  10240. unsigned CLocalWUResult::getResultHash() const
  10241. {
  10242. MemoryBuffer s;
  10243. p->getPropBin("Value", s);
  10244. unsigned len = s.length();
  10245. const byte * data = (const byte *)s.toByteArray();
  10246. return ~hashc(data, len, ~0);
  10247. }
  10248. IDataVal& CLocalWUResult::getResultUnicode(IDataVal & data) const
  10249. {
  10250. MemoryBuffer s;
  10251. p->getPropBin("Value", s);
  10252. if (s.length())
  10253. {
  10254. unsigned len;
  10255. s.read(len);
  10256. data.setLen(s.readDirect(len*2), len*2);
  10257. }
  10258. else
  10259. {
  10260. StringBuffer utf8;
  10261. if (p->getProp("xmlValue", utf8))
  10262. {
  10263. unsigned outlen;
  10264. UChar *out;
  10265. rtlUtf8ToUnicodeX(outlen, out, utf8.length(), utf8.str());
  10266. data.setLen(out, outlen*2);
  10267. rtlFree(out);
  10268. }
  10269. else
  10270. data.clear();
  10271. }
  10272. return data;
  10273. }
  10274. __int64 CLocalWUResult::getResultRawSize(IXmlToRawTransformer * xmlTransformer, ICsvToRawTransformer * csvTransformer) const
  10275. {
  10276. WUResultFormat format = getResultFormat();
  10277. if (format == ResultFormatRaw)
  10278. {
  10279. //MORE: This should not load the whole property...
  10280. MemoryBuffer s;
  10281. p->getPropBin("Value", s);
  10282. return s.length();
  10283. }
  10284. else
  10285. {
  10286. MemoryBuffer temp;
  10287. MemoryBuffer2IDataVal adaptor(temp);
  10288. getResultRaw(adaptor, xmlTransformer, csvTransformer);
  10289. return temp.length();
  10290. }
  10291. }
  10292. IDataVal& CLocalWUResult::getResultRaw(IDataVal & data, __int64 from, __int64 length, IXmlToRawTransformer * xmlTransformer, ICsvToRawTransformer * csvTransformer) const
  10293. {
  10294. WUResultFormat format = getResultFormat();
  10295. if (format != ResultFormatRaw)
  10296. {
  10297. MemoryBuffer temp;
  10298. MemoryBuffer2IDataVal adaptor(temp);
  10299. getResultRaw(adaptor, xmlTransformer, csvTransformer);
  10300. unsigned len = temp.length();
  10301. if (from > len) from = len;
  10302. if (from + length > len) length = len - from;
  10303. data.setLen(temp.readDirect(len) + from, (size32_t)length);
  10304. return data;
  10305. }
  10306. else
  10307. {
  10308. //MORE: This should not load the whole property, and should be different from the code above...
  10309. MemoryBuffer s;
  10310. p->getPropBin("Value", s);
  10311. unsigned len = s.length();
  10312. if (from > len) from = len;
  10313. if (from + length > len) length = len - from;
  10314. data.setLen(s.readDirect(len) + from, (size32_t)length);
  10315. return data;
  10316. }
  10317. }
  10318. bool CLocalWUResult::getResultIsAll() const
  10319. {
  10320. return p->getPropBool("@isAll", false);
  10321. }
  10322. // MORE - it's an undetected error if we call setResult... of a type that does not match schema
  10323. void CLocalWUResult::setResultInt(__int64 val)
  10324. {
  10325. // Note: we always serialize scalar integer results as int8, and schema must reflect this
  10326. MemoryBuffer m;
  10327. serializeInt8(val, m);
  10328. p->setPropBin("Value", m.length(), m.toByteArray());
  10329. setResultRowCount(1);
  10330. setResultTotalRowCount(1);
  10331. }
  10332. void CLocalWUResult::setResultUInt(unsigned __int64 val)
  10333. {
  10334. setResultInt((__int64) val);
  10335. }
  10336. void CLocalWUResult::setResultReal(double val)
  10337. {
  10338. // Note: we always serialize scalar real results as real8, and schema must reflect this
  10339. MemoryBuffer m;
  10340. serializeReal8(val, m);
  10341. p->setPropBin("Value", m.length(), m.toByteArray());
  10342. setResultRowCount(1);
  10343. setResultTotalRowCount(1);
  10344. }
  10345. void CLocalWUResult::setResultBool(bool val)
  10346. {
  10347. MemoryBuffer m;
  10348. serializeBool(val, m);
  10349. p->setPropBin("Value", m.length(), m.toByteArray());
  10350. setResultRowCount(1);
  10351. setResultTotalRowCount(1);
  10352. }
  10353. void CLocalWUResult::setResultString(const char *val, unsigned len)
  10354. {
  10355. // Note: we always serialize scalar strings with length prefix, and schema must reflect this
  10356. MemoryBuffer m;
  10357. serializeLPString(len, val, m);
  10358. p->setPropBin("Value", m.length(), m.toByteArray());
  10359. setResultRowCount(1);
  10360. setResultTotalRowCount(1);
  10361. }
  10362. void CLocalWUResult::setResultUnicode(const void *val, unsigned len)
  10363. {
  10364. // Note: we always serialize scalar strings with length prefix, and schema must reflect this
  10365. MemoryBuffer m;
  10366. m.append(len).append(len*2, val);
  10367. p->setPropBin("Value", m.length(), m.toByteArray());
  10368. setResultRowCount(1);
  10369. setResultTotalRowCount(1);
  10370. }
  10371. void CLocalWUResult::setResultData(const void *val, unsigned len)
  10372. {
  10373. // Note: we always serialize scalar data with length prefix, and schema must reflect this
  10374. MemoryBuffer m;
  10375. serializeLPString(len, (const char *)val, m);
  10376. p->setPropBin("Value", m.length(), m.toByteArray());
  10377. setResultRowCount(1);
  10378. setResultTotalRowCount(1);
  10379. }
  10380. void CLocalWUResult::setResultDecimal(const void *val, unsigned len)
  10381. {
  10382. // Note: serialized as data but with length known from schema
  10383. MemoryBuffer m;
  10384. serializeFixedData(len, val, m);
  10385. p->setPropBin("Value", m.length(), m.toByteArray());
  10386. setResultRowCount(1);
  10387. setResultTotalRowCount(1);
  10388. }
  10389. void CLocalWUResult::setResultRow(unsigned len, const void * data)
  10390. {
  10391. p->setPropBin("Value", len, data);
  10392. setResultRowCount(1);
  10393. setResultTotalRowCount(1);
  10394. setResultFormat(ResultFormatRaw);
  10395. }
  10396. void CLocalWUResult::setResultIsAll(bool value)
  10397. {
  10398. p->setPropBool("@isAll", value);
  10399. }
  10400. //==========================================================================================
  10401. CLocalWUPlugin::CLocalWUPlugin(IPropertyTree *props) : p(props)
  10402. {
  10403. }
  10404. IStringVal& CLocalWUPlugin::getPluginName(IStringVal &str) const
  10405. {
  10406. str.set(p->queryProp("@dllname"));
  10407. return str;
  10408. }
  10409. IStringVal& CLocalWUPlugin::getPluginVersion(IStringVal &str) const
  10410. {
  10411. str.set(p->queryProp("@version"));
  10412. return str;
  10413. }
  10414. void CLocalWUPlugin::setPluginName(const char *str)
  10415. {
  10416. p->setProp("@dllname", str);
  10417. }
  10418. void CLocalWUPlugin::setPluginVersion(const char *str)
  10419. {
  10420. p->setProp("@version", str);
  10421. }
  10422. //==========================================================================================
  10423. CLocalWULibrary::CLocalWULibrary(IPropertyTree *props) : p(props)
  10424. {
  10425. }
  10426. IStringVal& CLocalWULibrary::getName(IStringVal &str) const
  10427. {
  10428. str.set(p->queryProp("@name"));
  10429. return str;
  10430. }
  10431. void CLocalWULibrary::setName(const char *str)
  10432. {
  10433. p->setProp("@name", str);
  10434. }
  10435. //==========================================================================================
  10436. CLocalWUException::CLocalWUException(IPropertyTree *props) : p(props)
  10437. {
  10438. }
  10439. IStringVal& CLocalWUException::getExceptionSource(IStringVal &str) const
  10440. {
  10441. str.set(p->queryProp("@source"));
  10442. return str;
  10443. }
  10444. IStringVal& CLocalWUException::getExceptionMessage(IStringVal &str) const
  10445. {
  10446. str.set(p->queryProp(NULL));
  10447. return str;
  10448. }
  10449. unsigned CLocalWUException::getExceptionCode() const
  10450. {
  10451. return p->getPropInt("@code", 0);
  10452. }
  10453. ErrorSeverity CLocalWUException::getSeverity() const
  10454. {
  10455. return (ErrorSeverity)p->getPropInt("@severity", SeverityError);
  10456. }
  10457. IStringVal & CLocalWUException::getTimeStamp(IStringVal & dt) const
  10458. {
  10459. dt.set(p->queryProp("@time"));
  10460. return dt;
  10461. }
  10462. IStringVal & CLocalWUException::getExceptionFileName(IStringVal & str) const
  10463. {
  10464. str.set(p->queryProp("@filename"));
  10465. return str;
  10466. }
  10467. unsigned CLocalWUException::getExceptionLineNo() const
  10468. {
  10469. return p->getPropInt("@row", 0);
  10470. }
  10471. unsigned CLocalWUException::getExceptionColumn() const
  10472. {
  10473. return p->getPropInt("@col", 0);
  10474. }
  10475. unsigned CLocalWUException::getActivityId() const
  10476. {
  10477. const char * scope = queryScope();
  10478. if (scope)
  10479. {
  10480. const char * colon = strrchr(scope, ':');
  10481. if (colon && hasPrefix(colon+1, ActivityScopePrefix, true))
  10482. return atoi(colon+1+strlen(ActivityScopePrefix));
  10483. }
  10484. return p->getPropInt("@activity", 0);
  10485. }
  10486. unsigned CLocalWUException::getSequence() const
  10487. {
  10488. return p->getPropInt("@sequence", 0);
  10489. }
  10490. const char * CLocalWUException::queryScope() const
  10491. {
  10492. return p->queryProp("@scope");
  10493. }
  10494. unsigned CLocalWUException::getPriority() const
  10495. {
  10496. return p->getPropInt("@prio", 0);
  10497. }
  10498. void CLocalWUException::setExceptionSource(const char *str)
  10499. {
  10500. p->setProp("@source", str);
  10501. }
  10502. void CLocalWUException::setExceptionMessage(const char *str)
  10503. {
  10504. p->setProp(NULL, str);
  10505. }
  10506. void CLocalWUException::setExceptionCode(unsigned code)
  10507. {
  10508. p->setPropInt("@code", code);
  10509. }
  10510. void CLocalWUException::setSeverity(ErrorSeverity level)
  10511. {
  10512. p->setPropInt("@severity", level);
  10513. }
  10514. void CLocalWUException::setTimeStamp(const char *str)
  10515. {
  10516. p->setProp("@time", str);
  10517. }
  10518. void CLocalWUException::setExceptionFileName(const char *str)
  10519. {
  10520. p->setProp("@filename", str);
  10521. }
  10522. void CLocalWUException::setExceptionLineNo(unsigned r)
  10523. {
  10524. p->setPropInt("@row", r);
  10525. }
  10526. void CLocalWUException::setExceptionColumn(unsigned c)
  10527. {
  10528. p->setPropInt("@col", c);
  10529. }
  10530. void CLocalWUException::setActivityId(unsigned _id)
  10531. {
  10532. p->setPropInt("@activity", _id);
  10533. }
  10534. void CLocalWUException::setScope(const char * _scope)
  10535. {
  10536. p->setProp("@scope", _scope);
  10537. }
  10538. void CLocalWUException::setPriority(unsigned _priority)
  10539. {
  10540. p->setPropInt("@prio", _priority);
  10541. }
  10542. //==========================================================================================
  10543. CLocalWUAppValue::CLocalWUAppValue(const IPropertyTree *_owner, const IPropertyTree *_props) : owner(_owner), props(_props)
  10544. {
  10545. }
  10546. const char * CLocalWUAppValue::queryApplication() const
  10547. {
  10548. return owner->queryName();
  10549. }
  10550. const char * CLocalWUAppValue::queryName() const
  10551. {
  10552. return props->queryName();
  10553. }
  10554. const char * CLocalWUAppValue::queryValue() const
  10555. {
  10556. return props->queryProp(nullptr);
  10557. }
  10558. //==========================================================================================
  10559. CLocalWUStatistic::CLocalWUStatistic(const IPropertyTree *props) : p(props)
  10560. {
  10561. }
  10562. IStringVal & CLocalWUStatistic::getCreator(IStringVal & str) const
  10563. {
  10564. const char * creator = p->queryProp("@creator");
  10565. str.set(creator);
  10566. return str;
  10567. }
  10568. IStringVal & CLocalWUStatistic::getDescription(IStringVal & str, bool createDefault) const
  10569. {
  10570. const char * desc = p->queryProp("@desc");
  10571. if (desc)
  10572. {
  10573. str.set(desc); // legacy and in case it is overridden
  10574. }
  10575. else if (createDefault)
  10576. {
  10577. StatisticKind kind = getKind();
  10578. assertex(kind != StKindNone);
  10579. const char * scope = p->queryProp("@scope");
  10580. assertex(scope);
  10581. //Clean up the format of the scope when converting it to a description
  10582. StringBuffer descriptionText;
  10583. if (isGlobalScope(scope))
  10584. {
  10585. const char * creator = p->queryProp("@creator");
  10586. descriptionText.append(creator).append(":");
  10587. queryLongStatisticName(descriptionText, kind);
  10588. }
  10589. else
  10590. {
  10591. for (;;)
  10592. {
  10593. char c = *scope++;
  10594. if (!c)
  10595. break;
  10596. if (c == ':')
  10597. descriptionText.append(": ");
  10598. else
  10599. descriptionText.append(c);
  10600. }
  10601. if (kind != StTimeElapsed)
  10602. queryLongStatisticName(descriptionText.append(": "), kind);
  10603. }
  10604. str.set(descriptionText);
  10605. }
  10606. else
  10607. str.clear();
  10608. return str;
  10609. }
  10610. IStringVal & CLocalWUStatistic::getFormattedValue(IStringVal & str) const
  10611. {
  10612. StringBuffer formatted;
  10613. formatStatistic(formatted, getValue(), getMeasure());
  10614. str.set(formatted);
  10615. return str;
  10616. }
  10617. StatisticCreatorType CLocalWUStatistic::getCreatorType() const
  10618. {
  10619. return queryCreatorType(p->queryProp("@c"), SCTnone);
  10620. }
  10621. StatisticScopeType CLocalWUStatistic::getScopeType() const
  10622. {
  10623. return queryScopeType(p->queryProp("@s"), SSTnone);
  10624. }
  10625. StatisticKind CLocalWUStatistic::getKind() const
  10626. {
  10627. return queryStatisticKind(p->queryProp("@kind"), StKindNone);
  10628. }
  10629. const char * CLocalWUStatistic::queryScope() const
  10630. {
  10631. const char * scope = p->queryProp("@scope");
  10632. if (!scope || streq(scope, LEGACY_GLOBAL_SCOPE))
  10633. scope = GLOBAL_SCOPE;
  10634. return scope;
  10635. }
  10636. StatisticMeasure CLocalWUStatistic::getMeasure() const
  10637. {
  10638. return queryMeasure(p->queryProp("@unit"), SMeasureNone);
  10639. }
  10640. unsigned __int64 CLocalWUStatistic::getValue() const
  10641. {
  10642. return p->getPropInt64("@value", 0);
  10643. }
  10644. unsigned __int64 CLocalWUStatistic::getCount() const
  10645. {
  10646. return p->getPropInt64("@count", 0);
  10647. }
  10648. unsigned __int64 CLocalWUStatistic::getMax() const
  10649. {
  10650. return p->getPropInt64("@max", 0);
  10651. }
  10652. unsigned __int64 CLocalWUStatistic::getTimestamp() const
  10653. {
  10654. return p->getPropInt64("@ts", 0);
  10655. }
  10656. //==========================================================================================
  10657. extern WORKUNIT_API ILocalWorkUnit * createLocalWorkUnit(const char *xml)
  10658. {
  10659. Owned<CLocalWorkUnit> cw = new CLocalWorkUnit((ISecManager *) NULL, NULL);
  10660. if (xml)
  10661. cw->loadPTree(createPTreeFromXMLString(xml, ipt_lowmem));
  10662. else
  10663. {
  10664. Owned<IPropertyTree> p = createPTree("W_LOCAL", ipt_lowmem);
  10665. p->setProp("@xmlns:xsi", "http://www.w3.org/1999/XMLSchema-instance");
  10666. cw->loadPTree(p.getClear());
  10667. }
  10668. ILocalWorkUnit* ret = QUERYINTERFACE(&cw->lockRemote(false), ILocalWorkUnit);
  10669. return ret;
  10670. }
  10671. extern WORKUNIT_API ILocalWorkUnit * createLocalWorkUnitFromPTree(IPropertyTree *ptree)
  10672. {
  10673. Owned<CLocalWorkUnit> cw = new CLocalWorkUnit((ISecManager *) NULL, NULL);
  10674. cw->loadPTree(ptree);
  10675. ILocalWorkUnit* ret = QUERYINTERFACE(&cw->lockRemote(false), ILocalWorkUnit);
  10676. return ret;
  10677. }
  10678. void exportWorkUnitToXMLWithHiddenPasswords(IPropertyTree *p, IIOStream &out, unsigned extraXmlFlags)
  10679. {
  10680. const char *name = p->queryName();
  10681. if (!name)
  10682. name = "__unnamed__";
  10683. StringBuffer temp;
  10684. writeStringToStream(out, appendPTreeOpenTag(temp, p, name, 1, true));
  10685. Owned<IPropertyTreeIterator> elems = p->getElements("*", iptiter_sort);
  10686. ForEach(*elems)
  10687. {
  10688. IPropertyTree &elem = elems->query();
  10689. if (streq(elem.queryName(), "Parameters"))
  10690. {
  10691. writeStringToStream(out, appendPTreeOpenTag(temp.clear().append(' '), &elem, "Parameters", 2, false).append('\n'));
  10692. Owned<IPropertyTreeIterator> params = elem.getElements("*", iptiter_sort);
  10693. ForEach(*params)
  10694. {
  10695. IPropertyTree &param = params->query();
  10696. const char *paramname = param.queryName();
  10697. VStringBuffer xpath("Variables/Variable[@name='%s']/Format/@password", paramname);
  10698. if (p->getPropBool(xpath))
  10699. writeStringToStream(out, appendXMLTag(temp.clear().append(" "), paramname, "****").append('\n'));
  10700. else
  10701. {
  10702. toXML(&param, out, 2, XML_Format|XML_SortTags|extraXmlFlags);
  10703. }
  10704. }
  10705. writeStringToStream(out, appendXMLCloseTag(temp.clear().append(' '), "Parameters").append('\n'));
  10706. }
  10707. else if (streq(elem.queryName(), "Variables"))
  10708. {
  10709. writeStringToStream(out, appendPTreeOpenTag(temp.clear().append(' '), &elem, "Variables", 2, false).append('\n'));
  10710. Owned<IPropertyTreeIterator> vars = elem.getElements("*", iptiter_sort);
  10711. ForEach(*vars)
  10712. {
  10713. Owned<IPropertyTree> var = LINK(&vars->query());
  10714. if (var->getPropBool("Format/@password"))
  10715. {
  10716. var.setown(createPTreeFromIPT(var)); //copy and remove password values
  10717. var->removeProp("Value");
  10718. var->removeProp("xmlValue");
  10719. }
  10720. toXML(var, out, 2, XML_Format|XML_SortTags|extraXmlFlags);
  10721. }
  10722. writeStringToStream(out, appendXMLCloseTag(temp.clear().append(' '), "Variables").append('\n'));
  10723. }
  10724. else
  10725. toXML(&elem, out, 1, XML_Format|XML_SortTags|extraXmlFlags);
  10726. }
  10727. writeStringToStream(out, appendXMLCloseTag(temp.clear(), name));
  10728. }
  10729. StringBuffer &exportWorkUnitToXMLWithHiddenPasswords(IPropertyTree *p, StringBuffer &str)
  10730. {
  10731. class CAdapter : public CInterface, implements IIOStream
  10732. {
  10733. StringBuffer &out;
  10734. public:
  10735. IMPLEMENT_IINTERFACE;
  10736. CAdapter(StringBuffer &_out) : out(_out) { }
  10737. virtual void flush() { }
  10738. virtual size32_t read(size32_t len, void * data) { UNIMPLEMENTED; return 0; }
  10739. virtual size32_t write(size32_t len, const void * data) { out.append(len, (const char *)data); return len; }
  10740. } adapter(str);
  10741. exportWorkUnitToXMLWithHiddenPasswords(p->queryBranch(NULL), adapter, 0);
  10742. return str;
  10743. }
  10744. void exportWorkUnitToXMLFileWithHiddenPasswords(IPropertyTree *p, const char *filename, unsigned extraXmlFlags)
  10745. {
  10746. OwnedIFile ifile = createIFile(filename);
  10747. OwnedIFileIO ifileio = ifile->open(IFOcreate);
  10748. Owned<IIOStream> stream = createIOStream(ifileio);
  10749. exportWorkUnitToXMLWithHiddenPasswords(p->queryBranch(NULL), *stream, extraXmlFlags);
  10750. }
  10751. extern WORKUNIT_API StringBuffer &exportWorkUnitToXML(const IConstWorkUnit *wu, StringBuffer &str, bool unpack, bool includeProgress, bool hidePasswords)
  10752. {
  10753. // MORE - queryPTree isn't really safe without holding CLocalWorkUnit::crit - really need to move these functions into CLocalWorkunit
  10754. const IExtendedWUInterface *ewu = queryExtendedWU(wu);
  10755. if (ewu)
  10756. {
  10757. Linked<IPropertyTree> p;
  10758. if (unpack||includeProgress)
  10759. p.setown(ewu->getUnpackedTree(includeProgress));
  10760. else
  10761. p.set(ewu->queryPTree());
  10762. if (hidePasswords)
  10763. return exportWorkUnitToXMLWithHiddenPasswords(p, str);
  10764. else
  10765. return toXML(p, str, 0, XML_Format|XML_SortTags);
  10766. }
  10767. else
  10768. return str.append("Unrecognized workunit format");
  10769. }
  10770. extern WORKUNIT_API void exportWorkUnitToXMLFile(const IConstWorkUnit *wu, const char * filename, unsigned extraXmlFlags, bool unpack, bool includeProgress, bool hidePasswords, bool regressionTest)
  10771. {
  10772. const IExtendedWUInterface *ewu = queryExtendedWU(wu);
  10773. if (ewu)
  10774. {
  10775. Linked<IPropertyTree> p;
  10776. if (unpack||includeProgress)
  10777. p.setown(ewu->getUnpackedTree(includeProgress));
  10778. else if (regressionTest)
  10779. p.setown(createPTreeFromIPT(ewu->queryPTree()));
  10780. else
  10781. p.set(ewu->queryPTree());
  10782. if (hidePasswords)
  10783. return exportWorkUnitToXMLFileWithHiddenPasswords(p, filename, extraXmlFlags);
  10784. if (regressionTest)
  10785. {
  10786. //This removes any items from the xml that will vary from run to run, so they can be binary compared from run to run
  10787. //The following attributes change with the build. Simpler to remove rather than needing to updated each build
  10788. p->removeProp("@buildVersion");
  10789. p->removeProp("@eclVersion");
  10790. p->removeProp("@hash");
  10791. //Remove statistics, and extract them to a separate file
  10792. IPropertyTree * stats = p->queryPropTree("Statistics");
  10793. if (stats)
  10794. {
  10795. StringBuffer statsFilename;
  10796. statsFilename.append(filename).append(".stats");
  10797. saveXML(statsFilename, stats, 0, (XML_Format|XML_SortTags|extraXmlFlags) & ~XML_LineBreakAttributes);
  10798. p->removeProp("Statistics");
  10799. }
  10800. //Now remove timestamps from exceptions
  10801. Owned<IPropertyTreeIterator> elems = p->getElements("Exceptions/Exception", iptiter_sort);
  10802. ForEach(*elems)
  10803. {
  10804. IPropertyTree &elem = elems->query();
  10805. elem.removeProp("@time");
  10806. }
  10807. }
  10808. saveXML(filename, p, 0, XML_Format|XML_SortTags|extraXmlFlags);
  10809. }
  10810. else
  10811. throw makeStringException(0, "Unrecognized workunit format");
  10812. }
  10813. extern WORKUNIT_API void submitWorkUnit(const char *wuid, const char *username, const char *password)
  10814. {
  10815. MemoryBuffer buffer;
  10816. Owned<INamedQueueConnection> conn = createNamedQueueConnection(0); // MORE - security token?
  10817. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  10818. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid);
  10819. assertex(cw);
  10820. StringAttr clusterName(cw->queryClusterName());
  10821. cw.clear();
  10822. if (!clusterName.length())
  10823. throw MakeStringException(WUERR_InvalidCluster, "No target cluster specified");
  10824. StringBuffer serverQueue;
  10825. getClusterEclCCServerQueueName(serverQueue, clusterName);
  10826. assertex(serverQueue.length());
  10827. Owned<IJobQueue> queue = createJobQueue(serverQueue.str());
  10828. if (!queue.get())
  10829. throw MakeStringException(WUERR_InvalidQueue, "Could not create workunit queue");
  10830. IJobQueueItem *item = createJobQueueItem(wuid);
  10831. queue->enqueue(item);
  10832. }
  10833. extern WORKUNIT_API void abortWorkUnit(const char *wuid)
  10834. {
  10835. StringBuffer xpath("/WorkUnitAborts/");
  10836. xpath.append(wuid);
  10837. Owned<IRemoteConnection> acon = querySDS().connect(xpath.str(), myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE, SDS_LOCK_TIMEOUT);
  10838. acon->queryRoot()->setPropInt(NULL, 1);
  10839. }
  10840. extern WORKUNIT_API void secSubmitWorkUnit(const char *wuid, ISecManager &secmgr, ISecUser &secuser)
  10841. {
  10842. if (checkWuSecAccess(wuid, &secmgr, &secuser, SecAccess_Write, "Submit", true, true))
  10843. submitWorkUnit(wuid, secuser.getName(), secuser.credentials().getPassword());
  10844. }
  10845. extern WORKUNIT_API void secAbortWorkUnit(const char *wuid, ISecManager &secmgr, ISecUser &secuser)
  10846. {
  10847. if (!checkWuSecAccess(wuid, &secmgr, &secuser, SecAccess_Write, "Submit", true, true))
  10848. return;
  10849. abortWorkUnit(wuid);
  10850. Owned<IConstWorkUnit> cw = globalFactory->openWorkUnit(wuid);
  10851. if(!cw)
  10852. return;
  10853. WorkunitUpdate wu(&cw->lock());
  10854. const char *abortBy = secuser.getName();
  10855. if (abortBy && *abortBy)
  10856. wu->setTracingValue("AbortBy", abortBy);
  10857. wu->setTracingValueInt64("AbortTimeStamp", getTimeStampNowValue());
  10858. }
  10859. extern WORKUNIT_API void submitWorkUnit(const char *wuid, ISecManager *secmgr, ISecUser *secuser)
  10860. {
  10861. if (secmgr && secuser)
  10862. return secSubmitWorkUnit(wuid, *secmgr, *secuser);
  10863. if (secuser)
  10864. return submitWorkUnit(wuid, secuser->getName(), secuser->credentials().getPassword());
  10865. submitWorkUnit(wuid, "", "");
  10866. }
  10867. extern WORKUNIT_API void abortWorkUnit(const char *wuid, ISecManager *secmgr, ISecUser *secuser)
  10868. {
  10869. if (secmgr && secuser)
  10870. return secAbortWorkUnit(wuid, *secmgr, *secuser);
  10871. abortWorkUnit(wuid);
  10872. }
  10873. bool CLocalWorkUnit::hasWorkflow() const
  10874. {
  10875. return p->hasProp("Workflow");
  10876. }
  10877. unsigned CLocalWorkUnit::queryEventScheduledCount() const
  10878. {
  10879. CriticalBlock block(crit);
  10880. if (p->hasProp("@eventScheduledCount"))
  10881. return p->getPropInt("@eventScheduledCount", 0);
  10882. else
  10883. return p->getPropInt("Workflow/@eventScheduledCount", 0); // Legacy location for this setting
  10884. }
  10885. void CLocalWorkUnit::incEventScheduledCount()
  10886. {
  10887. CriticalBlock block(crit);
  10888. p->setPropInt("@eventScheduledCount", queryEventScheduledCount()+1);
  10889. }
  10890. IPropertyTree * CLocalWorkUnit::queryWorkflowTree() const
  10891. {
  10892. CriticalBlock block(crit);
  10893. return p->queryPropTree("Workflow");
  10894. }
  10895. IConstWorkflowItemIterator* CLocalWorkUnit::getWorkflowItems() const
  10896. {
  10897. // For this to be legally called, we must have the read-able interface. So we are already locked for (at least) read.
  10898. CriticalBlock block(crit);
  10899. if(!workflowIteratorCached)
  10900. {
  10901. assertex(!workflowIterator);
  10902. Owned<IPropertyTree> s = p->getPropTree("Workflow");
  10903. if(s)
  10904. workflowIterator.setown(createWorkflowItemIterator(s));
  10905. workflowIteratorCached = true;
  10906. }
  10907. return workflowIterator.getLink();
  10908. }
  10909. IWorkflowItemArray * CLocalWorkUnit::getWorkflowClone() const
  10910. {
  10911. unsigned count = 0;
  10912. Owned<IConstWorkflowItemIterator> iter = getWorkflowItems();
  10913. for(iter->first(); iter->isValid(); iter->next())
  10914. count++;
  10915. Owned<IWorkflowItemArray> array = createWorkflowItemArray(count);
  10916. for(iter->first(); iter->isValid(); iter->next())
  10917. array->addClone(iter->query());
  10918. return array.getLink();
  10919. }
  10920. IWorkflowItem * CLocalWorkUnit::addWorkflowItem(unsigned wfid, WFType type, WFMode mode, unsigned success, unsigned failure, unsigned recovery, unsigned retriesAllowed, unsigned contingencyFor)
  10921. {
  10922. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  10923. CriticalBlock block(crit);
  10924. workflowIterator.clear();
  10925. workflowIteratorCached = false;
  10926. IPropertyTree * s = p->queryPropTree("Workflow");
  10927. if(!s)
  10928. s = p->addPropTree("Workflow");
  10929. return createWorkflowItem(s, wfid, type, mode, success, failure, recovery, retriesAllowed, contingencyFor);
  10930. }
  10931. IWorkflowItemIterator * CLocalWorkUnit::updateWorkflowItems()
  10932. {
  10933. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  10934. CriticalBlock block(crit);
  10935. if(!workflowIterator)
  10936. {
  10937. IPropertyTree * s = p->queryPropTree("Workflow");
  10938. if(!s)
  10939. s = p->addPropTree("Workflow");
  10940. workflowIterator.setown(createWorkflowItemIterator(s));
  10941. workflowIteratorCached = true;
  10942. }
  10943. return workflowIterator.getLink();
  10944. }
  10945. void CLocalWorkUnit::syncRuntimeWorkflow(IWorkflowItemArray * array)
  10946. {
  10947. Owned<IWorkflowItemIterator> iter = updateWorkflowItems();
  10948. Owned<IWorkflowItem> item;
  10949. for(iter->first(); iter->isValid(); iter->next())
  10950. {
  10951. item.setown(iter->get());
  10952. item->syncRuntimeData(array->queryWfid(item->queryWfid()));
  10953. }
  10954. workflowIterator.clear();
  10955. workflowIteratorCached = false;
  10956. }
  10957. void CLocalWorkUnit::resetWorkflow()
  10958. {
  10959. if (hasWorkflow())
  10960. {
  10961. Owned<IWorkflowItemIterator> iter = updateWorkflowItems();
  10962. Owned<IWorkflowItem> wf;
  10963. for(iter->first(); iter->isValid(); iter->next())
  10964. {
  10965. wf.setown(iter->get());
  10966. wf->reset();
  10967. }
  10968. workflowIterator.clear();
  10969. workflowIteratorCached = false;
  10970. }
  10971. }
  10972. void CLocalWorkUnit::schedule()
  10973. {
  10974. CriticalBlock block(crit);
  10975. if(queryEventScheduledCount() == 0) return;
  10976. switch(getState())
  10977. {
  10978. case WUStateCompleted:
  10979. setState(WUStateWait);
  10980. break;
  10981. case WUStateFailed:
  10982. case WUStateArchived:
  10983. case WUStateAborting:
  10984. case WUStateAborted:
  10985. case WUStateScheduled:
  10986. throw MakeStringException(WUERR_CannotSchedule, "Cannot schedule workunit in this state");
  10987. }
  10988. StringBuffer rootPath;
  10989. rootPath.append("/Schedule/").append(queryClusterName());
  10990. Owned<IRemoteConnection> conn = querySDS().connect(rootPath.str(), myProcessSession(), RTM_LOCK_WRITE | RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  10991. Owned<IPropertyTree> root = conn->getRoot();
  10992. if(!root->hasChildren())
  10993. {
  10994. StringBuffer addPath;
  10995. addPath.append("/Schedulers/").append(queryClusterName());
  10996. Owned<IRemoteConnection> addConn = querySDS().connect(addPath.str(), myProcessSession(), RTM_LOCK_WRITE | RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  10997. }
  10998. char const * wuid = p->queryName();
  10999. StringBuffer xpath("*/*/");
  11000. ncnameEscape(wuid, xpath);
  11001. bool more;
  11002. do more = root->removeProp(xpath.str()); while(more);
  11003. Owned<IConstWorkflowItemIterator> iter = getWorkflowItems();
  11004. Owned<IWorkflowEvent> event;
  11005. Owned<IPropertyTree> branch1, branch2;
  11006. for(iter->first(); iter->isValid(); iter->next())
  11007. {
  11008. event.setown(iter->query()->getScheduleEvent());
  11009. if(!event) continue;
  11010. ncnameEscape(event->queryName(), xpath.clear());
  11011. ensurePTree(root, xpath.str());
  11012. branch1.setown(root->getPropTree(xpath.str()));
  11013. ncnameEscape(event->queryText(), xpath.clear());
  11014. ensurePTree(branch1, xpath.str());
  11015. branch2.setown(branch1->getPropTree(xpath.str()));
  11016. ncnameEscape(wuid, xpath.clear());
  11017. ensurePTree(branch2, xpath.str());
  11018. }
  11019. }
  11020. void CLocalWorkUnit::deschedule()
  11021. {
  11022. if(queryEventScheduledCount() == 0) return;
  11023. if(getState() == WUStateWait)
  11024. setState(WUStateCompleted);
  11025. doDescheduleWorkkunit(p->queryName());
  11026. }
  11027. EnumMapping localFileUploadTypes[] = {
  11028. { UploadTypeFileSpray, "FileSpray" },
  11029. { UploadTypeWUResult, "WUResult" },
  11030. { UploadTypeWUResultCsv, "WUResultCsv" },
  11031. { UploadTypeWUResultXml, "WUResultXml" },
  11032. { UploadTypeSize, NULL }
  11033. };
  11034. class CLocalFileUpload : public CInterface, implements IConstLocalFileUpload
  11035. {
  11036. public:
  11037. CLocalFileUpload(IPropertyTree * _tree) : tree(_tree) {}
  11038. CLocalFileUpload(unsigned id, LocalFileUploadType type, char const * source, char const * destination, char const * eventTag)
  11039. {
  11040. tree.setown(createPTree());
  11041. tree->setPropInt("@id", id);
  11042. setEnum(tree, "@type", type, localFileUploadTypes);
  11043. tree->setProp("@source", source);
  11044. tree->setProp("@destination", destination);
  11045. if (eventTag)
  11046. tree->setProp("@eventTag", eventTag);
  11047. }
  11048. IMPLEMENT_IINTERFACE;
  11049. IPropertyTree * getTree() { return tree.getLink(); }
  11050. virtual unsigned queryID() const { return tree->getPropInt("@id"); }
  11051. virtual LocalFileUploadType queryType() const { return (LocalFileUploadType)getEnum(tree, "@type", localFileUploadTypes); }
  11052. virtual IStringVal & getSource(IStringVal & ret) const { ret.set(tree->queryProp("@source")); return ret; }
  11053. virtual IStringVal & getDestination(IStringVal & ret) const { ret.set(tree->queryProp("@destination")); return ret; }
  11054. virtual IStringVal & getEventTag(IStringVal & ret) const { if(tree->hasProp("@eventTag")) ret.set(tree->queryProp("@eventTag")); else ret.clear(); return ret; }
  11055. private:
  11056. Owned<IPropertyTree> tree;
  11057. };
  11058. class CLocalFileUploadIterator : public CInterface, implements IConstLocalFileUploadIterator
  11059. {
  11060. public:
  11061. CLocalFileUploadIterator(IPropertyTree * _tree) : tree(_tree), iter(tree->getElements("LocalFileUpload")) {}
  11062. IMPLEMENT_IINTERFACE;
  11063. bool first() { return iter->first(); }
  11064. bool isValid() { return iter->isValid(); }
  11065. bool next() { return iter->next(); }
  11066. IConstLocalFileUpload * get() { return new CLocalFileUpload(&iter->get()); }
  11067. private:
  11068. Owned<IPropertyTree> tree;
  11069. Owned<IPropertyTreeIterator> iter;
  11070. };
  11071. IConstLocalFileUploadIterator * CLocalWorkUnit::getLocalFileUploads() const
  11072. {
  11073. // For this to be legally called, we must have the read-able interface. So we are already locked for (at least) read.
  11074. CriticalBlock block(crit);
  11075. Owned<IPropertyTree> s = p->getPropTree("LocalFileUploads");
  11076. if(s)
  11077. return new CLocalFileUploadIterator(s.getClear());
  11078. else
  11079. return NULL;
  11080. }
  11081. bool CLocalWorkUnit::requiresLocalFileUpload() const
  11082. {
  11083. SCMStringBuffer dest;
  11084. Owned<IConstWUResult> result;
  11085. Owned<IConstLocalFileUploadIterator> iter(getLocalFileUploads());
  11086. if(!iter)
  11087. return false;
  11088. for(iter->first(); iter->isValid(); iter->next())
  11089. {
  11090. Owned<IConstLocalFileUpload> upload(iter->get());
  11091. switch(upload->queryType())
  11092. {
  11093. case UploadTypeWUResult:
  11094. case UploadTypeWUResultCsv:
  11095. case UploadTypeWUResultXml:
  11096. upload->getDestination(dest);
  11097. result.setown(getResultByName(dest.str()));
  11098. if(!result)
  11099. return true;
  11100. break;
  11101. default:
  11102. throw MakeStringException(WUERR_InvalidUploadFormat, "Unsupported local file upload type %s", getEnumText(upload->queryType(), localFileUploadTypes));
  11103. }
  11104. }
  11105. return false;
  11106. }
  11107. unsigned CLocalWorkUnit::addLocalFileUpload(LocalFileUploadType type, char const * source, char const * destination, char const * eventTag)
  11108. {
  11109. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  11110. CriticalBlock block(crit);
  11111. IPropertyTree * s = p->queryPropTree("LocalFileUploads");
  11112. if(!s)
  11113. s = p->addPropTree("LocalFileUploads");
  11114. unsigned id = s->numChildren();
  11115. Owned<CLocalFileUpload> upload = new CLocalFileUpload(id, type, source, destination, eventTag);
  11116. s->addPropTree("LocalFileUpload", upload->getTree());
  11117. return id;
  11118. }
  11119. IStringVal & CLocalWorkUnit::getAbortBy(IStringVal & str) const
  11120. {
  11121. CriticalBlock block(crit);
  11122. str.set(p->queryProp("Tracing/AbortBy"));
  11123. return str;
  11124. }
  11125. unsigned __int64 CLocalWorkUnit::getAbortTimeStamp() const
  11126. {
  11127. CriticalBlock block(crit);
  11128. return p->getPropInt64("Tracing/AbortTimeStamp", 0);
  11129. }
  11130. cost_type CLocalWorkUnit::getExecuteCost() const
  11131. {
  11132. CriticalBlock block(crit);
  11133. return p->getPropInt64("@costExecute");
  11134. }
  11135. cost_type CLocalWorkUnit::getFileAccessCost() const
  11136. {
  11137. CriticalBlock block(crit);
  11138. return p->getPropInt64("@costFileAccess");
  11139. }
  11140. cost_type CLocalWorkUnit::getCompileCost() const
  11141. {
  11142. CriticalBlock block(crit);
  11143. return p->getPropInt64("@costCompile");
  11144. }
  11145. #if 0
  11146. void testConstWorkflow(IConstWorkflowItem * cwf, bool * okay, bool * dep)
  11147. {
  11148. DBGLOG("Test workflow const iface %u", cwf->queryWfid());
  11149. unsigned deps = 0;
  11150. Owned<IWorkflowDependencyIterator> diter;
  11151. switch(cwf->queryWfid())
  11152. {
  11153. case 1:
  11154. assertex(!cwf->isScheduled());
  11155. assertex(cwf->queryType() == WFTypeNormal);
  11156. assertex(cwf->queryState() == WFStateNull);
  11157. diter.setown(cwf->getDependencies());
  11158. for(diter->first(); diter->isValid(); diter->next())
  11159. deps++;
  11160. assertex(deps==0);
  11161. okay[0] = true;
  11162. break;
  11163. case 2:
  11164. assertex(!cwf->isScheduled());
  11165. assertex(cwf->queryType() == WFTypeRecovery);
  11166. assertex(cwf->queryState() == WFStateSkip);
  11167. okay[1] = true;
  11168. break;
  11169. case 3:
  11170. assertex(cwf->queryContingencyFor() == 4);
  11171. okay[2] = true;
  11172. break;
  11173. case 4:
  11174. assertex(cwf->isScheduled());
  11175. assertex(cwf->queryType() == WFTypeNormal);
  11176. assertex(cwf->queryState() == WFStateReqd);
  11177. assertex(cwf->querySuccess() == 0);
  11178. assertex(cwf->queryFailure() == 3);
  11179. assertex(cwf->queryRecovery() == 2);
  11180. assertex(cwf->queryRetriesAllowed() == 10);
  11181. assertex(cwf->queryRetriesRemaining() == 10);
  11182. diter.setown(cwf->getDependencies());
  11183. for(diter->first(); diter->isValid(); diter->next())
  11184. {
  11185. dep[diter->query()-1] = true;
  11186. deps++;
  11187. }
  11188. assertex(deps==2);
  11189. assertex(dep[0]);
  11190. assertex(dep[1]);
  11191. okay[3] = true;
  11192. break;
  11193. case 5:
  11194. assertex(cwf->isScheduled());
  11195. assertex(!cwf->isScheduledNow());
  11196. assertex(cwf->querySchedulePriority() == 75);
  11197. assertex(cwf->queryScheduleCount() == 5);
  11198. assertex(cwf->queryScheduleCountRemaining() == 5);
  11199. okay[4] = true;
  11200. break;
  11201. case 6:
  11202. assertex(cwf->isScheduled());
  11203. assertex(!cwf->isScheduledNow());
  11204. assertex(cwf->querySchedulePriority() == 25);
  11205. assertex(!cwf->hasScheduleCount());
  11206. okay[5] = true;
  11207. break;
  11208. default:
  11209. assertex(!"unknown wfid in test");
  11210. }
  11211. }
  11212. void testRuntimeWorkflow(IRuntimeWorkflowItem * rwf, bool * okay)
  11213. {
  11214. DBGLOG("Test workflow runtime iface %u", rwf->queryWfid());
  11215. switch(rwf->queryWfid())
  11216. {
  11217. case 1:
  11218. case 2:
  11219. case 3:
  11220. okay[rwf->queryWfid()-1] = true;
  11221. break;
  11222. case 4:
  11223. {
  11224. unsigned tries = 0;
  11225. while(rwf->testAndDecRetries())
  11226. tries++;
  11227. assertex(tries == 10);
  11228. assertex(rwf->queryRetriesRemaining() == 0);
  11229. rwf->setState(WFStateFail);
  11230. assertex(rwf->queryState() == WFStateFail);
  11231. rwf->reset();
  11232. assertex(rwf->queryRetriesRemaining() == 10);
  11233. assertex(rwf->queryState() == WFStateReqd);
  11234. }
  11235. okay[3] = true;
  11236. break;
  11237. case 5:
  11238. {
  11239. assertex(rwf->queryScheduleCountRemaining() == 5);
  11240. unsigned count = 0;
  11241. do count++; while(rwf->decAndTestScheduleCountRemaining());
  11242. assertex(count == 5);
  11243. assertex(rwf->queryScheduleCountRemaining() == 0);
  11244. rwf->reset();
  11245. assertex(rwf->queryScheduleCountRemaining() == 5);
  11246. }
  11247. okay[4] = true;
  11248. break;
  11249. case 6:
  11250. {
  11251. assertex(!rwf->hasScheduleCount());
  11252. unsigned count;
  11253. for(count=0; count<20; count++)
  11254. assertex(rwf->decAndTestScheduleCountRemaining());
  11255. }
  11256. okay[5] = true;
  11257. break;
  11258. default:
  11259. assertex(!"unknown wfid in test");
  11260. }
  11261. }
  11262. void testWorkflow()
  11263. {
  11264. DBGLOG("workunit.cpp : testWorkflow");
  11265. CLocalWorkUnit wu("W-WF-TEST", 0, 0, 0);
  11266. Owned<IWorkflowItem> wf;
  11267. wf.setown(wu.addWorkflowItem(1, WFTypeNormal, 0, 0, 0, 0, 0));
  11268. wf.setown(wu.addWorkflowItem(2, WFTypeRecovery, 0, 0, 0, 0, 0));
  11269. wf.setown(wu.addWorkflowItem(3, WFTypeFailure, 0, 0, 0, 0, 4));
  11270. wf.setown(wu.addWorkflowItem(4, WFTypeNormal, 0, 3, 2, 10, 0));
  11271. wf->setScheduledNow();
  11272. wf->addDependency(1);
  11273. wf.setown(wu.addWorkflowItem(5, WFTypeNormal, 0, 0, 0, 0, 0));
  11274. wf->setScheduledOn("test", "foo*");
  11275. wf->setSchedulePriority(75);
  11276. wf->setScheduleCount(5);
  11277. wf.setown(wu.addWorkflowItem(6, WFTypeNormal, 0, 0, 0, 0, 0));
  11278. wf->setScheduledOn("test", "bar*");
  11279. wf->setSchedulePriority(25);
  11280. unsigned const n = 6;
  11281. bool okay[n];
  11282. bool dep[n];
  11283. unsigned i;
  11284. for(i=0; i<n; i++)
  11285. okay[i] = dep[i] = 0;
  11286. Owned<IConstWorkflowItemIterator> citer(wu.getWorkflowItems());
  11287. for(citer->first(); citer->isValid(); citer->next())
  11288. testConstWorkflow(citer->query(), okay, dep);
  11289. for(i=0; i<n; i++)
  11290. {
  11291. assertex(okay[i]);
  11292. okay[i] = false;
  11293. }
  11294. Owned<IWorkflowItemIterator> miter(wu.updateWorkflowItems());
  11295. for(miter->first(); miter->isValid(); miter->next())
  11296. {
  11297. Owned<IRuntimeWorkflowItem> rwf(miter->get());
  11298. testRuntimeWorkflow(rwf, okay);
  11299. }
  11300. for(i=0; i<n; i++)
  11301. {
  11302. assertex(okay[i]);
  11303. okay[i] = dep[i] = false;
  11304. }
  11305. Owned<IWorkflowItemArray> array(wu.getWorkflowClone());
  11306. unsigned wfid;
  11307. for(wfid = 1; array->isValid(wfid); wfid++)
  11308. testConstWorkflow(&array->queryWfid(wfid), okay, dep);
  11309. for(i=0; i<n; i++)
  11310. {
  11311. assertex(okay[i]);
  11312. okay[i] = false;
  11313. }
  11314. for(wfid = 1; array->isValid(wfid); wfid++)
  11315. testRuntimeWorkflow(&array->queryWfid(wfid), okay);
  11316. for(i=0; i<n; i++)
  11317. {
  11318. assertex(okay[i]);
  11319. okay[i] = false;
  11320. }
  11321. }
  11322. #endif
  11323. //------------------------------------------------------------------------------------------
  11324. extern WUState waitForWorkUnitToComplete(const char * wuid, int timeout, std::list<WUState> expectedStates)
  11325. {
  11326. return globalFactory->waitForWorkUnit(wuid, (unsigned) timeout, false, expectedStates);
  11327. }
  11328. extern WORKUNIT_API WUState secWaitForWorkUnitToComplete(const char * wuid, ISecManager *secmgr, ISecUser *secuser, int timeout, std::list<WUState> expectedStates)
  11329. {
  11330. if (checkWuSecAccess(wuid, secmgr, secuser, SecAccess_Read, "Wait for Complete", false, true))
  11331. return waitForWorkUnitToComplete(wuid, timeout, expectedStates);
  11332. return WUStateUnknown;
  11333. }
  11334. extern bool waitForWorkUnitToCompile(const char * wuid, int timeout)
  11335. {
  11336. switch(globalFactory->waitForWorkUnit(wuid, (unsigned) timeout, true, { WUStateWait }))
  11337. {
  11338. case WUStateCompiled:
  11339. case WUStateCompleted:
  11340. case WUStateWait:
  11341. case WUStateUploadingFiles:
  11342. return true;
  11343. default:
  11344. return false;
  11345. }
  11346. }
  11347. extern WORKUNIT_API bool secWaitForWorkUnitToCompile(const char *wuid, ISecManager *secmgr, ISecUser *secuser, int timeout)
  11348. {
  11349. if (checkWuSecAccess(wuid, secmgr, secuser, SecAccess_Read, "Wait for Compile", false, true))
  11350. return waitForWorkUnitToCompile(wuid, timeout);
  11351. return false;
  11352. }
  11353. extern WORKUNIT_API bool secDebugWorkunit(const char *wuid, ISecManager *secmgr, ISecUser *secuser, const char *command, StringBuffer &response)
  11354. {
  11355. if (strnicmp(command, "<debug:", 7) == 0 && checkWuSecAccess(wuid, secmgr, secuser, SecAccess_Read, "Debug", false, true))
  11356. {
  11357. Owned<IConstWorkUnit> wu = globalFactory->openWorkUnit(wuid, secmgr, secuser);
  11358. SCMStringBuffer ip;
  11359. unsigned port = 0;
  11360. try
  11361. {
  11362. port = wu->getDebugAgentListenerPort();
  11363. wu->getDebugAgentListenerIP(ip);
  11364. SocketEndpoint debugEP(ip.str(), port);
  11365. Owned<ISocket> socket = ISocket::connect_timeout(debugEP, 1000);
  11366. unsigned len = (size32_t)strlen(command);
  11367. unsigned revlen = len;
  11368. _WINREV(revlen);
  11369. socket->write(&revlen, sizeof(revlen));
  11370. socket->write(command, len);
  11371. for (;;)
  11372. {
  11373. socket->read(&len, sizeof(len));
  11374. _WINREV(len);
  11375. if (len == 0)
  11376. break;
  11377. if (len & 0x80000000)
  11378. {
  11379. throwUnexpected();
  11380. }
  11381. char * mem = (char*) response.reserve(len);
  11382. socket->read(mem, len);
  11383. }
  11384. return true;
  11385. }
  11386. catch (IException *E)
  11387. {
  11388. VStringBuffer msg("In secDebugWorkunit wuid %s port %d ip %s command %s", wuid, port, ip.str(), command);
  11389. EXCLOG(E, msg.str());
  11390. throw;
  11391. }
  11392. }
  11393. return false;
  11394. }
  11395. void getSimpleResultType(IWUResult *r, Owned<ITypeInfo> &type)
  11396. {
  11397. TypeInfoArray types;
  11398. StringAttrArray names;
  11399. r->getSchema(types, names, NULL);
  11400. if (types.ordinality()==1)
  11401. type.set(&types.item(0));
  11402. }
  11403. bool isSuppliedParamScalar(IWUResult *r, IPropertyTree &curVal, Owned<ITypeInfo> &type)
  11404. {
  11405. if (!r->isResultScalar())
  11406. return false;
  11407. if (!curVal.hasChildren())
  11408. return true;
  11409. getSimpleResultType(r, type);
  11410. return type && type->isScalar();
  11411. }
  11412. void updateSuppliedXmlParams(IWorkUnit * w)
  11413. {
  11414. Owned<const IPropertyTree> params = w->getXmlParams();
  11415. if (!params)
  11416. return;
  11417. Owned<IPropertyTreeIterator> elems = params->getElements("*");
  11418. ForEach(*elems)
  11419. {
  11420. IPropertyTree & curVal = elems->query();
  11421. const char *name = curVal.queryName();
  11422. Owned<IWUResult> r = updateWorkUnitResult(w, name, -1);
  11423. if (r)
  11424. {
  11425. Owned<ITypeInfo> type;
  11426. StringBuffer s;
  11427. if (isSuppliedParamScalar(r, curVal, type))
  11428. {
  11429. curVal.getProp(".", s);
  11430. r->setResultXML(s);
  11431. r->setResultStatus(ResultStatusSupplied);
  11432. }
  11433. else
  11434. {
  11435. toXML(&curVal, s);
  11436. if (!type)
  11437. getSimpleResultType(r, type);
  11438. bool isSet = (type && type->getTypeCode()==type_set);
  11439. r->setResultRaw(s.length(), s.str(), isSet ? ResultFormatXmlSet : ResultFormatXml);
  11440. }
  11441. }
  11442. else
  11443. DBGLOG("WARNING: no matching variable in workunit for input parameter %s", name);
  11444. }
  11445. }
  11446. IWUResult * updateWorkUnitResult(IWorkUnit * w, const char *name, unsigned sequence)
  11447. {
  11448. switch ((int)sequence)
  11449. {
  11450. case ResultSequenceStored:
  11451. return w->updateVariableByName(name);
  11452. case ResultSequencePersist:
  11453. return w->updateGlobalByName(name);
  11454. case ResultSequenceInternal:
  11455. case ResultSequenceOnce:
  11456. return w->updateTemporaryByName(name);
  11457. default:
  11458. return w->updateResultBySequence(sequence);
  11459. }
  11460. }
  11461. IConstWUResult * getWorkUnitResult(IConstWorkUnit * w, const char *name, unsigned sequence)
  11462. {
  11463. switch ((int)sequence)
  11464. {
  11465. case ResultSequenceStored:
  11466. return w->getVariableByName(name);
  11467. case ResultSequencePersist:
  11468. return w->getGlobalByName(name);
  11469. case ResultSequenceInternal:
  11470. case ResultSequenceOnce:
  11471. return w->getTemporaryByName(name);
  11472. default:
  11473. if (name && name[0])
  11474. return w->getResultByName(name);//name takes precedence over sequence
  11475. else
  11476. return w->getResultBySequence(sequence);
  11477. }
  11478. }
  11479. extern WORKUNIT_API bool getWorkUnitCreateTime(const char *wuid,CDateTime &time)
  11480. {
  11481. if (wuid) {
  11482. char prefchar;
  11483. unsigned year,month,day,hour,min,sec;
  11484. if (sscanf(wuid, "%c%4u%2u%2u-%2u%2u%2u", &prefchar, &year, &month, &day, &hour, &min, &sec)==7) {
  11485. time.set(year, month, day, hour, min, sec, 0, true);
  11486. // time.setDate(year, month, day);
  11487. // time.setTime(hour, min, sec, 0, true); // for some reason time is local
  11488. return true;
  11489. }
  11490. }
  11491. return false;
  11492. }
  11493. extern WORKUNIT_API WUState getWorkUnitState(const char* state)
  11494. {
  11495. return (WUState) getEnum(state, states);
  11496. }
  11497. constexpr LogMsgCategory MCschedconn = MCprogress(1000); // Category used to inform about schedule synchronization
  11498. class CWorkflowScheduleConnection : implements IWorkflowScheduleConnection, public CInterface
  11499. {
  11500. public:
  11501. CWorkflowScheduleConnection(char const * wuid)
  11502. {
  11503. basexpath.append("/WorkflowSchedule/").append(wuid);
  11504. flagxpath.append(basexpath.str()).append("/Active");
  11505. }
  11506. IMPLEMENT_IINTERFACE;
  11507. virtual void lock()
  11508. {
  11509. LOG(MCschedconn, "Locking base schedule connection");
  11510. baseconn.setown(querySDS().connect(basexpath.str(), myProcessSession(), RTM_CREATE_QUERY | RTM_LOCK_WRITE, INFINITE));
  11511. if(!baseconn)
  11512. throw MakeStringException(WUERR_ScheduleLockFailed, "Could not get base workflow schedule lock");
  11513. }
  11514. virtual void unlock()
  11515. {
  11516. LOG(MCschedconn, "Unlocking base schedule connection");
  11517. baseconn.clear();
  11518. }
  11519. virtual void setActive()
  11520. {
  11521. LOG(MCschedconn, "Setting active flag in schedule connection");
  11522. flagconn.setown(querySDS().connect(flagxpath.str(), myProcessSession(), RTM_CREATE | RTM_LOCK_WRITE | RTM_DELETE_ON_DISCONNECT, INFINITE));
  11523. if(!flagconn)
  11524. throw MakeStringException(WUERR_ScheduleLockFailed, "Could not get active workflow schedule lock");
  11525. }
  11526. virtual void resetActive()
  11527. {
  11528. LOG(MCschedconn, "Resetting active flag in schedule connection");
  11529. flagconn.clear();
  11530. }
  11531. virtual bool queryActive()
  11532. {
  11533. return baseconn->queryRoot()->hasProp("Active");
  11534. }
  11535. virtual bool pull(IWorkflowItemArray * workflow)
  11536. {
  11537. assertex(baseconn);
  11538. Owned<IPropertyTree> root = baseconn->getRoot();
  11539. Owned<IPropertyTree> eventQueue = root->getPropTree("EventQueue");
  11540. if(!eventQueue) return false;
  11541. if(!eventQueue->hasProp("Item")) return false;
  11542. {
  11543. Owned<IPropertyTreeIterator> eventItems = eventQueue->getElements("Item");
  11544. Owned<IPropertyTree> eventItem;
  11545. Owned<IRuntimeWorkflowItemIterator> wfItems = workflow->getSequenceIterator();
  11546. Owned<IRuntimeWorkflowItem> wfItem;
  11547. for(eventItems->first(); eventItems->isValid(); eventItems->next())
  11548. {
  11549. eventItem.setown(&eventItems->get());
  11550. const char * eventName = eventItem->queryProp("@name");
  11551. const char * eventText = eventItem->queryProp("@text");
  11552. for(wfItems->first(); wfItems->isValid(); wfItems->next())
  11553. {
  11554. wfItem.setown(wfItems->get());
  11555. if(wfItem->queryState() != WFStateWait)
  11556. continue;
  11557. Owned<IWorkflowEvent> targetEvent = wfItem->getScheduleEvent();
  11558. if(!targetEvent || !targetEvent->matches(eventName, eventText))
  11559. continue;
  11560. wfItem->setEvent(eventName, eventText);
  11561. wfItem->setState(WFStateReqd);
  11562. resetDependentsState(workflow, *wfItem);
  11563. }
  11564. }
  11565. }
  11566. bool more;
  11567. do
  11568. more = eventQueue->removeProp("Item");
  11569. while(more);
  11570. return true;
  11571. }
  11572. virtual void push(char const * name, char const * text)
  11573. {
  11574. assertex(baseconn);
  11575. Owned<IPropertyTree> root = baseconn->getRoot();
  11576. IPropertyTree *eventQueue = ensurePTree(root, "EventQueue");
  11577. IPropertyTree *eventItem = eventQueue->addPropTree("Item");
  11578. eventItem->setProp("@name", name);
  11579. eventItem->setProp("@text", text);
  11580. }
  11581. virtual void remove()
  11582. {
  11583. if (baseconn)
  11584. {
  11585. baseconn->close(true);
  11586. baseconn.clear();
  11587. }
  11588. }
  11589. private:
  11590. void resetItemStateAndDependents(IWorkflowItemArray * workflow, unsigned wfid) const
  11591. {
  11592. if (wfid)
  11593. resetItemStateAndDependents(workflow, workflow->queryWfid(wfid));
  11594. }
  11595. void resetItemStateAndDependents(IWorkflowItemArray * workflow, IRuntimeWorkflowItem & item) const
  11596. {
  11597. switch(item.queryState())
  11598. {
  11599. case WFStateDone:
  11600. case WFStateFail:
  11601. {
  11602. item.setState(WFStateNull);
  11603. resetItemStateAndDependents(workflow, item.queryPersistWfid());
  11604. resetDependentsState(workflow, item);
  11605. break;
  11606. }
  11607. }
  11608. }
  11609. void resetDependentsState(IWorkflowItemArray * workflow, IRuntimeWorkflowItem & item) const
  11610. {
  11611. Owned<IWorkflowDependencyIterator> iter(item.getDependencies());
  11612. for(iter->first(); iter->isValid(); iter->next())
  11613. {
  11614. IRuntimeWorkflowItem & dep = workflow->queryWfid(iter->query());
  11615. resetItemStateAndDependents(workflow, dep);
  11616. }
  11617. }
  11618. private:
  11619. StringBuffer basexpath;
  11620. StringBuffer flagxpath;
  11621. Owned<IRemoteConnection> baseconn;
  11622. Owned<IRemoteConnection> flagconn;
  11623. };
  11624. extern WORKUNIT_API IWorkflowScheduleConnection * getWorkflowScheduleConnection(char const * wuid)
  11625. {
  11626. return new CWorkflowScheduleConnection(wuid);
  11627. }
  11628. extern WORKUNIT_API IExtendedWUInterface * queryExtendedWU(IConstWorkUnit * wu)
  11629. {
  11630. return QUERYINTERFACE(wu, IExtendedWUInterface);
  11631. }
  11632. extern WORKUNIT_API const IExtendedWUInterface * queryExtendedWU(const IConstWorkUnit * wu)
  11633. {
  11634. return QUERYINTERFACE(wu, const IExtendedWUInterface);
  11635. }
  11636. extern WORKUNIT_API void addExceptionToWorkunit(IWorkUnit * wu, ErrorSeverity severity, const char * source, unsigned code, const char * text, const char * filename, unsigned lineno, unsigned column, unsigned activity)
  11637. {
  11638. Owned<IWUException> we = wu->createException();
  11639. we->setSeverity(severity);
  11640. we->setExceptionMessage(text);
  11641. if (source)
  11642. we->setExceptionSource(source);
  11643. if (code)
  11644. we->setExceptionCode(code);
  11645. if (filename)
  11646. we->setExceptionFileName(filename);
  11647. if (lineno)
  11648. {
  11649. we->setExceptionLineNo(lineno);
  11650. if (column)
  11651. we->setExceptionColumn(lineno);
  11652. }
  11653. if (activity)
  11654. we->setActivityId(activity);
  11655. }
  11656. const char * skipLeadingXml(const char * text)
  11657. {
  11658. if (!text)
  11659. return NULL;
  11660. //skip utf8 BOM, probably excessive
  11661. if (memcmp(text, UTF8_BOM, 3) == 0)
  11662. text += 3;
  11663. for (;;)
  11664. {
  11665. if (isspace(*text))
  11666. text++;
  11667. else if (text[0] == '<' && text[1] == '?')
  11668. {
  11669. text += 2;
  11670. for (;;)
  11671. {
  11672. if (!*text) break;
  11673. if (text[0] == '?' && text[1] == '>')
  11674. {
  11675. text += 2;
  11676. break;
  11677. }
  11678. text++;
  11679. }
  11680. }
  11681. else if (text[0] == '<' && text[1] == '!' && text[2] == '-' && text[3] == '-')
  11682. {
  11683. text += 4;
  11684. for (;;)
  11685. {
  11686. if (!*text) break;
  11687. if (text[0] == '-' && text[1] == '-' && text[2] == '>')
  11688. {
  11689. text += 3;
  11690. break;
  11691. }
  11692. text++;
  11693. }
  11694. }
  11695. else
  11696. break;
  11697. }
  11698. return text;
  11699. }
  11700. extern WORKUNIT_API bool isArchiveQuery(const char * text)
  11701. {
  11702. text = skipLeadingXml(text);
  11703. if (!text)
  11704. return false;
  11705. const char * archivePrefix = "<Archive";
  11706. return memicmp(text, archivePrefix, strlen(archivePrefix)) == 0;
  11707. }
  11708. extern WORKUNIT_API bool isQueryManifest(const char * text)
  11709. {
  11710. text = skipLeadingXml(text);
  11711. if (!text)
  11712. return false;
  11713. const char * manifestPrefix = "<Manifest";
  11714. return memicmp(text, manifestPrefix, strlen(manifestPrefix)) == 0;
  11715. }
  11716. //------------------------------------------------------------------------------
  11717. // Named Alias helper function
  11718. static IPropertyTree * resolveQueryByDll(IPropertyTree * queryRegistry, const char * dll)
  11719. {
  11720. StringBuffer xpath;
  11721. xpath.append("Query[@dll=\"").append(dll).append("\"]");
  11722. return queryRegistry->getPropTree(xpath);
  11723. }
  11724. static IPropertyTree * resolveQueryByWuid(IPropertyTree * queryRegistry, const char * wuid)
  11725. {
  11726. StringBuffer xpath;
  11727. xpath.append("Query[@wuid=\"").append(wuid).append("\"]");
  11728. return queryRegistry->getPropTree(xpath);
  11729. }
  11730. static void clearAliases(IPropertyTree * queryRegistry, const char * id)
  11731. {
  11732. StringBuffer lcId(id);
  11733. lcId.toLowerCase();
  11734. StringBuffer xpath;
  11735. xpath.append("Alias[@id=\"").append(lcId).append("\"]");
  11736. while (queryRegistry->removeProp(xpath));
  11737. }
  11738. IPropertyTree * addNamedQuery(IPropertyTree * queryRegistry, const char * name, const char * wuid, const char * dll, bool library, const char *userid, const char *snapshot)
  11739. {
  11740. StringBuffer lcName(name);
  11741. lcName.toLowerCase();
  11742. StringBuffer xpath;
  11743. xpath.append("Query[@name=\"").append(lcName.str()).append("\"]");
  11744. Owned<IPropertyTreeIterator> iter = queryRegistry->getElements(xpath);
  11745. unsigned seq = 1;
  11746. ForEach(*iter)
  11747. {
  11748. IPropertyTree &item = iter->query();
  11749. const char *thisWuid = item.queryProp("@wuid");
  11750. if (strieq(wuid, thisWuid))
  11751. return &item;
  11752. unsigned thisSeq = item.getPropInt("@seq");
  11753. if (thisSeq >= seq)
  11754. seq = thisSeq + 1;
  11755. }
  11756. StringBuffer id;
  11757. id.append(lcName).append(".").append(seq);
  11758. IPropertyTree * newEntry = createPTree("Query", ipt_caseInsensitive|ipt_lowmem);
  11759. newEntry->setProp("@name", lcName);
  11760. newEntry->setProp("@wuid", wuid);
  11761. newEntry->setProp("@dll", dll);
  11762. newEntry->setProp("@id", id);
  11763. newEntry->setPropInt("@seq", seq);
  11764. if (library)
  11765. newEntry->setPropBool("@isLibrary", true);
  11766. if (userid && *userid)
  11767. newEntry->setProp("@publishedBy", userid);
  11768. if (snapshot && *snapshot)
  11769. newEntry->setProp("@snapshot", snapshot);
  11770. return queryRegistry->addPropTree("Query", newEntry);
  11771. }
  11772. void removeNamedQuery(IPropertyTree * queryRegistry, const char * id)
  11773. {
  11774. StringBuffer lcId(id);
  11775. lcId.toLowerCase();
  11776. clearAliases(queryRegistry, lcId);
  11777. StringBuffer xpath;
  11778. xpath.append("Query[@id=\"").append(lcId).append("\"]");
  11779. queryRegistry->removeProp(xpath);
  11780. }
  11781. void removeDllFromNamedQueries(IPropertyTree * queryRegistry, const char * dll)
  11782. {
  11783. Owned<IPropertyTree> match = resolveQueryByDll(queryRegistry, dll);
  11784. if (!match)
  11785. return;
  11786. clearAliases(queryRegistry, match->queryProp("@id"));
  11787. queryRegistry->removeTree(match);
  11788. }
  11789. void removeWuidFromNamedQueries(IPropertyTree * queryRegistry, const char * wuid)
  11790. {
  11791. Owned<IPropertyTree> match = resolveQueryByWuid(queryRegistry, wuid);
  11792. if (!match)
  11793. return;
  11794. clearAliases(queryRegistry, match->queryProp("@id"));
  11795. queryRegistry->removeTree(match);
  11796. }
  11797. void removeAliasesFromNamedQuery(IPropertyTree * queryRegistry, const char * id)
  11798. {
  11799. clearAliases(queryRegistry, id);
  11800. }
  11801. void setQueryAlias(IPropertyTree * queryRegistry, const char * name, const char * value)
  11802. {
  11803. StringBuffer lcName(name);
  11804. lcName.toLowerCase();
  11805. StringBuffer xpath;
  11806. xpath.append("Alias[@name=\"").append(lcName).append("\"]");
  11807. IPropertyTree * match = queryRegistry->queryPropTree(xpath);
  11808. if (!match)
  11809. {
  11810. match = queryRegistry->addPropTree("Alias");
  11811. match->setProp("@name", lcName);
  11812. }
  11813. match->setProp("@id", value);
  11814. }
  11815. extern WORKUNIT_API IPropertyTree * getQueryById(IPropertyTree * queryRegistry, const char *queryid)
  11816. {
  11817. if (!queryRegistry || !queryid)
  11818. return NULL;
  11819. StringBuffer xpath;
  11820. xpath.append("Query[@id=\"").append(queryid).append("\"]");
  11821. return queryRegistry->getPropTree(xpath);
  11822. }
  11823. extern WORKUNIT_API IPropertyTree * getQueryById(const char *queryset, const char *queryid, bool readonly)
  11824. {
  11825. Owned<IPropertyTree> queryRegistry = getQueryRegistry(queryset, readonly);
  11826. return getQueryById(queryRegistry, queryid);
  11827. }
  11828. extern WORKUNIT_API IPropertyTree * resolveQueryAlias(IPropertyTree * queryRegistry, const char * alias)
  11829. {
  11830. if (!queryRegistry || !alias)
  11831. return NULL;
  11832. StringBuffer xpath;
  11833. unsigned cnt = 0;
  11834. StringBuffer lc(alias);
  11835. const char * search = lc.toLowerCase().str();
  11836. for (;;)
  11837. {
  11838. xpath.set("Alias[@name='").append(search).append("']/@id");
  11839. const char * queryId = queryRegistry->queryProp(xpath);
  11840. if (!queryId)
  11841. break;
  11842. //Check for too many alias indirections.
  11843. if (cnt++ > 10)
  11844. return NULL;
  11845. search = lc.set(queryId).toLowerCase().str();
  11846. }
  11847. return getQueryById(queryRegistry, search);
  11848. }
  11849. extern WORKUNIT_API IPropertyTree * resolveQueryAlias(const char *queryset, const char *alias, bool readonly)
  11850. {
  11851. Owned<IPropertyTree> queryRegistry = getQueryRegistry(queryset, readonly);
  11852. return resolveQueryAlias(queryRegistry, alias);
  11853. }
  11854. void setQuerySuspendedState(IPropertyTree * queryRegistry, const char *id, bool suspend, const char *userid)
  11855. {
  11856. StringBuffer lcId(id);
  11857. lcId.toLowerCase();
  11858. StringBuffer xpath;
  11859. xpath.append("Query[@id=\"").append(lcId).append("\"]");
  11860. IPropertyTree *tree = queryRegistry->queryPropTree(xpath);
  11861. if (tree)
  11862. {
  11863. if (tree->getPropBool("@suspended", false) == suspend)
  11864. return;
  11865. if (suspend)
  11866. {
  11867. tree->addPropBool("@suspended", true);
  11868. if (userid && *userid)
  11869. tree->addProp("@suspendedBy", userid);
  11870. }
  11871. else
  11872. {
  11873. tree->removeProp("@suspended");
  11874. tree->removeProp("@suspendedBy");
  11875. }
  11876. }
  11877. else
  11878. throw MakeStringException((suspend)? QUERRREG_SUSPEND : QUERRREG_UNSUSPEND, "Modifying query suspended state failed. Could not find query %s", id);
  11879. }
  11880. void setQueryCommentForNamedQuery(IPropertyTree * queryRegistry, const char *id, const char *queryComment)
  11881. {
  11882. if (queryComment)
  11883. {
  11884. StringBuffer lcId(id);
  11885. lcId.toLowerCase();
  11886. StringBuffer xpath;
  11887. xpath.append("Query[@id=\"").append(lcId).append("\"]");
  11888. IPropertyTree *tree = queryRegistry->queryPropTree(xpath);
  11889. if (tree)
  11890. tree->setProp("@queryComment", queryComment);
  11891. else
  11892. throw MakeStringException(QUERRREG_COMMENT, "Could not find query %s", id);
  11893. }
  11894. }
  11895. extern WORKUNIT_API IPropertyTree * getQueryRegistryRoot()
  11896. {
  11897. Owned<IRemoteConnection> conn = querySDS().connect("/QuerySets", myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT);
  11898. if (conn)
  11899. return conn->getRoot();
  11900. else
  11901. return NULL;
  11902. }
  11903. extern WORKUNIT_API void checkAddLibrariesToQueryEntry(IPropertyTree *queryTree, IConstWULibraryIterator *libraries)
  11904. {
  11905. if (!queryTree || !libraries)
  11906. return;
  11907. if (queryTree->hasProp("@libCount")) //already added
  11908. return;
  11909. unsigned libCount=0;
  11910. ForEach(*libraries)
  11911. {
  11912. IConstWULibrary &library = libraries->query();
  11913. SCMStringBuffer libname;
  11914. if (!library.getName(libname).length())
  11915. continue;
  11916. queryTree->addProp("Library", libname.str());
  11917. libCount++;
  11918. }
  11919. queryTree->setPropInt("@libCount", libCount);
  11920. }
  11921. extern WORKUNIT_API void checkAddLibrariesToQueryEntry(IPropertyTree *queryTree, IConstWorkUnit *cw)
  11922. {
  11923. Owned<IConstWULibraryIterator> libraries = &cw->getLibraries();
  11924. checkAddLibrariesToQueryEntry(queryTree, libraries);
  11925. }
  11926. extern WORKUNIT_API IPropertyTree * getQueryRegistry(const char * wsEclId, bool readonly)
  11927. {
  11928. //Only lock the branch for the target we're interested in.
  11929. StringBuffer xpath;
  11930. xpath.append("/QuerySets/QuerySet[@id=\"").append(wsEclId).append("\"]");
  11931. Owned<IRemoteConnection> conn = querySDS().connect(xpath.str(), myProcessSession(), readonly ? RTM_LOCK_READ : RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  11932. if (conn)
  11933. return conn->getRoot();
  11934. if (readonly)
  11935. return NULL;
  11936. //Lock the QuerySets in case another thread/client wants to check/add the same QuerySet.
  11937. Owned<IRemoteConnection> globalLock = querySDS().connect("/QuerySets/", myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  11938. //Re-check if the QuerySet has been added between checking the 1st time and gaining the globalLock.
  11939. conn.setown(querySDS().connect(xpath.str(), myProcessSession(), RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT));
  11940. if (conn)
  11941. return conn->getRoot();
  11942. conn.setown(querySDS().connect("/QuerySets/QuerySet", myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_ADD, SDS_LOCK_TIMEOUT));
  11943. if (!conn)
  11944. throwUnexpected();
  11945. IPropertyTree * root = conn->queryRoot();
  11946. root->setProp("@id",wsEclId);
  11947. conn->commit();
  11948. return LINK(root);
  11949. }
  11950. IPropertyTree * addNamedPackageSet(IPropertyTree * packageRegistry, const char * name, IPropertyTree *packageInfo, bool overWrite)
  11951. {
  11952. StringBuffer xpath;
  11953. StringBuffer lcName(name);
  11954. lcName.toLowerCase();
  11955. // see if "name" already exists
  11956. xpath.append("Package[@id='").append(name).append("']");
  11957. IPropertyTree *pkgTree = packageRegistry->queryPropTree(xpath.str());
  11958. if (pkgTree)
  11959. {
  11960. if (overWrite)
  11961. packageRegistry->removeTree(pkgTree);
  11962. else
  11963. throw MakeStringException(WUERR_PackageAlreadyExists, "Package name %s already exists, either delete it or specify overwrite",name);
  11964. }
  11965. IPropertyTree *tree = packageRegistry->addPropTree("Package", packageInfo);
  11966. tree->setProp("@id", lcName);
  11967. return tree;
  11968. }
  11969. void removeNamedPackage(IPropertyTree * packageRegistry, const char * id)
  11970. {
  11971. StringBuffer lcId(id);
  11972. lcId.toLowerCase();
  11973. StringBuffer xpath;
  11974. xpath.append("Package[@id=\"").append(lcId).append("\"]");
  11975. packageRegistry->removeProp(xpath);
  11976. }
  11977. extern WORKUNIT_API IPropertyTree * getPackageSetRegistry(const char * wsEclId, bool readonly)
  11978. {
  11979. //Only lock the branch for the target we're interested in.
  11980. StringBuffer xpath;
  11981. xpath.append("/PackageSets/PackageSet[@id=\"").append(wsEclId).append("\"]");
  11982. Owned<IRemoteConnection> conn = querySDS().connect(xpath.str(), myProcessSession(), readonly ? RTM_LOCK_READ : RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  11983. if (conn)
  11984. return conn->getRoot();
  11985. if (readonly)
  11986. return NULL;
  11987. //Lock the PackageSets in case another thread/client wants to check/add the same PackageSet.
  11988. Owned<IRemoteConnection> globalLock = querySDS().connect("/PackageSets/", myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  11989. //Re-check if the PackageSet has been added between checking the 1st time and gaining the globalLock.
  11990. conn.setown(querySDS().connect(xpath.str(), myProcessSession(), RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT));
  11991. if (conn)
  11992. return conn->getRoot();
  11993. conn.setown(querySDS().connect("/PackageSets/PackageSet", myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_ADD, SDS_LOCK_TIMEOUT));
  11994. if (!conn)
  11995. throwUnexpected();
  11996. IPropertyTree* root = conn->queryRoot();
  11997. root->setProp("@id",wsEclId);
  11998. conn->commit();
  11999. return LINK(root);
  12000. }
  12001. void addQueryToQuerySet(IWorkUnit *workunit, IPropertyTree *queryRegistry, const char *queryName, WUQueryActivationOptions activateOption, StringBuffer &newQueryId, const char *userid)
  12002. {
  12003. StringBuffer cleanQueryName;
  12004. appendUtf8XmlName(cleanQueryName, strlen(queryName), queryName);
  12005. SCMStringBuffer dllName;
  12006. Owned<IConstWUQuery> q = workunit->getQuery();
  12007. q->getQueryDllName(dllName);
  12008. if (!dllName.length())
  12009. throw MakeStringException(WUERR_InvalidDll, "Cannot deploy query - no associated dll.");
  12010. StringBuffer currentTargetClusterType;
  12011. queryRegistry->getProp("@targetclustertype", currentTargetClusterType);
  12012. SCMStringBuffer targetClusterType;
  12013. workunit->getDebugValue("targetclustertype", targetClusterType);
  12014. SCMStringBuffer snapshot;
  12015. workunit->getSnapshot(snapshot);
  12016. if (currentTargetClusterType.length() < 1)
  12017. {
  12018. queryRegistry->setProp("@targetclustertype", targetClusterType.str());
  12019. }
  12020. else
  12021. {
  12022. if (strcmp(currentTargetClusterType.str(), "roxie") == 0 && strcmp(currentTargetClusterType.str(), targetClusterType.str())!=0)
  12023. {
  12024. throw MakeStringException(WUERR_MismatchClusterType, "TargetClusterTypes of workunit and queryset do not match.");
  12025. }
  12026. }
  12027. IPropertyTree *newEntry = addNamedQuery(queryRegistry, cleanQueryName, workunit->queryWuid(), dllName.str(), isLibrary(workunit), userid, snapshot.str());
  12028. Owned<IConstWULibraryIterator> libraries = &workunit->getLibraries();
  12029. checkAddLibrariesToQueryEntry(newEntry, libraries);
  12030. newQueryId.append(newEntry->queryProp("@id"));
  12031. workunit->setIsQueryService(true); //will check querysets before delete
  12032. workunit->commit();
  12033. activateQuery(queryRegistry, activateOption, queryName, newQueryId, userid);
  12034. }
  12035. void activateQuery(IPropertyTree *queryRegistry, WUQueryActivationOptions activateOption, const char *queryName, const char *queryId, const char *userid)
  12036. {
  12037. StringBuffer cleanQueryName;
  12038. appendUtf8XmlName(cleanQueryName, strlen(queryName), queryName);
  12039. if (activateOption == ACTIVATE_SUSPEND_PREVIOUS|| activateOption == ACTIVATE_DELETE_PREVIOUS)
  12040. {
  12041. Owned<IPropertyTree> prevQuery = resolveQueryAlias(queryRegistry, cleanQueryName);
  12042. setQueryAlias(queryRegistry, cleanQueryName, queryId);
  12043. if (prevQuery && !streq(queryId, prevQuery->queryProp("@id")))
  12044. {
  12045. if (activateOption == ACTIVATE_SUSPEND_PREVIOUS)
  12046. setQuerySuspendedState(queryRegistry, prevQuery->queryProp("@id"), true, userid);
  12047. else
  12048. removeNamedQuery(queryRegistry, prevQuery->queryProp("@id"));
  12049. }
  12050. }
  12051. else if (activateOption == MAKE_ACTIVATE || activateOption == MAKE_ACTIVATE_LOAD_DATA_ONLY)
  12052. setQueryAlias(queryRegistry, cleanQueryName, queryId);
  12053. }
  12054. void addQueryToQuerySet(IWorkUnit *workunit, const char *querySetName, const char *queryName, WUQueryActivationOptions activateOption, StringBuffer &newQueryId, const char *userid)
  12055. {
  12056. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, false);
  12057. addQueryToQuerySet(workunit, queryRegistry, queryName, activateOption, newQueryId, userid);
  12058. }
  12059. bool removeQuerySetAlias(const char *querySetName, const char *alias)
  12060. {
  12061. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, true);
  12062. StringBuffer xpath;
  12063. xpath.appendf("Alias[@name='%s']", alias);
  12064. IPropertyTree *t = queryRegistry->queryPropTree(xpath);
  12065. return queryRegistry->removeTree(t);
  12066. }
  12067. void addQuerySetAlias(const char *querySetName, const char *alias, const char *id)
  12068. {
  12069. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, false);
  12070. setQueryAlias(queryRegistry, alias, id);
  12071. }
  12072. void setSuspendQuerySetQuery(const char *querySetName, const char *id, bool suspend, const char *userid)
  12073. {
  12074. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, true);
  12075. setQuerySuspendedState(queryRegistry, id, suspend, userid);
  12076. }
  12077. void deleteQuerySetQuery(const char *querySetName, const char *id)
  12078. {
  12079. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, true);
  12080. removeNamedQuery(queryRegistry, id);
  12081. }
  12082. void removeQuerySetAliasesFromNamedQuery(const char *querySetName, const char * id)
  12083. {
  12084. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, true);
  12085. clearAliases(queryRegistry, id);
  12086. }
  12087. void setQueryCommentForNamedQuery(const char *querySetName, const char *id, const char *queryComment)
  12088. {
  12089. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, true);
  12090. setQueryCommentForNamedQuery(queryRegistry, id, queryComment);
  12091. }
  12092. const char *queryIdFromQuerySetWuid(IPropertyTree *queryRegistry, const char *wuid, const char *queryName, IStringVal &id)
  12093. {
  12094. if (!queryRegistry)
  12095. return NULL;
  12096. StringBuffer xpath;
  12097. xpath.appendf("Query[@wuid='%s']", wuid);
  12098. if (queryName && *queryName)
  12099. xpath.appendf("[@name='%s']", queryName);
  12100. IPropertyTree *q = queryRegistry->queryPropTree(xpath.str());
  12101. if (q)
  12102. {
  12103. id.set(q->queryProp("@id"));
  12104. }
  12105. return id.str();
  12106. }
  12107. const char *queryIdFromQuerySetWuid(const char *querySetName, const char *wuid, const char *queryName, IStringVal &id)
  12108. {
  12109. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, true);
  12110. return queryIdFromQuerySetWuid(queryRegistry, wuid, queryName, id);
  12111. }
  12112. extern WORKUNIT_API void gatherLibraryNames(StringArray &names, StringArray &unresolved, IWorkUnitFactory &workunitFactory, IConstWorkUnit &cw, IPropertyTree *queryset)
  12113. {
  12114. Owned<IConstWULibraryIterator> wulibraries = &cw.getLibraries();
  12115. ForEach(*wulibraries)
  12116. {
  12117. SCMStringBuffer libname;
  12118. IConstWULibrary &wulibrary = wulibraries->query();
  12119. wulibrary.getName(libname);
  12120. if (names.contains(libname.str()) || unresolved.contains(libname.str()))
  12121. continue;
  12122. Owned<IPropertyTree> query = resolveQueryAlias(queryset, libname.str());
  12123. if (query && query->getPropBool("@isLibrary"))
  12124. {
  12125. const char *wuid = query->queryProp("@wuid");
  12126. Owned<IConstWorkUnit> libcw = workunitFactory.openWorkUnit(wuid);
  12127. if (libcw)
  12128. {
  12129. names.appendUniq(libname.str());
  12130. gatherLibraryNames(names, unresolved, workunitFactory, *libcw, queryset);
  12131. continue;
  12132. }
  12133. }
  12134. unresolved.appendUniq(libname.str());
  12135. }
  12136. }
  12137. bool looksLikeAWuid(const char * wuid, const char firstChar)
  12138. {
  12139. if (!wuid)
  12140. return false;
  12141. if (wuid[0] != firstChar)
  12142. return false;
  12143. if (!isdigit(wuid[1]) || !isdigit(wuid[2]) || !isdigit(wuid[3]) || !isdigit(wuid[4]))
  12144. return false;
  12145. if (!isdigit(wuid[5]) || !isdigit(wuid[6]) || !isdigit(wuid[7]) || !isdigit(wuid[8]))
  12146. return false;
  12147. return (wuid[9]=='-');
  12148. }
  12149. IPropertyTree * resolveDefinitionInArchive(IPropertyTree * archive, const char * path)
  12150. {
  12151. IPropertyTree * module = archive;
  12152. const char * dot = strrchr(path, '.');
  12153. StringBuffer xpath;
  12154. if (dot)
  12155. {
  12156. xpath.clear().append("Module[@key='").appendLower(dot-path, path).append("']");
  12157. module = archive->queryPropTree(xpath);
  12158. path = dot+1;
  12159. }
  12160. else
  12161. module = archive->queryPropTree("Module[@key='']");
  12162. if (!module)
  12163. return NULL;
  12164. xpath.clear().append("Attribute[@key='").appendLower(strlen(path), path).append("']");
  12165. return module->queryPropTree(xpath);
  12166. }
  12167. extern WORKUNIT_API void associateLocalFile(IWUQuery * query, WUFileType type, const char * name, const char * description, unsigned crc, unsigned minActivity, unsigned maxActivity)
  12168. {
  12169. StringBuffer fullPathName;
  12170. makeAbsolutePath(name, fullPathName);
  12171. if (isContainerized())
  12172. {
  12173. StringBuffer dllDirectory;
  12174. if (getConfigurationDirectory(nullptr, "query", nullptr, nullptr, dllDirectory))
  12175. {
  12176. StringBuffer destPathName(dllDirectory);
  12177. addNonEmptyPathSepChar(destPathName);
  12178. splitFilename(fullPathName.str(), nullptr, nullptr, &destPathName, &destPathName);
  12179. OwnedIFile source = createIFile(fullPathName);
  12180. OwnedIFile target = createIFile(destPathName);
  12181. if (!target->exists())
  12182. {
  12183. source->copyTo(target, 0, NULL, true);
  12184. }
  12185. query->addAssociatedFile(type, destPathName, "localhost", description, crc, minActivity, maxActivity);
  12186. // Should we delete the local files? No - they may not be finished with
  12187. }
  12188. else
  12189. {
  12190. //Containerized stand alone eclcc or other examples with no configuration file
  12191. query->addAssociatedFile(type, fullPathName, "localhost", description, crc, minActivity, maxActivity);
  12192. }
  12193. }
  12194. else
  12195. {
  12196. StringBuffer hostname;
  12197. queryHostIP().getIpText(hostname);
  12198. query->addAssociatedFile(type, fullPathName, hostname, description, crc, minActivity, maxActivity);
  12199. }
  12200. }
  12201. extern WORKUNIT_API void descheduleWorkunit(char const * wuid)
  12202. {
  12203. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  12204. Owned<IWorkUnit> workunit = factory->updateWorkUnit(wuid);
  12205. if(workunit)
  12206. workunit->deschedule();
  12207. else
  12208. doDescheduleWorkkunit(wuid);
  12209. }
  12210. extern WORKUNIT_API void updateWorkunitStat(IWorkUnit * wu, StatisticScopeType scopeType, const char * scope, StatisticKind kind, const char * description, unsigned __int64 value, unsigned wfid)
  12211. {
  12212. StringBuffer scopestr;
  12213. if (wfid && scope && *scope)
  12214. scopestr.append(WorkflowScopePrefix).append(wfid).append(":").append(scope);
  12215. else
  12216. scopestr.set(scope);
  12217. wu->setStatistic(queryStatisticsComponentType(), queryStatisticsComponentName(), scopeType, scopestr, kind, description, value, 1, 0, StatsMergeReplace);
  12218. }
  12219. class WuTimingUpdater : implements ITimeReportInfo
  12220. {
  12221. public:
  12222. WuTimingUpdater(IWorkUnit * _wu, StatisticScopeType _scopeType, StatisticKind _kind)
  12223. : wu(_wu), scopeType(_scopeType), kind(_kind)
  12224. { }
  12225. virtual void report(const char * scope, const __int64 totaltime, const __int64 maxtime, const unsigned count)
  12226. {
  12227. wu->setStatistic(queryStatisticsComponentType(), queryStatisticsComponentName(), scopeType, scope, kind, nullptr, totaltime, count, maxtime, StatsMergeReplace);
  12228. }
  12229. protected:
  12230. IWorkUnit * wu;
  12231. StatisticScopeType scopeType;
  12232. StatisticKind kind;
  12233. };
  12234. extern WORKUNIT_API void updateWorkunitTimings(IWorkUnit * wu, ITimeReporter *timer)
  12235. {
  12236. WuTimingUpdater target(wu, SSTsection, StTimeTotalExecute);
  12237. timer->report(target);
  12238. }
  12239. extern WORKUNIT_API void updateWorkunitTimings(IWorkUnit * wu, StatisticScopeType scopeType, StatisticKind kind, ITimeReporter *timer)
  12240. {
  12241. WuTimingUpdater target(wu, scopeType, kind);
  12242. timer->report(target);
  12243. }
  12244. extern WORKUNIT_API void addTimeStamp(IWorkUnit * wu, StatisticScopeType scopeType, const char * scope, StatisticKind kind, unsigned wfid)
  12245. {
  12246. StringBuffer scopestr;
  12247. if (wfid && scope && *scope)
  12248. scopestr.append(WorkflowScopePrefix).append(wfid).append(":").append(scope);
  12249. else
  12250. scopestr.set(scope);
  12251. wu->setStatistic(queryStatisticsComponentType(), queryStatisticsComponentName(), scopeType, scopestr, kind, NULL, getTimeStampNowValue(), 1, 0, StatsMergeAppend);
  12252. }
  12253. static double getCpuSize(const char *resourceName)
  12254. {
  12255. Owned<IPropertyTree> compConfig = getComponentConfig();
  12256. const char * cpuRequestedStr = compConfig->queryProp(resourceName);
  12257. if (!cpuRequestedStr)
  12258. return 0.0;
  12259. char * endptr;
  12260. double cpuRequested = strtod(cpuRequestedStr, &endptr);
  12261. if (cpuRequested < 0.0)
  12262. return 0.0;
  12263. if (*endptr == 'm')
  12264. cpuRequested /= 1000;
  12265. return cpuRequested;
  12266. }
  12267. static double getCostCpuHour()
  12268. {
  12269. double costCpuHour = 0.0;
  12270. if (getComponentConfigSP()->hasProp("cost/@perCpu"))
  12271. costCpuHour = getComponentConfigSP()->getPropReal("cost/@perCpu");
  12272. else
  12273. costCpuHour = getGlobalConfigSP()->getPropReal("cost/@perCpu");
  12274. if (costCpuHour < 0.0)
  12275. return 0.0;
  12276. return costCpuHour;
  12277. }
  12278. extern WORKUNIT_API double getMachineCostRate()
  12279. {
  12280. unsigned numCpus = isContainerized() ? getCpuSize("resources/@cpu") : getAffinityCpus();
  12281. return getCostCpuHour() * numCpus;
  12282. }
  12283. extern WORKUNIT_API double getThorManagerRate()
  12284. {
  12285. unsigned numCpus = isContainerized() ? getCpuSize("managerResources/@cpu") : getAffinityCpus();
  12286. return getCostCpuHour() * numCpus ;
  12287. }
  12288. extern WORKUNIT_API double getThorWorkerRate()
  12289. {
  12290. unsigned numCpus = isContainerized() ? getCpuSize("workerResources/@cpu") : getAffinityCpus();
  12291. return getCostCpuHour() * numCpus ;
  12292. }
  12293. extern WORKUNIT_API double calculateThorCost(unsigned __int64 ms, unsigned clusterWidth)
  12294. {
  12295. return calcCost(getThorManagerRate(), ms) + calcCost(getThorWorkerRate(), ms) * clusterWidth;
  12296. }
  12297. void aggregateStatistic(StatsAggregation & result, IConstWorkUnit * wu, const WuScopeFilter & filter, StatisticKind search)
  12298. {
  12299. SimpleReferenceAggregator aggregator(search, result);
  12300. Owned<IConstWUScopeIterator> it = &wu->getScopeIterator(filter);
  12301. ForEach(*it)
  12302. it->playProperties(aggregator);
  12303. }
  12304. class GlobalStatisticGatherer : public CInterfaceOf<IStatisticGatherer>
  12305. {
  12306. public:
  12307. GlobalStatisticGatherer(IWorkUnit * _wu) : wu(_wu) {}
  12308. virtual void beginScope(const StatsScopeId & id)
  12309. {
  12310. prevLenStack.append(scope.length());
  12311. if (scope.length())
  12312. scope.append(":");
  12313. id.getScopeText(scope);
  12314. scopeTypeStack.append(id.queryScopeType());
  12315. }
  12316. virtual void beginSubGraphScope(unsigned id)
  12317. {
  12318. StatsScopeId scopeId(SSTsubgraph, id);
  12319. beginScope(scopeId);
  12320. }
  12321. virtual void beginActivityScope(unsigned id)
  12322. {
  12323. StatsScopeId scopeId(SSTactivity, id);
  12324. beginScope(scopeId);
  12325. }
  12326. virtual void beginEdgeScope(unsigned id, unsigned oid)
  12327. {
  12328. StatsScopeId scopeId(SSTedge, id, oid);
  12329. beginScope(scopeId);
  12330. }
  12331. virtual void beginChildGraphScope(unsigned id)
  12332. {
  12333. StatsScopeId scopeId(SSTchildgraph, id);
  12334. beginScope(scopeId);
  12335. }
  12336. virtual void beginChannelScope(unsigned id)
  12337. {
  12338. StatsScopeId scopeId(SSTchannel, id);
  12339. beginScope(scopeId);
  12340. }
  12341. virtual void endScope()
  12342. {
  12343. scope.setLength(prevLenStack.popGet());
  12344. scopeTypeStack.pop();
  12345. }
  12346. virtual void addStatistic(StatisticKind kind, unsigned __int64 value)
  12347. {
  12348. StatisticScopeType scopeType = scopeTypeStack.ordinality() ? (StatisticScopeType)scopeTypeStack.tos() : SSTglobal;
  12349. wu->setStatistic(queryStatisticsComponentType(), queryStatisticsComponentName(), scopeType, scope, kind, NULL, value, 1, 0, StatsMergeAppend);
  12350. }
  12351. virtual void updateStatistic(StatisticKind kind, unsigned __int64 value, StatsMergeAction mergeAction)
  12352. {
  12353. StatisticScopeType scopeType = scopeTypeStack.ordinality() ? (StatisticScopeType)scopeTypeStack.tos() : SSTglobal;
  12354. wu->setStatistic(queryStatisticsComponentType(), queryStatisticsComponentName(), scopeType, scope, kind, NULL, value, 1, 0, mergeAction);
  12355. }
  12356. virtual IStatisticCollection * getResult()
  12357. {
  12358. return NULL;
  12359. }
  12360. protected:
  12361. Linked<IWorkUnit> wu;
  12362. StringBuffer scope;
  12363. UnsignedArray prevLenStack;
  12364. UnsignedArray scopeTypeStack;
  12365. };
  12366. IStatisticGatherer * createGlobalStatisticGatherer(IWorkUnit * wu)
  12367. {
  12368. return new GlobalStatisticGatherer(wu);
  12369. }
  12370. extern WORKUNIT_API IPropertyTree * getWUGraphProgress(const char * wuid, bool readonly)
  12371. {
  12372. if (!wuid || !*wuid)
  12373. return NULL;
  12374. VStringBuffer path("/GraphProgress/%s", wuid);
  12375. Owned<IRemoteConnection> conn = querySDS().connect(path.str(),myProcessSession(),readonly ? RTM_LOCK_READ : RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  12376. if (conn)
  12377. return conn->getRoot();
  12378. else
  12379. return NULL;
  12380. }
  12381. void addWorkunitException(IWorkUnit * wu, IError * error, bool removeTimeStamp)
  12382. {
  12383. ErrorSeverity wuSeverity = SeverityInformation;
  12384. ErrorSeverity severity = error->getSeverity();
  12385. switch (severity)
  12386. {
  12387. case SeverityIgnore:
  12388. return;
  12389. case SeverityInformation:
  12390. break;
  12391. case SeverityWarning:
  12392. wuSeverity = SeverityWarning;
  12393. break;
  12394. case SeverityError:
  12395. case SeverityFatal:
  12396. wuSeverity = SeverityError;
  12397. break;
  12398. }
  12399. Owned<IWUException> exception = wu->createException();
  12400. exception->setSeverity(wuSeverity);
  12401. StringBuffer msg;
  12402. exception->setExceptionCode(error->errorCode());
  12403. exception->setExceptionMessage(error->errorMessage(msg).str());
  12404. const char * source = queryCreatorTypeName(queryStatisticsComponentType());
  12405. exception->setExceptionSource(source);
  12406. exception->setExceptionFileName(error->getFilename());
  12407. exception->setExceptionLineNo(error->getLine());
  12408. exception->setExceptionColumn(error->getColumn());
  12409. if (removeTimeStamp)
  12410. exception->setTimeStamp(nullptr);
  12411. if (error->getActivity())
  12412. exception->setActivityId(error->getActivity());
  12413. if (error->queryScope())
  12414. exception->setScope(error->queryScope());
  12415. }
  12416. IError * WorkUnitErrorReceiver::mapError(IError * error)
  12417. {
  12418. return LINK(error);
  12419. }
  12420. void WorkUnitErrorReceiver::report(IError* eclError)
  12421. {
  12422. addWorkunitException(wu, eclError, removeTimeStamp);
  12423. }
  12424. size32_t WorkUnitErrorReceiver::errCount()
  12425. {
  12426. unsigned count = 0;
  12427. Owned<IConstWUExceptionIterator> exceptions = &wu->getExceptions();
  12428. ForEach(*exceptions)
  12429. if (exceptions->query().getSeverity() == SeverityError)
  12430. count++;
  12431. return count;
  12432. }
  12433. size32_t WorkUnitErrorReceiver::warnCount()
  12434. {
  12435. unsigned count = 0;
  12436. Owned<IConstWUExceptionIterator> exceptions = &wu->getExceptions();
  12437. ForEach(*exceptions)
  12438. if (exceptions->query().getSeverity() == SeverityWarning)
  12439. count++;
  12440. return count;
  12441. }
  12442. bool isValidPriorityValue(const char *priority)
  12443. {
  12444. if (isEmptyString(priority))
  12445. return false;
  12446. if (strieq("SLA", priority) || strieq("LOW", priority) || strieq("HIGH", priority) || strieq("NONE", priority))
  12447. return true;
  12448. return false;
  12449. }
  12450. bool isValidMemoryValue(const char *memoryUnit)
  12451. {
  12452. if (isEmptyString(memoryUnit) || !isdigit(*memoryUnit))
  12453. return false;
  12454. while (isdigit(*++memoryUnit));
  12455. if (!*memoryUnit)
  12456. return true;
  12457. switch (toupper(*memoryUnit++))
  12458. {
  12459. case 'E':
  12460. case 'P':
  12461. case 'T':
  12462. case 'G':
  12463. case 'M':
  12464. case 'K':
  12465. if (!*memoryUnit || strieq("B", memoryUnit))
  12466. return true;
  12467. break;
  12468. case 'B':
  12469. if (!*memoryUnit)
  12470. return true;
  12471. break;
  12472. }
  12473. return false;
  12474. }
  12475. // used by eclagent and roxie
  12476. #define ABORT_POLL_PERIOD (5*1000)
  12477. void executeThorGraph(const char * graphName, IConstWorkUnit &workunit, const IPropertyTree &config)
  12478. {
  12479. unsigned timelimit = workunit.getDebugValueInt("thorConnectTimeout", config.getPropInt("@thorConnectTimeout", 60));
  12480. StringAttr wuid(workunit.queryWuid());
  12481. IConstWUGraph *graph = workunit.getGraph(graphName);
  12482. if (!graph)
  12483. throw makeStringExceptionV(0, "getGraph() returns nullptr for %s", graphName);
  12484. unsigned wfid = graph->getWfid();
  12485. #ifdef _CONTAINERIZED
  12486. // NB: If a single agent were to want to launch >1 Thor, then the threading could be in the workflow above this call.
  12487. {
  12488. Owned<IWorkUnit> w = &workunit.lock();
  12489. w->setState(WUStateBlocked);
  12490. }
  12491. WUState state = WUStateUnknown;
  12492. if (config.hasProp("@queue"))
  12493. {
  12494. CCycleTimer elapsedTimer;
  12495. bool multiJobLinger = config.getPropBool("@multiJobLinger");
  12496. if (executeGraphOnLingeringThor(workunit, graphName, multiJobLinger ? config.queryProp("@queue") : nullptr))
  12497. PROGLOG("Existing lingering Thor handled graph: %s", graphName);
  12498. else
  12499. {
  12500. VStringBuffer queueName("%s.thor", config.queryProp("@queue"));
  12501. DBGLOG("Queueing wuid=%s, graph=%s, on queue=%s, timelimit=%u seconds", wuid.str(), graphName, queueName.str(), timelimit);
  12502. Owned<IJobQueue> queue = createJobQueue(queueName);
  12503. VStringBuffer jobName("%s/%s", wuid.get(), graphName);
  12504. IJobQueueItem *item = createJobQueueItem(jobName);
  12505. queue->enqueue(item);
  12506. }
  12507. // NB: overall max runtime if guillotine set handled by abortmonitor
  12508. unsigned runningTimeLimit = workunit.getDebugValueInt("maxRunTime", 0);
  12509. runningTimeLimit = runningTimeLimit ? runningTimeLimit : INFINITE;
  12510. std::list<WUState> expectedStates = { WUStateRunning, WUStateWait };
  12511. unsigned __int64 blockedTime = 0;
  12512. for (unsigned i=0; i<2; i++)
  12513. {
  12514. WUState state = waitForWorkUnitToComplete(wuid, timelimit*1000, expectedStates);
  12515. DBGLOG("Got state: %s", getWorkunitStateStr(state));
  12516. if (WUStateWait == state) // already finished
  12517. {
  12518. // workunit may have spent time in blocked state, but then transitioned to
  12519. // wait state quickly such that this code did not see its running state.
  12520. if (!blockedTime)
  12521. blockedTime = elapsedTimer.elapsedNs();
  12522. break;
  12523. }
  12524. else if ((INFINITE != timelimit) && (WUStateUnknown == state))
  12525. throw makeStringExceptionV(0, "Query %s failed to start within specified timelimit (%u) seconds", wuid.str(), timelimit);
  12526. else
  12527. {
  12528. auto it = std::find(expectedStates.begin(), expectedStates.end(), state);
  12529. if (it == expectedStates.end())
  12530. throw makeStringExceptionV(0, "Query %s failed, state: %s", wuid.str(), getWorkunitStateStr(state));
  12531. }
  12532. blockedTime = elapsedTimer.elapsedNs();
  12533. timelimit = runningTimeLimit;
  12534. expectedStates = { WUStateWait };
  12535. }
  12536. Owned<IWorkUnit> w = &workunit.lock();
  12537. updateWorkunitStat(w, SSTgraph, graphName, StTimeBlocked, nullptr, blockedTime, wfid);
  12538. }
  12539. else
  12540. {
  12541. VStringBuffer job("%s-%s", wuid.str(), graphName);
  12542. runK8sJob("thormanager", wuid, job, { { "graphName", graphName} });
  12543. }
  12544. /* In k8s, Thor feeds back the terminating exception via the workunit.
  12545. * (in bare-metal it is returned via a socket the agent connect to Thor with)
  12546. */
  12547. workunit.forceReload();
  12548. if (workunit.getExceptionCount())
  12549. {
  12550. Owned<IConstWUExceptionIterator> iter = &workunit.getExceptions();
  12551. ForEach(*iter)
  12552. {
  12553. IConstWUException &e = iter->query();
  12554. SCMStringBuffer str;
  12555. e.getExceptionSource(str);
  12556. if (streq("thormasterexception", str.s))
  12557. {
  12558. str.clear();
  12559. e.getExceptionMessage(str);
  12560. unsigned errCode = e.getExceptionCode();
  12561. {
  12562. /* Clear the feedback exception (consume it),
  12563. * so that any future re-submissions of this workunit (e.g. due to workflow)
  12564. * don't see it again.
  12565. */
  12566. Owned<IWorkUnit> w = &workunit.lock();
  12567. w->clearExceptions("thormasterexception");
  12568. }
  12569. throw makeStringException(errCode, str.str());
  12570. }
  12571. }
  12572. }
  12573. else
  12574. {
  12575. Owned<IWorkUnit> w = &workunit.lock();
  12576. WUState state = w->getState();
  12577. if (WUStateFailed == state)
  12578. throw makeStringException(0, "Workunit failed");
  12579. }
  12580. {
  12581. Owned<IWorkUnit> w = &workunit.lock();
  12582. w->setState(WUStateRunning);
  12583. }
  12584. #else
  12585. StringAttr owner(workunit.queryUser());
  12586. StringAttr cluster(workunit.queryClusterName());
  12587. int priority = workunit.getPriorityValue();
  12588. Owned<IConstWUClusterInfo> c = getTargetClusterInfo(cluster);
  12589. if (!c)
  12590. throw MakeStringException(0, "Invalid thor cluster %s", cluster.str());
  12591. SCMStringBuffer queueName;
  12592. c->getThorQueue(queueName);
  12593. Owned<IJobQueue> jq = createJobQueue(queueName.str());
  12594. bool resubmit;
  12595. Owned<IWorkUnitFactory> wuFactory = getWorkUnitFactory();
  12596. do // loop if pause interrupted graph and needs resubmitting on resume
  12597. {
  12598. resubmit = false; // set if job interrupted in thor
  12599. if (WUStatePaused == workunit.getState()) // check initial state - and wait if paused
  12600. {
  12601. for (;;)
  12602. {
  12603. WUAction action = wuFactory->waitForWorkUnitAction(wuid, workunit.getAction());
  12604. if (action == WUActionUnknown)
  12605. throw new WorkflowException(0, "Workunit aborting", 0, WorkflowException::ABORT, MSGAUD_user);
  12606. if (action != WUActionPause && action != WUActionPauseNow)
  12607. break;
  12608. }
  12609. }
  12610. {
  12611. Owned<IWorkUnit> w = &workunit.lock();
  12612. w->setState(WUStateBlocked);
  12613. }
  12614. class cPollThread: public Thread // MORE - why do we ned a thread here?
  12615. {
  12616. Semaphore sem;
  12617. bool stopped;
  12618. IJobQueue *jq;
  12619. IConstWorkUnit *wu;
  12620. public:
  12621. bool timedout;
  12622. CTimeMon tm;
  12623. cPollThread(IJobQueue *_jq, IConstWorkUnit *_wu, unsigned timelimit)
  12624. : tm(timelimit)
  12625. {
  12626. stopped = false;
  12627. jq = _jq;
  12628. wu = _wu;
  12629. timedout = false;
  12630. }
  12631. ~cPollThread()
  12632. {
  12633. stop();
  12634. }
  12635. int run()
  12636. {
  12637. while (!stopped) {
  12638. sem.wait(ABORT_POLL_PERIOD);
  12639. if (stopped)
  12640. break;
  12641. if (tm.timedout()) {
  12642. timedout = true;
  12643. stopped = true;
  12644. jq->cancelInitiateConversation();
  12645. }
  12646. else if (wu->aborting()) {
  12647. stopped = true;
  12648. jq->cancelInitiateConversation();
  12649. }
  12650. }
  12651. return 0;
  12652. }
  12653. void stop()
  12654. {
  12655. stopped = true;
  12656. sem.signal();
  12657. }
  12658. } pollthread(jq, &workunit, timelimit*1000);
  12659. pollthread.start();
  12660. CCycleTimer elapsedTimer;
  12661. PROGLOG("Enqueuing on %s to run wuid=%s, graph=%s, timelimit=%d seconds, priority=%d", queueName.str(), wuid.str(), graphName, timelimit, priority);
  12662. VStringBuffer wuidGraph("%s/%s", wuid.str(), graphName);
  12663. IJobQueueItem* item = createJobQueueItem(wuidGraph.str());
  12664. item->setOwner(owner.str());
  12665. item->setPriority(priority);
  12666. Owned<IConversation> conversation = jq->initiateConversation(item);
  12667. bool got = conversation.get()!=NULL;
  12668. pollthread.stop();
  12669. pollthread.join();
  12670. if (!got)
  12671. {
  12672. if (pollthread.timedout)
  12673. throw MakeStringException(0, "Query %s failed to start within specified timelimit (%u) seconds", wuidGraph.str(), timelimit);
  12674. throw MakeStringException(0, "Query %s cancelled (1)", wuidGraph.str());
  12675. }
  12676. // get the thor ep from whoever picked up
  12677. SocketEndpoint thorMaster;
  12678. MemoryBuffer msg;
  12679. if (!conversation->recv(msg,1000*60))
  12680. throw MakeStringException(0, "Query %s cancelled (2)", wuidGraph.str());
  12681. thorMaster.deserialize(msg);
  12682. msg.clear();
  12683. SocketEndpoint myep;
  12684. myep.setLocalHost(0);
  12685. myep.serialize(msg); // only used for tracing
  12686. if (!conversation->send(msg)) {
  12687. StringBuffer s("Failed to send query to Thor on ");
  12688. thorMaster.getUrlStr(s);
  12689. throw MakeStringExceptionDirect(-1, s.str()); // maybe retry?
  12690. }
  12691. unsigned __int64 blockedTime = elapsedTimer.elapsedNs();
  12692. {
  12693. Owned<IWorkUnit> w = &workunit.lock();
  12694. updateWorkunitStat(w, SSTgraph, graphName, StTimeBlocked, nullptr, blockedTime, wfid);
  12695. }
  12696. StringBuffer eps;
  12697. PROGLOG("Thor on %s running %s", thorMaster.getUrlStr(eps).str(), wuidGraph.str());
  12698. MemoryBuffer reply;
  12699. try
  12700. {
  12701. if (!conversation->recv(reply,INFINITE))
  12702. {
  12703. StringBuffer s("Failed to receive reply from thor ");
  12704. thorMaster.getUrlStr(s);
  12705. throw MakeStringExceptionDirect(-1, s.str());
  12706. }
  12707. }
  12708. catch (IException *e)
  12709. {
  12710. StringBuffer s("Failed to receive reply from thor ");
  12711. thorMaster.getUrlStr(s);
  12712. s.append("; (").append(e->errorCode()).append(", ");
  12713. e->errorMessage(s).append(")");
  12714. e->Release();
  12715. throw MakeStringExceptionDirect(-1, s.str());
  12716. }
  12717. unsigned replyCode;
  12718. reply.read(replyCode);
  12719. switch ((ThorReplyCodes) replyCode)
  12720. {
  12721. case DAMP_THOR_REPLY_PAUSED:
  12722. {
  12723. bool isException;
  12724. reply.read(isException);
  12725. if (isException)
  12726. {
  12727. Owned<IException> e = deserializeException(reply);
  12728. VStringBuffer str("Pausing job %s caused exception", wuidGraph.str());
  12729. EXCLOG(e, str.str());
  12730. }
  12731. Owned<IWorkUnit> w = &workunit.lock();
  12732. w->setState(WUStatePaused); // will trigger executeThorGraph to pause next time around.
  12733. WUAction action = w->getAction();
  12734. switch (action)
  12735. {
  12736. case WUActionPause:
  12737. case WUActionPauseNow:
  12738. w->setAction(WUActionUnknown);
  12739. }
  12740. resubmit = true; // JCSMORE - all subgraph _could_ be done, thor will check though and not rerun
  12741. break;
  12742. }
  12743. case DAMP_THOR_REPLY_GOOD:
  12744. break;
  12745. case DAMP_THOR_REPLY_ERROR:
  12746. {
  12747. throw deserializeException(reply);
  12748. }
  12749. case DAMP_THOR_REPLY_ABORT:
  12750. {
  12751. Owned<IException> e = deserializeException(reply);
  12752. StringBuffer msg;
  12753. e->errorMessage(msg);
  12754. throw new WorkflowException(e->errorCode(), msg.str(), 0, WorkflowException::ABORT, MSGAUD_user);
  12755. }
  12756. default:
  12757. throwUnexpected();
  12758. }
  12759. workunit.forceReload();
  12760. }
  12761. while (resubmit); // if pause interrupted job (i.e. with pausenow action), resubmit graph
  12762. #endif
  12763. }
  12764. #ifdef _CONTAINERIZED
  12765. bool executeGraphOnLingeringThor(IConstWorkUnit &workunit, const char *graphName, const char *multiJobLingerQueueName)
  12766. {
  12767. // check if lingering thor instance is up.
  12768. // Returns true if successfully submitted graph to a lingering Thor.
  12769. /* If code was dependent on reading a workunit in parallel, the whole area of
  12770. * workunit locking and reading will need revisiting, because at the moment the
  12771. * workunit reading code does not lock at all.
  12772. *
  12773. * Either,
  12774. * 1) multiJobThorLinger is set, meaning Thor instances will wait for any job's graph.
  12775. * OR
  12776. * 2) if not set, the Thor will wait for graphs from same job only, and will be
  12777. * notified to quit by the agent, when the last graph is complete.
  12778. *
  12779. * This code is called from the client (agent) when it wants to execute a Thor graph.
  12780. * This function checks to see if there any registered lingering Thor instances available.
  12781. *
  12782. * Lingering Thor's register when they are idle either with the workunit itself in
  12783. * the case of [2], or with a Dali psuedo queue (a branch under /Status/ThorLinger),
  12784. * in the case of [1]
  12785. * If they have not been contacted within the 'lingerPeriod' they shutdown and remove
  12786. * their registered instance from the queue.
  12787. *
  12788. * The client will attempt to find a lingering Thor in either the workunit or a global
  12789. * named dali queue (depending on the configuration), it will then remove that entry,
  12790. * and attempt to post the graph to that lingering Thor. If the Thor acknowledges the
  12791. * request, this function exits with success. If it it fails, or there are no more
  12792. * lingering Thor instances, it returns with false to indicate that the graph was not
  12793. * submitted to a lingering Thor.
  12794. * The client should then queue the wuid/graph normally, which will invoke a new Thor
  12795. * instance.
  12796. */
  12797. try
  12798. {
  12799. if (multiJobLingerQueueName && graphName)
  12800. {
  12801. // check a Dali queue for idle spare Thor's
  12802. DBGLOG("Checking multi job lingering queue: %s", multiJobLingerQueueName);
  12803. VStringBuffer xpath("/Status/ThorLinger/%s", multiJobLingerQueueName);
  12804. while (true)
  12805. {
  12806. Owned<IRemoteConnection> conn = querySDS().connect(xpath, myProcessSession(), RTM_LOCK_WRITE, 5*60*1000);
  12807. if (!conn)
  12808. break;
  12809. IPropertyTree *root = conn->queryRoot();
  12810. if (0 == root->numChildren())
  12811. break;
  12812. IPropertyTree *entry = root->queryPropTree("Thor*[1]");
  12813. const char *entryName = entry->queryName();
  12814. // get available instance and remove it
  12815. VStringBuffer entryXPath("%s/%s", xpath.str(), entryName);
  12816. Owned<IRemoteConnection> lingerInstanceConn = querySDS().connect(entryXPath, myProcessSession(), RTM_LOCK_WRITE | RTM_DELETE_ON_DISCONNECT, 5*1000);
  12817. if (lingerInstanceConn)
  12818. {
  12819. StringBuffer instanceName;
  12820. lingerInstanceConn->queryRoot()->getProp(nullptr, instanceName);
  12821. lingerInstanceConn->close(true);
  12822. lingerInstanceConn.clear();
  12823. conn.clear();
  12824. Owned<INode> masterNode = createINode(instanceName);
  12825. CMessageBuffer msg;
  12826. VStringBuffer jobStr("%s/%s", workunit.queryWuid(), graphName);
  12827. msg.append(jobStr);
  12828. bool success = queryWorldCommunicator().sendRecv(msg, masterNode, MPTAG_THOR, 10000);
  12829. DBGLOG("Found lingering Thor: %s, submitted: %s, success: %s", instanceName.str(), jobStr.str(), boolToStr(success));
  12830. if (success)
  12831. {
  12832. bool ok;
  12833. msg.read(ok);
  12834. return true;
  12835. }
  12836. }
  12837. }
  12838. }
  12839. else
  12840. {
  12841. /* NB: forcing a reload here to ensure Debug values are recent.
  12842. * Could be improved by refreshing only the specific area of interest (i.e. Debug/ in this case).
  12843. * The area of persisting a non-locked connection to workunits should be resivisted..
  12844. */
  12845. workunit.forceReload();
  12846. Owned<IStringIterator> iter = &workunit.getDebugValues("thorinstance_*");
  12847. ForEach(*iter)
  12848. {
  12849. /* NB: Thor's set their running endpoint into Debug values of workunit
  12850. * Check to see if workunit has any Thor's available lingering instances.
  12851. */
  12852. SCMStringBuffer thorInstance;
  12853. iter->str(thorInstance);
  12854. SCMStringBuffer thorInstanceValue;
  12855. if (workunit.getDebugValueBool(thorInstance.s, false))
  12856. {
  12857. {
  12858. Owned<IWorkUnit> w = &workunit.lock();
  12859. w->setDebugValue(thorInstance.s, "0", true);
  12860. }
  12861. /* NB: there's a window where Thor could shutdown here.
  12862. * In that case, the sendRecv will fail and it will fall through to queueing.
  12863. */
  12864. const char *instanceName = strchr(thorInstance.str(), '_') + 1;
  12865. Owned<INode> masterNode = createINode(instanceName);
  12866. CMessageBuffer msg;
  12867. VStringBuffer jobStr("%s/%s", workunit.queryWuid(), graphName?graphName:"");
  12868. msg.append(jobStr);
  12869. if (queryWorldCommunicator().sendRecv(msg, masterNode, MPTAG_THOR, 10000))
  12870. {
  12871. bool ok;
  12872. msg.read(ok);
  12873. if (graphName) // if graphName==nullptr, then continue around to look at others
  12874. {
  12875. if (ok)
  12876. return true;
  12877. }
  12878. }
  12879. }
  12880. }
  12881. }
  12882. }
  12883. catch (IException *e)
  12884. {
  12885. EXCLOG(e, "executeGraphOnLingeringThor");
  12886. e->Release();
  12887. }
  12888. return false;
  12889. }
  12890. static void setResources(IPropertyTree *workerConfig, const IConstWorkUnit *workunit, const char *process)
  12891. {
  12892. auto setResourcesItem = [&workerConfig](const char *category, const char *resourceName, unsigned value, const char *units)
  12893. {
  12894. if (!value) return;
  12895. VStringBuffer xpath("spec/template/spec/containers/resources/%s@%s", category, resourceName);
  12896. ensurePTree(workerConfig, xpath.str());
  12897. VStringBuffer v("%u%s", value, units);
  12898. workerConfig->setProp(xpath.str(), v.str());
  12899. };
  12900. StringBuffer s;
  12901. unsigned memRequest = workunit->getDebugValueInt(s.clear().appendf("resource-%s-memory", process), 0);
  12902. setResourcesItem("requests", "memory", memRequest, "Mi");
  12903. setResourcesItem("limits", "memory", memRequest, "Mi");
  12904. unsigned cpuRequest = workunit->getDebugValueInt(s.clear().appendf("resource-%s-cpu", process), 0);
  12905. setResourcesItem("requests", "cpu", cpuRequest, "m");
  12906. setResourcesItem("limits", "cpu", cpuRequest, "m");
  12907. }
  12908. KeepK8sJobs translateKeepJobs(const char *keepJob)
  12909. {
  12910. if (!isEmptyString(keepJob)) // common case
  12911. {
  12912. if (streq("podfailures", keepJob))
  12913. return KeepK8sJobs::podfailures;
  12914. else if (streq("all", keepJob))
  12915. return KeepK8sJobs::all;
  12916. }
  12917. return KeepK8sJobs::none;
  12918. }
  12919. bool isActiveK8sService(const char *serviceName)
  12920. {
  12921. VStringBuffer getEndpoints("kubectl get endpoints %s \"--output=jsonpath={range .subsets[*].addresses[*]}{.ip}{'\\n'}{end}\"", serviceName);
  12922. StringBuffer output;
  12923. runKubectlCommand("checkEndpoints", getEndpoints.str(), nullptr, &output);
  12924. // Output should be zero or more lines each with an IP
  12925. return (output.length() && output.charAt(0) != '\n');
  12926. }
  12927. void deleteK8sResource(const char *componentName, const char *job, const char *resource)
  12928. {
  12929. VStringBuffer jobname("%s-%s", componentName, job);
  12930. jobname.toLowerCase();
  12931. VStringBuffer deleteResource("kubectl delete %s/%s", resource, jobname.str());
  12932. runKubectlCommand(componentName, deleteResource, nullptr, nullptr);
  12933. }
  12934. void waitK8sJob(const char *componentName, const char *job, unsigned pendingTimeoutSecs, KeepK8sJobs keepJob)
  12935. {
  12936. VStringBuffer jobname("%s-%s", componentName, job);
  12937. jobname.toLowerCase();
  12938. VStringBuffer waitJob("kubectl get jobs %s -o jsonpath={.status.active}", jobname.str());
  12939. VStringBuffer getScheduleStatus("kubectl get pods --selector=job-name=%s --output=jsonpath={.items[*].status.conditions[?(@.type=='PodScheduled')].status}", jobname.str());
  12940. VStringBuffer checkJobExitCode("kubectl get pods --selector=job-name=%s --output=jsonpath={.items[*].status.containerStatuses[?(@.name==\"%s\")].state.terminated.exitCode}", jobname.str(), jobname.str());
  12941. unsigned delay = 100;
  12942. unsigned start = msTick();
  12943. bool schedulingTimeout = false;
  12944. Owned<IException> exception;
  12945. try
  12946. {
  12947. for (;;)
  12948. {
  12949. StringBuffer output;
  12950. runKubectlCommand(componentName, waitJob, nullptr, &output);
  12951. if (!streq(output, "1")) // status.active value
  12952. {
  12953. // Job is no longer active - we can terminate
  12954. DBGLOG("kubectl jobs output: %s", output.str());
  12955. runKubectlCommand(componentName, checkJobExitCode, nullptr, &output.clear());
  12956. if (output.length() && !streq(output, "0")) // state.terminated.exitCode
  12957. throw makeStringExceptionV(0, "Failed to run %s: pod exited with error: %s", jobname.str(), output.str());
  12958. break;
  12959. }
  12960. runKubectlCommand(nullptr, getScheduleStatus, nullptr, &output.clear());
  12961. // Check whether pod has been scheduled yet - if resources are not available pods may block indefinitely waiting to be scheduled, and
  12962. // we would prefer them to fail instead.
  12963. bool pending = streq(output, "False");
  12964. if (pendingTimeoutSecs && pending && msTick()-start > pendingTimeoutSecs*1000)
  12965. {
  12966. schedulingTimeout = true;
  12967. VStringBuffer getReason("kubectl get pods --selector=job-name=%s \"--output=jsonpath={range .items[*].status.conditions[?(@.type=='PodScheduled')]}{.reason}{': '}{.message}{end}\"", jobname.str());
  12968. runKubectlCommand(componentName, getReason, nullptr, &output.clear());
  12969. throw makeStringExceptionV(0, "Failed to run %s - pod not scheduled after %u seconds: %s ", jobname.str(), pendingTimeoutSecs, output.str());
  12970. }
  12971. MilliSleep(delay);
  12972. if (delay < 10000)
  12973. delay = delay * 2;
  12974. }
  12975. }
  12976. catch (IException *e)
  12977. {
  12978. EXCLOG(e, nullptr);
  12979. exception.setown(e);
  12980. }
  12981. if (keepJob != KeepK8sJobs::all)
  12982. {
  12983. // Delete jobs unless the pod failed and keepJob==podfailures
  12984. if ((nullptr == exception) || (KeepK8sJobs::podfailures != keepJob) || schedulingTimeout)
  12985. deleteK8sResource(componentName, job, "job");
  12986. }
  12987. if (exception)
  12988. throw exception.getClear();
  12989. }
  12990. bool applyK8sYaml(const char *componentName, const char *wuid, const char *job, const char *suffix, const std::list<std::pair<std::string, std::string>> &extraParams, bool optional)
  12991. {
  12992. StringBuffer jobname(job);
  12993. jobname.toLowerCase();
  12994. VStringBuffer jobSpecFilename("/etc/config/%s-%s.yaml", componentName, suffix);
  12995. StringBuffer jobYaml;
  12996. try
  12997. {
  12998. jobYaml.loadFile(jobSpecFilename, false);
  12999. }
  13000. catch (IException *E)
  13001. {
  13002. if (!optional)
  13003. throw;
  13004. E->Release();
  13005. return false;
  13006. }
  13007. jobYaml.replaceString("_HPCC_JOBNAME_", jobname.str());
  13008. VStringBuffer args("\"--workunit=%s\"", wuid);
  13009. for (const auto &p: extraParams)
  13010. {
  13011. if (hasPrefix(p.first.c_str(), "_HPCC_", false)) // jobspec substituion
  13012. jobYaml.replaceString(p.first.c_str(), p.second.c_str());
  13013. else
  13014. args.append(" \"--").append(p.first.c_str()).append('=').append(p.second.c_str()).append("\"");
  13015. }
  13016. jobYaml.replaceString("_HPCC_ARGS_", args.str());
  13017. // Disable ability change resources from within workunit
  13018. // - all values are unquoted by toYAML. This caused problems when previous string values are
  13019. // outputted unquoted and then treated as a non string type -e.g. labels in metadata.
  13020. // - Also, ability to control if and how much users may change resources should be provided.
  13021. #if 0
  13022. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  13023. if (factory)
  13024. {
  13025. Owned<IConstWorkUnit> workunit = factory->openWorkUnit(wuid);
  13026. if (workunit)
  13027. {
  13028. Owned<IPropertyTree> workerConfig = createPTreeFromYAMLString(jobYaml.length(), jobYaml.str(), 0, ptr_none, nullptr);
  13029. setResources(workerConfig, workunit, componentName);
  13030. toYAML(workerConfig, jobYaml.clear(), 2, 0);
  13031. }
  13032. }
  13033. #endif
  13034. runKubectlCommand(componentName, "kubectl replace --force -f -", jobYaml, nullptr);
  13035. return true;
  13036. }
  13037. static constexpr unsigned defaultPendingTimeSecs = 600;
  13038. void runK8sJob(const char *componentName, const char *wuid, const char *job, const std::list<std::pair<std::string, std::string>> &extraParams)
  13039. {
  13040. Owned<IPropertyTree> compConfig = getComponentConfig();
  13041. KeepK8sJobs keepJob = translateKeepJobs(compConfig->queryProp("@keepJobs"));
  13042. unsigned pendingTimeoutSecs = compConfig->getPropInt("@pendingTimeoutSecs", defaultPendingTimeSecs);
  13043. bool removeNetwork = applyK8sYaml(componentName, wuid, job, "networkspec", extraParams, true);
  13044. applyK8sYaml(componentName, wuid, job, "jobspec", extraParams, false);
  13045. Owned<IException> exception;
  13046. try
  13047. {
  13048. waitK8sJob(componentName, job, pendingTimeoutSecs, keepJob);
  13049. }
  13050. catch (IException *e)
  13051. {
  13052. EXCLOG(e, nullptr);
  13053. exception.setown(e);
  13054. }
  13055. if (removeNetwork)
  13056. deleteK8sResource(componentName, job, "networkpolicy");
  13057. if (exception)
  13058. throw exception.getClear();
  13059. }
  13060. // returns a vector of {pod-name, node-name} vectors,
  13061. // represented as a nested vector for extensibility, e.g. to add other meta fields
  13062. std::vector<std::vector<std::string>> getPodNodes(const char *selector)
  13063. {
  13064. VStringBuffer getWorkerNodes("kubectl get pods --selector=job-name=%s \"--output=jsonpath={range .items[*]}{.metadata.name},{.spec.nodeName}{'\\n'}{end}\"", selector);
  13065. StringBuffer result;
  13066. runKubectlCommand("get-worker-nodes", getWorkerNodes, nullptr, &result);
  13067. if (result.isEmpty())
  13068. throw makeStringExceptionV(-1, "No worker nodes found for selector '%s'", selector);
  13069. const char *start = result.str();
  13070. const char *finger = start;
  13071. std::string fieldName;
  13072. std::vector<std::vector<std::string>> results;
  13073. std::vector<std::string> current;
  13074. while (true)
  13075. {
  13076. switch (*finger)
  13077. {
  13078. case ',':
  13079. {
  13080. if (start == finger)
  13081. throw makeStringException(-1, "getPodNodes: Missing node name(s) in output");
  13082. fieldName.assign(start, finger-start);
  13083. current.emplace_back(std::move(fieldName));
  13084. finger++;
  13085. start = finger;
  13086. break;
  13087. }
  13088. case '\n':
  13089. case '\0':
  13090. {
  13091. if (start == finger)
  13092. throw makeStringException(-1, "getPodNodes: Missing pod name(s) in output");
  13093. fieldName.assign(start, finger-start);
  13094. current.emplace_back(std::move(fieldName));
  13095. results.emplace_back(std::move(current));
  13096. if ('\0' == *finger)
  13097. return results;
  13098. finger++;
  13099. start = finger;
  13100. break;
  13101. }
  13102. default:
  13103. {
  13104. ++finger;
  13105. break;
  13106. }
  13107. }
  13108. }
  13109. }
  13110. #endif