workunit.cpp 342 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448
  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 "jlib.hpp"
  14. #include "workunit.hpp"
  15. #include "jprop.hpp"
  16. #include "jmisc.hpp"
  17. #include "jexcept.hpp"
  18. #include "jiter.ipp"
  19. #include "jptree.hpp"
  20. #include "jtime.ipp"
  21. #include "jencrypt.hpp"
  22. #include "junicode.hpp"
  23. #include "eclrtl.hpp"
  24. #include "deftype.hpp"
  25. #include <time.h>
  26. #include "mpbase.hpp"
  27. #include "daclient.hpp"
  28. #include "dadfs.hpp"
  29. #include "dafdesc.hpp"
  30. #include "dasds.hpp"
  31. #include "danqs.hpp"
  32. #include "dautils.hpp"
  33. #include "dllserver.hpp"
  34. #include "thorhelper.hpp"
  35. #include "workflow.hpp"
  36. #include "nbcd.hpp"
  37. #include "seclib.hpp"
  38. #include "wuerror.hpp"
  39. #include "wujobq.hpp"
  40. #define GLOBAL_WORKUNIT "global"
  41. #define SDS_LOCK_TIMEOUT (5*60*1000) // 5mins, 30s a bit short
  42. static int workUnitTraceLevel = 1;
  43. static StringBuffer &getXPath(StringBuffer &wuRoot, const char *wuid)
  44. {
  45. // MORE - can fold in the date
  46. return wuRoot.append("/WorkUnits/").append(wuid);
  47. }
  48. //To be called by eclserver, but esp etc. won't know, so we need to store it.
  49. static StringBuffer & appendLibrarySuffix(StringBuffer & suffix)
  50. {
  51. #ifdef _WIN32
  52. suffix.append("W");
  53. #else
  54. suffix.append("L");
  55. #endif
  56. #ifdef __64BIT__
  57. suffix.append("64");
  58. #else
  59. suffix.append("32");
  60. #endif
  61. return suffix;
  62. }
  63. typedef MapStringTo<bool> UniqueScopes;
  64. static void wuAccessError(const char *username, const char *action, const char *wuscope, const char *wuid, bool excpt, bool log)
  65. {
  66. StringBuffer err;
  67. err.append("Workunit Access Denied - action: ").append(action).append(" user:").append(username ? username : "<Unknown>");
  68. if (wuid)
  69. err.append(" workunit:").append(wuid);
  70. if (wuscope)
  71. err.append(" scope:").append(wuscope);
  72. //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
  73. SYSLOG(AUDIT_TYPE_ACCESS_FAILURE, err.str());
  74. if (log)
  75. LOG(MCuserError, "%s", err.str());
  76. if (excpt)
  77. throw MakeStringException(WUERR_AccessError, "%s", err.str());
  78. }
  79. static bool checkWuScopeSecAccess(const char *wuscope, ISecManager &secmgr, ISecUser *secuser, int required, const char *action, bool excpt, bool log)
  80. {
  81. bool ret=(!secuser) ? true : (secmgr.authorizeEx(RT_WORKUNIT_SCOPE, *secuser, wuscope)>=required);
  82. if (!ret && (log || excpt))
  83. wuAccessError(secuser ? secuser->getName() : NULL, action, wuscope, NULL, excpt, log);
  84. return ret;
  85. }
  86. static bool checkWuScopeListSecAccess(const char *wuscope, ISecResourceList *scopes, int required, const char *action, bool excpt, bool log)
  87. {
  88. if (!scopes)
  89. return true;
  90. bool ret=true;
  91. if (wuscope)
  92. {
  93. Owned<ISecResource> res=scopes->getResource(wuscope);
  94. if (!res || res->getAccessFlags()<required)
  95. ret=false;
  96. }
  97. else
  98. {
  99. for (int seq=0; ret && seq<scopes->count(); seq++)
  100. {
  101. ISecResource *res=scopes->queryResource(seq);
  102. if (res && res->getAccessFlags()<required)
  103. return false;
  104. }
  105. }
  106. if (!ret && (log || excpt))
  107. wuAccessError(NULL, action, wuscope, NULL, excpt, log);
  108. return ret;
  109. }
  110. static bool checkWuSecAccess(IConstWorkUnit &cw, ISecManager &secmgr, ISecUser *secuser, int required, const char *action, bool excpt, bool log)
  111. {
  112. SCMStringBuffer wuscope;
  113. bool ret=(!secuser) ? true : (secmgr.authorizeEx(RT_WORKUNIT_SCOPE, *secuser, cw.getWuScope(wuscope).str())>=required);
  114. if (!ret && (log || excpt))
  115. {
  116. SCMStringBuffer wuid;
  117. wuAccessError(secuser ? secuser->getName() : NULL, action, wuscope.str(), cw.getWuid(wuid).str(), excpt, log);
  118. }
  119. return ret;
  120. }
  121. static bool checkWuSecAccess(const char *wuid, ISecManager &secmgr, ISecUser *secuser, int required, const char *action, bool excpt, bool log)
  122. {
  123. StringBuffer wuRoot;
  124. Owned<IRemoteConnection> conn = querySDS().connect(getXPath(wuRoot, wuid).str(), myProcessSession(), 0, SDS_LOCK_TIMEOUT);
  125. if (conn)
  126. {
  127. Owned<IPropertyTree> ptree=conn->getRoot();
  128. return checkWuScopeSecAccess(ptree->queryProp("@scope"), secmgr, secuser, required, action, excpt, log);
  129. }
  130. if (log || excpt)
  131. wuAccessError(secuser ? secuser->getName() : NULL, action, "Unknown", NULL, excpt, log);
  132. return false;
  133. }
  134. void doDescheduleWorkkunit(char const * wuid)
  135. {
  136. StringBuffer xpath;
  137. xpath.append("*/*/*/");
  138. ncnameEscape(wuid, xpath);
  139. Owned<IRemoteConnection> conn = querySDS().connect("/Schedule", myProcessSession(), RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  140. if(!conn) return;
  141. Owned<IPropertyTree> root = conn->getRoot();
  142. bool more;
  143. do more = root->removeProp(xpath.str()); while(more);
  144. }
  145. #define PROGRESS_FORMAT_V 2
  146. class CConstGraphProgress : public CInterface, implements IConstWUGraphProgress
  147. {
  148. class CGraphProgress : public CInterface, implements IWUGraphProgress
  149. {
  150. CConstGraphProgress &parent;
  151. public:
  152. IMPLEMENT_IINTERFACE;
  153. CGraphProgress(CConstGraphProgress &_parent) : parent(_parent)
  154. {
  155. parent.lockWrite();
  156. }
  157. ~CGraphProgress()
  158. {
  159. parent.unlock();
  160. }
  161. virtual IPropertyTree * queryProgressTree() { return parent.queryProgressTree(); }
  162. virtual WUGraphState queryGraphState() { return parent.queryGraphState(); }
  163. virtual WUGraphState queryNodeState(WUGraphIDType nodeId) { return parent.queryNodeState(nodeId); }
  164. virtual IWUGraphProgress * update() { return parent.update(); }
  165. virtual unsigned queryFormatVersion() { return parent.queryFormatVersion(); }
  166. virtual IPropertyTree & updateEdge(WUGraphIDType nodeId, const char *edgeId)
  167. {
  168. return parent.updateEdge(nodeId, edgeId);
  169. }
  170. virtual IPropertyTree & updateNode(WUGraphIDType nodeId, WUNodeIDType id)
  171. {
  172. return parent.updateNode(nodeId, id);
  173. }
  174. virtual void setGraphState(WUGraphState state)
  175. {
  176. parent.setGraphState(state);
  177. }
  178. virtual void setNodeState(WUGraphIDType nodeId, WUGraphState state)
  179. {
  180. parent.setNodeState(nodeId, state);
  181. }
  182. };
  183. IPropertyTree &updateElement(WUGraphIDType nodeId, const char *elemName, const char *id)
  184. {
  185. IPropertyTree *elem = NULL;
  186. if (!connectedWrite) lockWrite();
  187. StringBuffer path;
  188. path.append("node[@id=\"").append(nodeId).append("\"]");
  189. IPropertyTree *node = progress->queryPropTree(path.str());
  190. if (!node)
  191. {
  192. node = progress->addPropTree("node", createPTree());
  193. node->setPropInt("@id", (int)nodeId);
  194. elem = node->addPropTree(elemName, createPTree());
  195. elem->setProp("@id", id);
  196. }
  197. else
  198. {
  199. path.clear().append(elemName).append("[@id=\"").append(id).append("\"]");
  200. elem = node->queryPropTree(path.str());
  201. if (!elem)
  202. {
  203. elem = node->addPropTree(elemName, createPTree());
  204. elem->setProp("@id", id);
  205. }
  206. }
  207. return *elem;
  208. }
  209. public:
  210. IMPLEMENT_IINTERFACE;
  211. static void deleteWuidProgress(const char *wuid)
  212. {
  213. StringBuffer path("/GraphProgress/");
  214. path.append(wuid);
  215. Owned<IRemoteConnection> conn = querySDS().connect(path.str(), myProcessSession(), RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  216. if (conn)
  217. conn->close(true);
  218. }
  219. CConstGraphProgress(const char *_wuid, const char *_graphName) : wuid(_wuid), graphName(_graphName)
  220. {
  221. rootPath.append("/GraphProgress/").append(wuid).append('/').append(graphName).append('/');
  222. connected = connectedWrite = false;
  223. formatVersion = 0;
  224. progress = NULL;
  225. }
  226. void connect()
  227. {
  228. conn.clear();
  229. packProgress(wuid,false);
  230. conn.setown(querySDS().connect(rootPath.str(), myProcessSession(), RTM_LOCK_READ|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT));
  231. progress = conn->queryRoot();
  232. formatVersion = progress->getPropInt("@format");
  233. connected = true;
  234. }
  235. void lockWrite()
  236. {
  237. if (connectedWrite) return;
  238. // JCSMORE - look at using changeMode here.
  239. if (conn)
  240. conn.clear();
  241. else
  242. packProgress(wuid,false);
  243. conn.setown(querySDS().connect(rootPath.str(), myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT));
  244. progress = conn->queryRoot();
  245. if (!progress->hasChildren()) // i.e. blank.
  246. {
  247. formatVersion = PROGRESS_FORMAT_V;
  248. progress->setPropInt("@format", PROGRESS_FORMAT_V);
  249. }
  250. else
  251. formatVersion = progress->getPropInt("@format");
  252. connected = connectedWrite = true;
  253. }
  254. void unlock()
  255. {
  256. connected = false;
  257. connectedWrite = false;
  258. conn.clear();
  259. }
  260. IPropertyTree &updateNode(WUGraphIDType nodeId, WUNodeIDType id)
  261. {
  262. StringBuffer s;
  263. return updateElement(nodeId, "node", s.append(id).str());
  264. }
  265. IPropertyTree &updateEdge(WUGraphIDType nodeId, const char *edgeId)
  266. {
  267. return updateElement(nodeId, "edge", edgeId);
  268. }
  269. static bool getRunningGraph(const char *wuid, IStringVal &graphName, WUGraphIDType &subId)
  270. {
  271. StringBuffer path;
  272. Owned<IRemoteConnection> conn = querySDS().connect(path.append("/GraphProgress/").append(wuid).str(), myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT);
  273. if (!conn) return false;
  274. const char *name = conn->queryRoot()->queryProp("Running/@graph");
  275. if (name)
  276. {
  277. graphName.set(name);
  278. subId = conn->queryRoot()->getPropInt64("Running/@subId");
  279. return true;
  280. }
  281. else
  282. return false;
  283. }
  284. void setGraphState(WUGraphState state)
  285. {
  286. progress->setPropInt("@_state", (unsigned)state);
  287. }
  288. void setNodeState(WUGraphIDType nodeId, WUGraphState state)
  289. {
  290. if (!connectedWrite) lockWrite();
  291. StringBuffer path;
  292. path.append("node[@id=\"").append(nodeId).append("\"]");
  293. IPropertyTree *node = progress->queryPropTree(path.str());
  294. if (!node)
  295. {
  296. node = progress->addPropTree("node", createPTree());
  297. node->setPropInt("@id", (int)nodeId);
  298. }
  299. node->setPropInt("@_state", (unsigned)state);
  300. switch (state)
  301. {
  302. case WUGraphRunning:
  303. {
  304. StringBuffer path;
  305. Owned<IRemoteConnection> conn = querySDS().connect(path.append("/GraphProgress/").append(wuid).str(), myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  306. IPropertyTree *running = conn->queryRoot()->setPropTree("Running", createPTree());
  307. running->setProp("@graph", graphName);
  308. running->setPropInt64("@subId", nodeId);
  309. break;
  310. }
  311. case WUGraphComplete:
  312. {
  313. StringBuffer path;
  314. Owned<IRemoteConnection> conn = querySDS().connect(path.append("/GraphProgress/").append(wuid).str(), myProcessSession(), RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  315. conn->queryRoot()->removeProp("Running"); // only one thing running at any given time and one thing with lockWrite access
  316. break;
  317. }
  318. }
  319. }
  320. virtual IPropertyTree * queryProgressTree()
  321. {
  322. if (!connected) connect();
  323. return progress;
  324. }
  325. virtual WUGraphState queryGraphState()
  326. {
  327. return (WUGraphState)queryProgressTree()->getPropInt("@_state", (unsigned)WUGraphUnknown);
  328. }
  329. virtual WUGraphState queryNodeState(WUGraphIDType nodeId)
  330. {
  331. StringBuffer path;
  332. path.append("node[@id=\"").append(nodeId).append("\"]/@_state");
  333. return (WUGraphState)queryProgressTree()->getPropInt(path.str(), (unsigned)WUGraphUnknown);
  334. }
  335. virtual IWUGraphProgress * update()
  336. {
  337. return new CGraphProgress(*this);
  338. }
  339. virtual unsigned queryFormatVersion()
  340. {
  341. if (!connected) connect();
  342. return formatVersion;
  343. }
  344. static bool packProgress(const char *wuid,bool pack)
  345. {
  346. StringBuffer path;
  347. path.append("/GraphProgress/").append(wuid);
  348. Owned<IRemoteConnection> conn(querySDS().connect(path.str(), myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT));
  349. if (!conn)
  350. return false;
  351. Owned<IPropertyTree> newt;
  352. MemoryBuffer buf;
  353. IPropertyTree *root = conn->queryRoot();
  354. if (root->getPropBin("Packed",buf)) {
  355. if (pack)
  356. return true;
  357. newt.setown(createPTree(buf));
  358. IPropertyTree *running = root->queryPropTree("Running");
  359. if (running)
  360. newt->setPropTree("Running",createPTreeFromIPT(running));
  361. }
  362. else {
  363. if (!pack)
  364. return true;
  365. newt.setown(createPTree(wuid));
  366. IPropertyTree *running = root->queryPropTree("Running");
  367. if (running) {
  368. newt->setPropTree("Running",createPTreeFromIPT(running));
  369. root->removeTree(running);
  370. }
  371. root->serialize(buf);
  372. newt->setPropBin("Packed",buf.length(),buf.bufferBase());
  373. }
  374. root->setPropTree(NULL,newt.getClear());
  375. return true;
  376. }
  377. private:
  378. Owned<IRemoteConnection> conn;
  379. IPropertyTree* progress;
  380. StringAttr wuid, graphName;
  381. StringBuffer rootPath;
  382. bool connected, connectedWrite;
  383. unsigned formatVersion;
  384. };
  385. class CLocalWUTimeStamp : public CInterface, implements IConstWUTimeStamp
  386. {
  387. Owned<IPropertyTree> p;
  388. public:
  389. IMPLEMENT_IINTERFACE;
  390. CLocalWUTimeStamp(IPropertyTree *p);
  391. virtual IStringVal & getApplication(IStringVal & str) const;
  392. virtual IStringVal & getEvent(IStringVal & str) const;
  393. virtual IStringVal & getDate(IStringVal & dt) const;
  394. };
  395. class CLocalWUAppValue : public CInterface, implements IConstWUAppValue
  396. {
  397. Owned<IPropertyTree> p;
  398. StringBuffer prop;
  399. public:
  400. IMPLEMENT_IINTERFACE;
  401. CLocalWUAppValue(IPropertyTree *p,unsigned child);
  402. virtual IStringVal & getApplication(IStringVal & str) const;
  403. virtual IStringVal & getName(IStringVal & str) const;
  404. virtual IStringVal & getValue(IStringVal & dt) const;
  405. };
  406. class CLocalWUStatistic : public CInterface, implements IConstWUStatistic
  407. {
  408. Owned<IPropertyTree> p;
  409. public:
  410. IMPLEMENT_IINTERFACE;
  411. CLocalWUStatistic(IPropertyTree *p);
  412. virtual IStringVal & getFullName(IStringVal & str) const;
  413. virtual IStringVal & getCreator(IStringVal & str) const;
  414. virtual IStringVal & getDescription(IStringVal & str) const;
  415. virtual IStringVal & getName(IStringVal & str) const;
  416. virtual IStringVal & getScope(IStringVal & str) const;
  417. virtual StatisticMeasure getKind() const;
  418. virtual unsigned __int64 getValue() const;
  419. virtual unsigned __int64 getCount() const;
  420. virtual unsigned __int64 getMax() const;
  421. };
  422. template <typename T, typename IT> struct CachedTags
  423. {
  424. CachedTags(): cached(false) {}
  425. void load(IPropertyTree* p,const char* xpath)
  426. {
  427. if (!cached)
  428. {
  429. assertex(tags.length() == 0);
  430. Owned<IPropertyTreeIterator> r = p->getElements(xpath);
  431. for (r->first(); r->isValid(); r->next())
  432. {
  433. IPropertyTree *rp = &r->query();
  434. rp->Link();
  435. tags.append(*new T(rp));
  436. }
  437. cached = true;
  438. }
  439. }
  440. void append(IPropertyTree * p)
  441. {
  442. tags.append(*new T(p));
  443. }
  444. operator IArrayOf<IT>&() { return tags; }
  445. void kill()
  446. {
  447. cached = false;
  448. tags.kill();
  449. }
  450. bool cached;
  451. IArrayOf<IT> tags;
  452. };
  453. template <> struct CachedTags<CLocalWUAppValue, IConstWUAppValue>
  454. {
  455. CachedTags(): cached(false) {}
  456. void load(IPropertyTree* p,const char* xpath)
  457. {
  458. if (!cached)
  459. {
  460. assertex(tags.length() == 0);
  461. Owned<IPropertyTreeIterator> r = p->getElements(xpath);
  462. for (r->first(); r->isValid(); r->next())
  463. {
  464. IPropertyTree *rp = &r->query();
  465. Owned<IPropertyTreeIterator> v = rp->getElements("*");
  466. unsigned pos = 1;
  467. for (v->first(); v->isValid(); v->next())
  468. {
  469. rp->Link();
  470. tags.append(*new CLocalWUAppValue(rp,pos++));
  471. }
  472. }
  473. cached = true;
  474. }
  475. }
  476. operator IArrayOf<IConstWUAppValue>&() { return tags; }
  477. void kill()
  478. {
  479. cached = false;
  480. tags.kill();
  481. }
  482. bool cached;
  483. IArrayOf<IConstWUAppValue> tags;
  484. };
  485. class CLocalWorkUnit : public CInterface, implements IConstWorkUnit , implements ISDSSubscription, implements IExtendedWUInterface
  486. {
  487. friend StringBuffer &exportWorkUnitToXML(const IConstWorkUnit *wu, StringBuffer &str, bool decodeGraphs, bool includeProgress);
  488. friend void exportWorkUnitToXMLFile(const IConstWorkUnit *wu, const char * filename, unsigned extraXmlFlags, bool decodeGraphs, bool includeProgress);
  489. // NOTE - order is important - we need to construct connection before p and (especially) destruct after p
  490. Owned<IRemoteConnection> connection;
  491. Owned<IPropertyTree> p;
  492. bool dirty;
  493. bool connectAtRoot;
  494. mutable bool abortDirty;
  495. mutable bool abortState;
  496. mutable CriticalSection crit;
  497. mutable Owned<IWUQuery> query;
  498. mutable Owned<IWUWebServicesInfo> webServicesInfo;
  499. mutable Owned<IWURoxieQueryInfo> roxieQueryInfo;
  500. mutable Owned<IWorkflowItemIterator> workflowIterator;
  501. mutable bool workflowIteratorCached;
  502. mutable bool resultsCached;
  503. mutable bool temporariesCached;
  504. mutable bool variablesCached;
  505. mutable bool exceptionsCached;
  506. mutable bool pluginsCached;
  507. mutable bool librariesCached;
  508. mutable bool activitiesCached;
  509. mutable bool webServicesInfoCached;
  510. mutable bool roxieQueryInfoCached;
  511. mutable bool timersCached;
  512. mutable IArrayOf<IWUActivity> activities;
  513. mutable IArrayOf<IWUPlugin> plugins;
  514. mutable IArrayOf<IWULibrary> libraries;
  515. mutable IArrayOf<IWUException> exceptions;
  516. mutable IArrayOf<IWUGraph> graphs;
  517. mutable IArrayOf<IWUResult> results;
  518. mutable IArrayOf<IWUResult> temporaries;
  519. mutable IArrayOf<IWUResult> variables;
  520. mutable IArrayOf<IWUTimer> timers;
  521. mutable CachedTags<CLocalWUTimeStamp,IConstWUTimeStamp> timestamps;
  522. mutable CachedTags<CLocalWUAppValue,IConstWUAppValue> appvalues;
  523. mutable CachedTags<CLocalWUStatistic,IConstWUStatistic> statistics;
  524. mutable Owned<IUserDescriptor> userDesc;
  525. Mutex locked;
  526. Owned<ISecManager> secMgr;
  527. Owned<ISecUser> secUser;
  528. mutable Owned<IPropertyTree> cachedGraphs;
  529. public:
  530. IMPLEMENT_IINTERFACE;
  531. CLocalWorkUnit(IRemoteConnection *_conn, ISecManager *secmgr, ISecUser *secuser, const char *parentWuid = NULL);
  532. CLocalWorkUnit(IRemoteConnection *_conn, IPropertyTree* root, ISecManager *secmgr, ISecUser *secuser);
  533. ~CLocalWorkUnit();
  534. CLocalWorkUnit(const char *dummyWuid, const char *parentWuid, ISecManager *secmgr, ISecUser *secuser);
  535. IPropertyTree *getUnpackedTree(bool includeProgress) const;
  536. ISecManager *querySecMgr(){return secMgr.get();}
  537. ISecUser *querySecUser(){return secUser.get();}
  538. void setSecIfcs(ISecManager *mgr, ISecUser*usr){secMgr.set(mgr); secUser.set(usr);}
  539. virtual bool aborting() const;
  540. virtual void forceReload();
  541. virtual WUAction getAction() const;
  542. virtual IStringVal& getActionEx(IStringVal & str) const;
  543. virtual IStringVal & getApplicationValue(const char * application, const char * propname, IStringVal & str) const;
  544. virtual int getApplicationValueInt(const char * application, const char * propname, int defVal) const;
  545. virtual IConstWUAppValueIterator & getApplicationValues() const;
  546. virtual bool hasWorkflow() const;
  547. virtual unsigned queryEventScheduledCount() const;
  548. virtual IPropertyTree * queryWorkflowTree() const;
  549. virtual IConstWorkflowItemIterator * getWorkflowItems() const;
  550. virtual IWorkflowItemArray * getWorkflowClone() const;
  551. virtual IConstLocalFileUploadIterator * getLocalFileUploads() const;
  552. virtual bool requiresLocalFileUpload() const;
  553. virtual bool getIsQueryService() const;
  554. virtual IStringVal & getClusterName(IStringVal & str) const;
  555. virtual unsigned getCombineQueries() const;
  556. virtual WUCompareMode getCompareMode() const;
  557. virtual IStringVal & getCustomerId(IStringVal & str) const;
  558. virtual bool hasDebugValue(const char * propname) const;
  559. virtual IStringVal & getDebugValue(const char * propname, IStringVal & str) const;
  560. virtual IStringIterator & getDebugValues() const;
  561. virtual IStringIterator & getDebugValues(const char *prop) const;
  562. virtual int getDebugValueInt(const char * propname, int defVal) const;
  563. virtual __int64 getDebugValueInt64(const char * propname, __int64 defVal) const;
  564. virtual bool getDebugValueBool(const char * propname, bool defVal) const;
  565. virtual unsigned getExceptionCount() const;
  566. virtual IConstWUExceptionIterator & getExceptions() const;
  567. virtual IConstWUResult * getGlobalByName(const char * name) const;
  568. virtual unsigned getGraphCount() const;
  569. virtual unsigned getSourceFileCount() const;
  570. virtual unsigned getResultCount() const;
  571. virtual unsigned getVariableCount() const;
  572. virtual unsigned getTimerCount() const;
  573. virtual unsigned getApplicationValueCount() const;
  574. virtual IConstWUGraphIterator & getGraphs(WUGraphType type) const;
  575. virtual IConstWUGraph * getGraph(const char *name) const;
  576. virtual IConstWUGraphProgress * getGraphProgress(const char * name) const;
  577. virtual IStringVal & getJobName(IStringVal & str) const;
  578. virtual IStringVal & getParentWuid(IStringVal & str) const;
  579. virtual IConstWUPlugin * getPluginByName(const char * name) const;
  580. virtual IConstWUPluginIterator & getPlugins() const;
  581. virtual IConstWULibraryIterator & getLibraries() const;
  582. virtual WUPriorityClass getPriority() const;
  583. virtual int getPriorityLevel() const;
  584. virtual int getPriorityValue() const;
  585. virtual IConstWUQuery * getQuery() const;
  586. virtual bool getRescheduleFlag() const;
  587. virtual IConstWUResult * getResultByName(const char * name) const;
  588. virtual IConstWUResult * getResultBySequence(unsigned seq) const;
  589. virtual unsigned getResultLimit() const;
  590. virtual IConstWUResultIterator & getResults() const;
  591. virtual IConstWUActivityIterator& getActivities() const;
  592. virtual IConstWUActivity* getActivity(__int64 id) const;
  593. virtual IStringVal & getScope(IStringVal & str) const;
  594. virtual IStringVal & getSecurityToken(IStringVal & str) const;
  595. virtual WUState getState() const;
  596. virtual IStringVal & getStateEx(IStringVal & str) const;
  597. virtual __int64 getAgentSession() const;
  598. virtual unsigned getAgentPID() const;
  599. virtual IStringVal & getStateDesc(IStringVal & str) const;
  600. virtual IConstWUResult * getTemporaryByName(const char * name) const;
  601. virtual IConstWUResultIterator & getTemporaries() const;
  602. virtual unsigned getTimerCount(const char * timerName) const;
  603. virtual unsigned getTimerDuration(const char * timerName) const;
  604. virtual IStringVal & getTimerDescription(const char * timerName, IStringVal & str) const;
  605. virtual IStringIterator & getTimers() const;
  606. virtual IConstWUTimerIterator & getTimerIterator() const;
  607. virtual IConstWUTimeStampIterator & getTimeStamps() const;
  608. virtual IConstWUStatisticIterator & getStatistics() const;
  609. virtual IConstWUStatistic * getStatistic(const char * name) const;
  610. virtual IConstWUWebServicesInfo * getWebServicesInfo() const;
  611. virtual IConstWURoxieQueryInfo * getRoxieQueryInfo() const;
  612. virtual IStringVal & getXmlParams(IStringVal & params) const;
  613. virtual const IPropertyTree *getXmlParams() const;
  614. virtual unsigned __int64 getHash() const;
  615. virtual IStringIterator *getLogs(const char *type, const char *component) const;
  616. virtual IStringIterator *getProcesses(const char *type) const;
  617. virtual IPropertyTreeIterator* getProcesses(const char *type, const char *instance) const;
  618. virtual bool getWuDate(unsigned & year, unsigned & month, unsigned& day);
  619. virtual IStringVal & getSnapshot(IStringVal & str) const;
  620. virtual IStringVal & getTimeStamp(const char * name, const char * instance, IStringVal & str) const;
  621. virtual IStringVal & getUser(IStringVal & str) const;
  622. virtual IStringVal & getWuScope(IStringVal & str) const;
  623. virtual IConstWUResult * getVariableByName(const char * name) const;
  624. virtual IConstWUResultIterator & getVariables() const;
  625. virtual IStringVal & getWuid(IStringVal & str) const;
  626. virtual bool getRunningGraph(IStringVal &graphName, WUGraphIDType &subId) const;
  627. virtual bool isProtected() const;
  628. virtual bool isPausing() const;
  629. virtual bool isBilled() const;
  630. virtual IWorkUnit& lock();
  631. virtual bool reload();
  632. virtual void requestAbort();
  633. virtual void subscribe(WUSubscribeOptions options);
  634. virtual unsigned calculateHash(unsigned prevHash);
  635. virtual void copyWorkUnit(IConstWorkUnit *cached, bool all);
  636. virtual unsigned queryFileUsage(const char *filename) const;
  637. virtual bool getCloneable() const;
  638. virtual IUserDescriptor * queryUserDescriptor() const;
  639. virtual unsigned getCodeVersion() const;
  640. virtual unsigned getWuidVersion() const;
  641. virtual void getBuildVersion(IStringVal & buildVersion, IStringVal & eclVersion) const;
  642. virtual IPropertyTree * getDiskUsageStats();
  643. virtual IPropertyTreeIterator & getFileIterator() const;
  644. virtual bool archiveWorkUnit(const char *base,bool del,bool ignoredllerrors,bool deleteOwned);
  645. virtual void packWorkUnit(bool pack=true);
  646. virtual IJlibDateTime & getTimeScheduled(IJlibDateTime &val) const;
  647. virtual IPropertyTreeIterator & getFilesReadIterator() const;
  648. virtual void protect(bool protectMode);
  649. virtual IConstWULibrary * getLibraryByName(const char * name) const;
  650. virtual unsigned getDebugAgentListenerPort() const;
  651. virtual IStringVal & getDebugAgentListenerIP(IStringVal &ip) const;
  652. void clearExceptions();
  653. void commit();
  654. IWUException *createException();
  655. void setTimeStamp(const char *name, const char *instance, const char *event);
  656. void addTimeStamp(const char * name, const char * instance, const char *event);
  657. void addProcess(const char *type, const char *instance, unsigned pid, const char *log);
  658. void setAction(WUAction action);
  659. void setApplicationValue(const char * application, const char * propname, const char * value, bool overwrite);
  660. void setApplicationValueInt(const char * application, const char * propname, int value, bool overwrite);
  661. void incEventScheduledCount();
  662. void setIsQueryService(bool value);
  663. void setCloneable(bool value);
  664. void setIsClone(bool value);
  665. void setClusterName(const char * value);
  666. void setCodeVersion(unsigned version, const char * buildVersion, const char * eclVersion);
  667. void setCombineQueries(unsigned combine);
  668. void setCompareMode(WUCompareMode value);
  669. void setCustomerId(const char * value);
  670. void setDebugValue(const char * propname, const char * value, bool overwrite);
  671. void setDebugValueInt(const char * propname, int value, bool overwrite);
  672. void setJobName(const char * value);
  673. void setPriority(WUPriorityClass cls);
  674. void setPriorityLevel(int level);
  675. void setRescheduleFlag(bool value);
  676. void setResultLimit(unsigned value);
  677. void setState(WUState state);
  678. void setStateEx(const char * text);
  679. void setAgentSession(__int64 sessionId);
  680. void setSecurityToken(const char *value);
  681. void setStatistic(const char * creator, const char * wuScope, const char * stat, const char * description, StatisticMeasure kind, unsigned __int64 value, unsigned __int64 count, unsigned __int64 maxValue, bool merge);
  682. void setTimerInfo(const char * name, unsigned ms, unsigned count, unsigned __int64 max);
  683. void setTracingValue(const char * propname, const char * value);
  684. void setTracingValueInt(const char * propname, int value);
  685. void setUser(const char * value);
  686. void setWuScope(const char * value);
  687. void setBilled(bool billed);
  688. void setSnapshot(const char * value);
  689. void setTimeStamp(const char *application, const char *instance, const char *event, bool add);
  690. void setDebugAgentListenerPort(unsigned port);
  691. void setDebugAgentListenerIP(const char * ip);
  692. void setXmlParams(const char *params);
  693. void setXmlParams(IPropertyTree *tree);
  694. void setHash(unsigned __int64 hash);
  695. IWorkflowItem* addWorkflowItem(unsigned wfid, WFType type, WFMode mode, unsigned success, unsigned failure, unsigned recovery, unsigned retriesAllowed, unsigned contingencyFor);
  696. IWorkflowItemIterator * updateWorkflowItems();
  697. void syncRuntimeWorkflow(IWorkflowItemArray * array);
  698. void resetWorkflow();
  699. void schedule();
  700. void deschedule();
  701. unsigned addLocalFileUpload(LocalFileUploadType type, char const * source, char const * destination, char const * eventTag);
  702. IWUResult * updateGlobalByName(const char * name);
  703. IWUGraph * updateGraph(const char * name);
  704. IWUQuery * updateQuery();
  705. IWUWebServicesInfo* updateWebServicesInfo(bool create);
  706. IWURoxieQueryInfo* updateRoxieQueryInfo(const char *wuid, const char *roxieClusterName);
  707. IWUActivity* updateActivity(__int64 id);
  708. IWUPlugin * updatePluginByName(const char * name);
  709. IWULibrary * updateLibraryByName(const char * name);
  710. IWUResult * updateResultByName(const char * name);
  711. IWUResult * updateResultBySequence(unsigned seq);
  712. IWUResult * updateTemporaryByName(const char * name);
  713. IWUResult * updateVariableByName(const char * name);
  714. void addFile(const char *fileName, StringArray *clusters, unsigned usageCount, WUFileKind fileKind, const char *graphOwner);
  715. void noteFileRead(IDistributedFile *file);
  716. void releaseFile(const char *fileName);
  717. void clearGraphProgress();
  718. void resetBeforeGeneration();
  719. void deleteTempFiles(const char *graph, bool deleteOwned, bool deleteJobOwned);
  720. void deleteTemporaries();
  721. void addDiskUsageStats(__int64 avgNodeUsage, unsigned minNode, __int64 minNodeUsage, unsigned maxNode, __int64 maxNodeUsage, __int64 graphId);
  722. void setTimeScheduled(const IJlibDateTime &val);
  723. // ILocalWorkUnit - used for debugging etc
  724. void loadXML(const char *xml);
  725. void serialize(MemoryBuffer &tgt);
  726. void deserialize(MemoryBuffer &src);
  727. IWorkUnit &lockRemote(bool commit);
  728. void unlockRemote(bool closing);
  729. void notify(SubscriptionId id, const char *xpath, SDSNotifyFlags flags, unsigned valueLen=0, const void *valueData=NULL);
  730. void abort();
  731. void cleanupAndDelete(bool deldll,bool deleteOwned, const StringArray *deleteExclusions=NULL);
  732. bool switchThorQueue(const char *cluster, IQueueSwitcher *qs);
  733. void setAllowedClusters(const char *value);
  734. IStringVal & getAllowedClusters(IStringVal & str) const;
  735. void remoteCheckAccess(IUserDescriptor *user, bool writeaccess) const;
  736. void setAllowAutoQueueSwitch(bool val);
  737. bool getAllowAutoQueueSwitch() const;
  738. void setLibraryInformation(const char * name, unsigned interfaceHash, unsigned definitionHash);
  739. protected:
  740. IConstWUStatistic * getStatisticByDescription(const char * name) const;
  741. private:
  742. void init();
  743. IWUGraph *createGraph();
  744. IWUResult *createResult();
  745. void loadGraphs() const;
  746. void loadResults() const;
  747. void loadTemporaries() const;
  748. void loadVariables() const;
  749. void loadExceptions() const;
  750. void loadPlugins() const;
  751. void loadLibraries() const;
  752. void loadClusters() const;
  753. void loadActivities() const;
  754. void loadTimers() const;
  755. void unsubscribe();
  756. void checkAgentRunning(WUState & state);
  757. // MORE - the two could be a bit more similar...
  758. class CWorkUnitWatcher : public CInterface, implements ISDSSubscription
  759. {
  760. ISDSSubscription *parent; // not linked - it links me
  761. SubscriptionId change;
  762. bool sub;
  763. public:
  764. IMPLEMENT_IINTERFACE;
  765. CWorkUnitWatcher(ISDSSubscription *_parent, const char *wuid, bool _sub) : parent(_parent), sub(_sub)
  766. {
  767. StringBuffer wuRoot;
  768. getXPath(wuRoot, wuid);
  769. change = querySDS().subscribe(wuRoot.str(), *this, sub);
  770. }
  771. ~CWorkUnitWatcher()
  772. {
  773. assertex(change==0);
  774. }
  775. bool watchingChildren()
  776. {
  777. return sub;
  778. }
  779. void unsubscribe()
  780. {
  781. querySDS().unsubscribe(change);
  782. change = 0;
  783. }
  784. void notify(SubscriptionId id, const char *xpath, SDSNotifyFlags flags, unsigned valueLen, const void *valueData)
  785. {
  786. parent->notify(id, xpath, flags, valueLen, valueData);
  787. }
  788. };
  789. class CWorkUnitAbortWatcher : public CInterface, implements ISDSSubscription
  790. {
  791. CLocalWorkUnit *parent; // not linked - it links me
  792. SubscriptionId abort;
  793. public:
  794. IMPLEMENT_IINTERFACE;
  795. CWorkUnitAbortWatcher(CLocalWorkUnit *_parent, const char *wuid) : parent(_parent)
  796. {
  797. StringBuffer wuRoot;
  798. wuRoot.append("/WorkUnitAborts/").append(wuid);
  799. abort = querySDS().subscribe(wuRoot.str(), *this);
  800. }
  801. ~CWorkUnitAbortWatcher()
  802. {
  803. assertex(abort==0);
  804. }
  805. void unsubscribe()
  806. {
  807. querySDS().unsubscribe(abort);
  808. abort = 0;
  809. }
  810. void notify(SubscriptionId id, const char *xpath, SDSNotifyFlags flags, unsigned valueLen, const void *valueData)
  811. {
  812. parent->abort();
  813. }
  814. };
  815. Owned<CWorkUnitAbortWatcher> abortWatcher;
  816. Owned<CWorkUnitWatcher> changeWatcher;
  817. void ensureGraphsUnpacked ()
  818. {
  819. IPropertyTree *t = p->queryPropTree("PackedGraphs");
  820. MemoryBuffer buf;
  821. if (t&&t->getPropBin(NULL,buf)) {
  822. cachedGraphs.clear();
  823. IPropertyTree *st = createPTree(buf);
  824. if (st) {
  825. p->setPropTree("Graphs",st);
  826. p->removeTree(t);
  827. }
  828. }
  829. }
  830. };
  831. class CLockedWorkUnit : public CInterface, implements ILocalWorkUnit, implements IExtendedWUInterface
  832. {
  833. public:
  834. Owned<CLocalWorkUnit> c;
  835. IMPLEMENT_IINTERFACE;
  836. CLockedWorkUnit(CLocalWorkUnit *_c) : c(_c) {}
  837. ~CLockedWorkUnit()
  838. {
  839. if (workUnitTraceLevel > 1)
  840. {
  841. StringAttr x;
  842. StringAttrAdaptor strval(x);
  843. getWuid(strval);
  844. PrintLog("Releasing locked workunit %s", x.get());
  845. }
  846. if (c)
  847. c->unlockRemote(c->IsShared());
  848. }
  849. void setSecIfcs(ISecManager *mgr, ISecUser*usr){c->setSecIfcs(mgr, usr);}
  850. virtual IConstWorkUnit * unlock()
  851. {
  852. c->unlockRemote(c->IsShared());
  853. return c.getClear();
  854. }
  855. virtual bool aborting() const
  856. { return c->aborting(); }
  857. virtual void forceReload()
  858. { UNIMPLEMENTED; }
  859. virtual WUAction getAction() const
  860. { return c->getAction(); }
  861. virtual IStringVal& getActionEx(IStringVal & str) const
  862. { return c->getActionEx(str); }
  863. virtual IStringVal & getApplicationValue(const char * application, const char * propname, IStringVal & str) const
  864. { return c->getApplicationValue(application, propname, str); }
  865. virtual int getApplicationValueInt(const char * application, const char * propname, int defVal) const
  866. { return c->getApplicationValueInt(application, propname, defVal); }
  867. virtual IConstWUAppValueIterator & getApplicationValues() const
  868. { return c->getApplicationValues(); }
  869. virtual bool hasWorkflow() const
  870. { return c->hasWorkflow(); }
  871. virtual unsigned queryEventScheduledCount() const
  872. { return c->queryEventScheduledCount(); }
  873. virtual IPropertyTree * queryWorkflowTree() const
  874. { return c->queryWorkflowTree(); }
  875. virtual IConstWorkflowItemIterator * getWorkflowItems() const
  876. { return c->getWorkflowItems(); }
  877. virtual IWorkflowItemArray * getWorkflowClone() const
  878. { return c->getWorkflowClone(); }
  879. virtual bool requiresLocalFileUpload() const
  880. { return c->requiresLocalFileUpload(); }
  881. virtual IConstLocalFileUploadIterator * getLocalFileUploads() const
  882. { return c->getLocalFileUploads(); }
  883. virtual bool getIsQueryService() const
  884. { return c->getIsQueryService(); }
  885. virtual bool getCloneable() const
  886. { return c->getCloneable(); }
  887. virtual IUserDescriptor * queryUserDescriptor() const
  888. { return c->queryUserDescriptor(); }
  889. virtual IStringVal & getClusterName(IStringVal & str) const
  890. { return c->getClusterName(str); }
  891. virtual unsigned getCodeVersion() const
  892. { return c->getCodeVersion(); }
  893. virtual unsigned getWuidVersion() const
  894. { return c->getWuidVersion(); }
  895. virtual void getBuildVersion(IStringVal & buildVersion, IStringVal & eclVersion) const
  896. { c->getBuildVersion(buildVersion, eclVersion); }
  897. virtual unsigned getCombineQueries() const
  898. { return c->getCombineQueries(); }
  899. virtual WUCompareMode getCompareMode() const
  900. { return c->getCompareMode(); }
  901. virtual IStringVal & getCustomerId(IStringVal & str) const
  902. { return c->getCustomerId(str); }
  903. virtual bool hasDebugValue(const char * propname) const
  904. { return c->hasDebugValue(propname); }
  905. virtual IStringVal & getDebugValue(const char * propname, IStringVal & str) const
  906. { return c->getDebugValue(propname, str); }
  907. virtual int getDebugValueInt(const char * propname, int defVal) const
  908. { return c->getDebugValueInt(propname, defVal); }
  909. virtual __int64 getDebugValueInt64(const char * propname, __int64 defVal) const
  910. { return c->getDebugValueInt64(propname, defVal); }
  911. virtual bool getDebugValueBool(const char * propname, bool defVal) const
  912. { return c->getDebugValueBool(propname, defVal); }
  913. virtual IStringIterator & getDebugValues() const
  914. { return c->getDebugValues(NULL); }
  915. virtual IStringIterator & getDebugValues(const char *prop) const
  916. { return c->getDebugValues(prop); }
  917. virtual unsigned getExceptionCount() const
  918. { return c->getExceptionCount(); }
  919. virtual IConstWUExceptionIterator & getExceptions() const
  920. { return c->getExceptions(); }
  921. virtual unsigned getGraphCount() const
  922. { return c->getGraphCount(); }
  923. virtual unsigned getSourceFileCount() const
  924. { return c->getSourceFileCount(); }
  925. virtual unsigned getResultCount() const
  926. { return c->getResultCount(); }
  927. virtual unsigned getVariableCount() const
  928. { return c->getVariableCount(); }
  929. virtual unsigned getTimerCount() const
  930. { return c->getTimerCount(); }
  931. virtual unsigned getApplicationValueCount() const
  932. { return c->getApplicationValueCount(); }
  933. virtual IConstWUGraphIterator & getGraphs(WUGraphType type) const
  934. { return c->getGraphs(type); }
  935. virtual IConstWUGraph * getGraph(const char *name) const
  936. { return c->getGraph(name); }
  937. virtual IConstWUGraphProgress * getGraphProgress(const char * name) const
  938. { return c->getGraphProgress(name); }
  939. virtual IStringVal & getJobName(IStringVal & str) const
  940. { return c->getJobName(str); }
  941. virtual IStringVal & getParentWuid(IStringVal & str) const
  942. { return c->getParentWuid(str); }
  943. virtual IConstWUPlugin * getPluginByName(const char * name) const
  944. { return c->getPluginByName(name); }
  945. virtual IConstWUPluginIterator & getPlugins() const
  946. { return c->getPlugins(); }
  947. virtual IConstWULibrary* getLibraryByName(const char *name) const
  948. { return c->getLibraryByName(name); }
  949. virtual IConstWULibraryIterator & getLibraries() const
  950. { return c->getLibraries(); }
  951. virtual WUPriorityClass getPriority() const
  952. { return c->getPriority(); }
  953. virtual int getPriorityLevel() const
  954. { return c->getPriorityLevel(); }
  955. virtual int getPriorityValue() const
  956. { return c->getPriorityValue(); }
  957. virtual IConstWUQuery * getQuery() const
  958. { return c->getQuery(); }
  959. virtual IConstWUWebServicesInfo * getWebServicesInfo() const
  960. { return c->getWebServicesInfo(); }
  961. virtual IConstWURoxieQueryInfo* getRoxieQueryInfo() const
  962. { return c->getRoxieQueryInfo(); }
  963. virtual bool getRescheduleFlag() const
  964. { return c->getRescheduleFlag(); }
  965. virtual IConstWUResult * getResultByName(const char * name) const
  966. { return c->getResultByName(name); }
  967. virtual IConstWUResult * getResultBySequence(unsigned seq) const
  968. { return c->getResultBySequence(seq); }
  969. virtual unsigned getResultLimit() const
  970. { return c->getResultLimit(); }
  971. virtual IConstWUResultIterator & getResults() const
  972. { return c->getResults(); }
  973. virtual IConstWUActivityIterator & getActivities() const
  974. { return c->getActivities(); }
  975. virtual IConstWUActivity * getActivity(__int64 id) const
  976. { return c->getActivity(id); }
  977. virtual IStringVal & getScope(IStringVal & str) const
  978. { return c->getScope(str); }
  979. virtual IStringVal & getSecurityToken(IStringVal & str) const
  980. { return c->getSecurityToken(str); }
  981. virtual WUState getState() const
  982. { return c->getState(); }
  983. virtual IStringVal & getStateEx(IStringVal & str) const
  984. { return c->getStateEx(str); }
  985. virtual __int64 getAgentSession() const
  986. { return c->getAgentSession(); }
  987. virtual unsigned getAgentPID() const
  988. { return c->getAgentPID(); }
  989. virtual IStringVal & getStateDesc(IStringVal & str) const
  990. { return c->getStateDesc(str); }
  991. virtual bool getRunningGraph(IStringVal & graphName, WUGraphIDType & subId) const
  992. { return c->getRunningGraph(graphName, subId); }
  993. virtual unsigned getTimerCount(const char * timerName) const
  994. { return c->getTimerCount(timerName); }
  995. virtual unsigned getTimerDuration(const char * timerName) const
  996. { return c->getTimerDuration(timerName); }
  997. virtual IStringVal & getTimerDescription(const char * timerName, IStringVal & str) const
  998. { return c->getTimerDescription(timerName, str); }
  999. virtual IStringVal & getTimeStamp(const char * name, const char * instance, IStringVal & str) const
  1000. { return c->getTimeStamp(name, instance, str); }
  1001. virtual IStringIterator & getTimers() const
  1002. { return c->getTimers(); }
  1003. virtual IConstWUTimerIterator & getTimerIterator() const
  1004. { return c->getTimerIterator(); }
  1005. virtual IConstWUTimeStampIterator & getTimeStamps() const
  1006. { return c->getTimeStamps(); }
  1007. virtual IConstWUStatisticIterator & getStatistics() const
  1008. { return c->getStatistics(); }
  1009. virtual IConstWUStatistic * getStatistic(const char * name) const
  1010. { return c->getStatistic(name); }
  1011. virtual bool getWuDate(unsigned & year, unsigned & month, unsigned& day)
  1012. { return c->getWuDate(year,month,day);}
  1013. virtual IStringVal & getSnapshot(IStringVal & str) const
  1014. { return c->getSnapshot(str); }
  1015. virtual IStringVal & getUser(IStringVal & str) const
  1016. { return c->getUser(str); }
  1017. virtual IStringVal & getWuScope(IStringVal & str) const
  1018. { return c->getWuScope(str); }
  1019. virtual IStringVal & getWuid(IStringVal & str) const
  1020. { return c->getWuid(str); }
  1021. virtual IConstWUResult * getGlobalByName(const char * name) const
  1022. { return c->getGlobalByName(name); }
  1023. virtual IConstWUResult * getTemporaryByName(const char * name) const
  1024. { return c->getTemporaryByName(name); }
  1025. virtual IConstWUResultIterator & getTemporaries() const
  1026. { return c->getTemporaries(); }
  1027. virtual IConstWUResult * getVariableByName(const char * name) const
  1028. { return c->getVariableByName(name); }
  1029. virtual IConstWUResultIterator & getVariables() const
  1030. { return c->getVariables(); }
  1031. virtual bool isProtected() const
  1032. { return c->isProtected(); }
  1033. virtual bool isPausing() const
  1034. { return c->isPausing(); }
  1035. virtual bool isBilled() const
  1036. { return c->isBilled(); }
  1037. virtual IWorkUnit & lock()
  1038. { ((CInterface *)this)->Link(); return (IWorkUnit &) *this; }
  1039. virtual bool reload()
  1040. { UNIMPLEMENTED; }
  1041. virtual void subscribe(WUSubscribeOptions options)
  1042. { c->subscribe(options); }
  1043. virtual void requestAbort()
  1044. { c->requestAbort(); }
  1045. virtual unsigned calculateHash(unsigned prevHash)
  1046. { return c->calculateHash(prevHash); }
  1047. virtual void copyWorkUnit(IConstWorkUnit *cached, bool all)
  1048. { c->copyWorkUnit(cached, all); }
  1049. virtual bool archiveWorkUnit(const char *base,bool del,bool deldll,bool deleteOwned)
  1050. { return c->archiveWorkUnit(base,del,deldll,deleteOwned); }
  1051. virtual void packWorkUnit(bool pack)
  1052. { c->packWorkUnit(pack); }
  1053. virtual unsigned queryFileUsage(const char *filename) const
  1054. { return c->queryFileUsage(filename); }
  1055. virtual IJlibDateTime & getTimeScheduled(IJlibDateTime &val) const
  1056. { return c->getTimeScheduled(val); }
  1057. virtual unsigned getDebugAgentListenerPort() const
  1058. { return c->getDebugAgentListenerPort(); }
  1059. virtual IStringVal & getDebugAgentListenerIP(IStringVal &ip) const
  1060. { return c->getDebugAgentListenerIP(ip); }
  1061. virtual IStringVal & getXmlParams(IStringVal & params) const
  1062. { return c->getXmlParams(params); }
  1063. virtual const IPropertyTree *getXmlParams() const
  1064. { return c->getXmlParams(); }
  1065. virtual unsigned __int64 getHash() const
  1066. { return c->getHash(); }
  1067. virtual IStringIterator *getLogs(const char *type, const char *instance) const
  1068. { return c->getLogs(type, instance); }
  1069. virtual IStringIterator *getProcesses(const char *type) const
  1070. { return c->getProcesses(type); }
  1071. virtual IPropertyTreeIterator* getProcesses(const char *type, const char *instance) const
  1072. { return c->getProcesses(type, instance); }
  1073. virtual void clearExceptions()
  1074. { c->clearExceptions(); }
  1075. virtual void commit()
  1076. { c->commit(); }
  1077. virtual IWUException * createException()
  1078. { return c->createException(); }
  1079. virtual void setTimeStamp(const char * name, const char * instance, const char *event)
  1080. { c->setTimeStamp(name, instance, event); }
  1081. virtual void addTimeStamp(const char * name, const char * instance, const char *event)
  1082. { c->addTimeStamp(name, instance, event); }
  1083. virtual void addProcess(const char *type, const char *instance, unsigned pid, const char *log)
  1084. { c->addProcess(type, instance, pid, log); }
  1085. virtual void protect(bool protectMode)
  1086. { c->protect(protectMode); }
  1087. virtual void setBilled(bool billed)
  1088. { c->setBilled(billed); }
  1089. virtual void setAction(WUAction action)
  1090. { c->setAction(action); }
  1091. virtual void setApplicationValue(const char * application, const char * propname, const char * value, bool overwrite)
  1092. { c->setApplicationValue(application, propname, value, overwrite); }
  1093. virtual void setApplicationValueInt(const char * application, const char * propname, int value, bool overwrite)
  1094. { c->setApplicationValueInt(application, propname, value, overwrite); }
  1095. virtual void incEventScheduledCount()
  1096. { c->incEventScheduledCount(); }
  1097. virtual void setIsQueryService(bool value)
  1098. { c->setIsQueryService(value); }
  1099. virtual void setCloneable(bool value)
  1100. { c->setCloneable(value); }
  1101. virtual void setIsClone(bool value)
  1102. { c->setIsClone(value); }
  1103. virtual void setClusterName(const char * value)
  1104. { c->setClusterName(value); }
  1105. virtual void setCodeVersion(unsigned version, const char * buildVersion, const char * eclVersion)
  1106. { c->setCodeVersion(version, buildVersion, eclVersion); }
  1107. virtual void setCombineQueries(unsigned combine)
  1108. { c->setCombineQueries(combine); }
  1109. virtual void setCompareMode(WUCompareMode value)
  1110. { c->setCompareMode(value); }
  1111. virtual void setCustomerId(const char * value)
  1112. { c->setCustomerId(value); }
  1113. virtual void setDebugValue(const char * propname, const char * value, bool overwrite)
  1114. { c->setDebugValue(propname, value, overwrite); }
  1115. virtual void setDebugValueInt(const char * propname, int value, bool overwrite)
  1116. { c->setDebugValueInt(propname, value, overwrite); }
  1117. virtual void setJobName(const char * value)
  1118. { c->setJobName(value); }
  1119. virtual void setPriority(WUPriorityClass cls)
  1120. { c->setPriority(cls); }
  1121. virtual void setPriorityLevel(int level)
  1122. { c->setPriorityLevel(level); }
  1123. virtual void setRescheduleFlag(bool value)
  1124. { c->setRescheduleFlag(value); }
  1125. virtual void setResultLimit(unsigned value)
  1126. { c->setResultLimit(value); }
  1127. virtual void setSecurityToken(const char *value)
  1128. { c->setSecurityToken(value); }
  1129. virtual void setState(WUState state)
  1130. { c->setState(state); }
  1131. virtual void setStateEx(const char * text)
  1132. { c->setStateEx(text); }
  1133. virtual void setAgentSession(__int64 sessionId)
  1134. { c->setAgentSession(sessionId); }
  1135. virtual void setTimerInfo(const char * name, unsigned ms, unsigned count, unsigned __int64 max)
  1136. { c->setTimerInfo(name, ms, count, max); }
  1137. virtual void setStatistic(const char * creator, const char * wuScope, const char * stat, const char * description, StatisticMeasure kind, unsigned __int64 value, unsigned __int64 count, unsigned __int64 maxValue, bool merge)
  1138. { c->setStatistic(creator, wuScope, stat, description, kind, value, count, maxValue, merge); }
  1139. virtual void setTracingValue(const char * propname, const char * value)
  1140. { c->setTracingValue(propname, value); }
  1141. virtual void setTracingValueInt(const char * propname, int value)
  1142. { c->setTracingValueInt(propname, value); }
  1143. virtual void setUser(const char * value)
  1144. { c->setUser(value); }
  1145. virtual void setWuScope(const char * value)
  1146. {
  1147. if (value && *value)
  1148. {
  1149. ISecManager *secmgr=c->querySecMgr();
  1150. ISecUser *secusr=c->querySecUser();
  1151. if (!secmgr || !secusr)
  1152. throw MakeStringException(WUERR_SecurityNotAvailable, "Trying to change workunit scope without security interfaces available");
  1153. if (checkWuScopeSecAccess(value, *secmgr, secusr, SecAccess_Write, "Change Scope", true, true))
  1154. c->setWuScope(value);
  1155. }
  1156. }
  1157. virtual IWorkflowItem* addWorkflowItem(unsigned wfid, WFType type, WFMode mode, unsigned success, unsigned failure, unsigned recovery, unsigned retriesAllowed, unsigned contingencyFor)
  1158. { return c->addWorkflowItem(wfid, type, mode, success, failure, recovery, retriesAllowed, contingencyFor); }
  1159. virtual void syncRuntimeWorkflow(IWorkflowItemArray * array)
  1160. { c->syncRuntimeWorkflow(array); }
  1161. virtual IWorkflowItemIterator * updateWorkflowItems()
  1162. { return c->updateWorkflowItems(); }
  1163. virtual void resetWorkflow()
  1164. { c->resetWorkflow(); }
  1165. virtual void schedule()
  1166. { c->schedule(); }
  1167. virtual void deschedule()
  1168. { c->deschedule(); }
  1169. virtual unsigned addLocalFileUpload(LocalFileUploadType type, char const * source, char const * destination, char const * eventTag)
  1170. { return c->addLocalFileUpload(type, source, destination, eventTag); }
  1171. virtual IWUResult * updateGlobalByName(const char * name)
  1172. { return c->updateGlobalByName(name); }
  1173. virtual IWUGraph * updateGraph(const char * name)
  1174. { return c->updateGraph(name); }
  1175. virtual IWUQuery * updateQuery()
  1176. { return c->updateQuery(); }
  1177. virtual IWUWebServicesInfo * updateWebServicesInfo(bool create)
  1178. { return c->updateWebServicesInfo(create); }
  1179. virtual IWURoxieQueryInfo * updateRoxieQueryInfo(const char *wuid, const char *roxieClusterName)
  1180. { return c->updateRoxieQueryInfo(wuid, roxieClusterName); }
  1181. virtual IWUActivity * updateActivity(__int64 id)
  1182. { return c->updateActivity(id); }
  1183. virtual IWUPlugin * updatePluginByName(const char * name)
  1184. { return c->updatePluginByName(name); }
  1185. virtual IWULibrary * updateLibraryByName(const char * name)
  1186. { return c->updateLibraryByName(name); }
  1187. virtual IWUResult * updateResultByName(const char * name)
  1188. { return c->updateResultByName(name); }
  1189. virtual IWUResult * updateResultBySequence(unsigned seq)
  1190. { return c->updateResultBySequence(seq); }
  1191. virtual IWUResult * updateTemporaryByName(const char * name)
  1192. { return c->updateTemporaryByName(name); }
  1193. virtual IWUResult * updateVariableByName(const char * name)
  1194. { return c->updateVariableByName(name); }
  1195. virtual void addFile(const char *fileName, StringArray *clusters, unsigned usageCount, WUFileKind fileKind, const char *graphOwner)
  1196. { c->addFile(fileName, clusters, usageCount, fileKind, graphOwner); }
  1197. virtual void noteFileRead(IDistributedFile *file)
  1198. { c->noteFileRead(file); }
  1199. virtual void releaseFile(const char *fileName)
  1200. { c->releaseFile(fileName); }
  1201. virtual void clearGraphProgress()
  1202. { c->clearGraphProgress(); }
  1203. virtual void resetBeforeGeneration()
  1204. { c->resetBeforeGeneration(); }
  1205. virtual void deleteTempFiles(const char *graph, bool deleteOwned, bool deleteJobOwned)
  1206. { c->deleteTempFiles(graph, deleteOwned, deleteJobOwned); }
  1207. virtual void deleteTemporaries()
  1208. { c->deleteTemporaries(); }
  1209. virtual void addDiskUsageStats(__int64 avgNodeUsage, unsigned minNode, __int64 minNodeUsage, unsigned maxNode, __int64 maxNodeUsage, __int64 graphId)
  1210. { c->addDiskUsageStats(avgNodeUsage, minNode, minNodeUsage, maxNode, maxNodeUsage, graphId); }
  1211. virtual IPropertyTree * getDiskUsageStats()
  1212. { return c->getDiskUsageStats(); }
  1213. virtual IPropertyTreeIterator & getFileIterator() const
  1214. { return c->getFileIterator(); }
  1215. virtual IPropertyTreeIterator & getFilesReadIterator() const
  1216. { return c->getFilesReadIterator(); }
  1217. virtual void setSnapshot(const char * value)
  1218. { c->setSnapshot(value); }
  1219. virtual void setTimeScheduled(const IJlibDateTime &val)
  1220. { c->setTimeScheduled(val); }
  1221. virtual void setDebugAgentListenerPort(unsigned port)
  1222. { c->setDebugAgentListenerPort(port); }
  1223. virtual void setDebugAgentListenerIP(const char * ip)
  1224. { c->setDebugAgentListenerIP(ip); }
  1225. virtual void setXmlParams(const char *params)
  1226. { c->setXmlParams(params); }
  1227. virtual void setXmlParams(IPropertyTree *tree)
  1228. { c->setXmlParams(tree); }
  1229. virtual void setHash(unsigned __int64 hash)
  1230. { c->setHash(hash); }
  1231. // ILocalWorkUnit - used for debugging etc
  1232. virtual void loadXML(const char *xml)
  1233. { c->loadXML(xml); }
  1234. virtual void serialize(MemoryBuffer &tgt)
  1235. { c->serialize(tgt); }
  1236. virtual void deserialize(MemoryBuffer &src)
  1237. { c->deserialize(src); }
  1238. virtual bool switchThorQueue(const char *cluster, IQueueSwitcher *qs)
  1239. { return c->switchThorQueue(cluster,qs); }
  1240. virtual void setAllowedClusters(const char *value)
  1241. { c->setAllowedClusters(value); }
  1242. virtual IStringVal& getAllowedClusters(IStringVal &str) const
  1243. { return c->getAllowedClusters(str); }
  1244. virtual void remoteCheckAccess(IUserDescriptor *user, bool writeaccess) const
  1245. { c->remoteCheckAccess(user,writeaccess); }
  1246. virtual void setAllowAutoQueueSwitch(bool val)
  1247. { c->setAllowAutoQueueSwitch(val); }
  1248. virtual bool getAllowAutoQueueSwitch() const
  1249. { return c->getAllowAutoQueueSwitch(); }
  1250. virtual void setLibraryInformation(const char * name, unsigned interfaceHash, unsigned definitionHash)
  1251. { c->setLibraryInformation(name, interfaceHash, definitionHash); }
  1252. virtual void setResultInt(const char * name, unsigned sequence, __int64 val)
  1253. {
  1254. Owned<IWUResult> r = updateResult(name, sequence);
  1255. if (r)
  1256. {
  1257. r->setResultInt(val);
  1258. r->setResultStatus(ResultStatusCalculated);
  1259. }
  1260. }
  1261. virtual void setResultUInt(const char * name, unsigned sequence, unsigned __int64 val)
  1262. {
  1263. Owned<IWUResult> r = updateResult(name, sequence);
  1264. if (r)
  1265. {
  1266. r->setResultUInt(val);
  1267. r->setResultStatus(ResultStatusCalculated);
  1268. }
  1269. }
  1270. virtual void setResultReal(const char *name, unsigned sequence, double val)
  1271. {
  1272. Owned<IWUResult> r = updateResult(name, sequence);
  1273. if (r)
  1274. {
  1275. r->setResultReal(val);
  1276. r->setResultStatus(ResultStatusCalculated);
  1277. }
  1278. }
  1279. virtual void setResultVarString(const char * stepname, unsigned sequence, const char *val)
  1280. {
  1281. setResultString(stepname, sequence, strlen(val), val);
  1282. }
  1283. virtual void setResultVarUnicode(const char * stepname, unsigned sequence, UChar const *val)
  1284. {
  1285. setResultUnicode(stepname, sequence, rtlUnicodeStrlen(val), val);
  1286. }
  1287. virtual void setResultString(const char * stepname, unsigned sequence, int len, const char *val)
  1288. {
  1289. doSetResultString(type_string, stepname, sequence, len, val);
  1290. }
  1291. virtual void setResultData(const char * stepname, unsigned sequence, int len, const void *val)
  1292. {
  1293. doSetResultString(type_data, stepname, sequence, len, (const char *)val);
  1294. }
  1295. virtual void setResultRaw(const char * name, unsigned sequence, int len, const void *val)
  1296. {
  1297. Owned<IWUResult> r = updateResult(name, sequence);
  1298. if (r)
  1299. {
  1300. r->setResultRaw(len, val, ResultFormatRaw);
  1301. r->setResultStatus(ResultStatusCalculated);
  1302. }
  1303. }
  1304. virtual void setResultSet(const char * name, unsigned sequence, bool isAll, size32_t len, const void *val, ISetToXmlTransformer *)
  1305. {
  1306. Owned<IWUResult> r = updateResult(name, sequence);
  1307. if (r)
  1308. {
  1309. r->setResultIsAll(isAll);
  1310. r->setResultRaw(len, val, ResultFormatRaw);
  1311. r->setResultStatus(ResultStatusCalculated);
  1312. }
  1313. }
  1314. virtual void setResultUnicode(const char * name, unsigned sequence, int len, UChar const * val)
  1315. {
  1316. Owned<IWUResult> r = updateResult(name, sequence);
  1317. if (r)
  1318. {
  1319. r->setResultUnicode((char const *)val, len);
  1320. r->setResultStatus(ResultStatusCalculated);
  1321. }
  1322. }
  1323. virtual void setResultBool(const char *name, unsigned sequence, bool val)
  1324. {
  1325. Owned<IWUResult> r = updateResult(name, sequence);
  1326. if (r)
  1327. {
  1328. r->setResultBool(val);
  1329. r->setResultStatus(ResultStatusCalculated);
  1330. }
  1331. }
  1332. virtual void setResultDecimal(const char *name, unsigned sequence, int len, int precision, bool isSigned, const void *val)
  1333. {
  1334. Owned<IWUResult> r = updateResult(name, sequence);
  1335. if (r)
  1336. {
  1337. r->setResultDecimal(val, len);
  1338. r->setResultStatus(ResultStatusCalculated);
  1339. }
  1340. }
  1341. virtual void setResultDataset(const char * name, unsigned sequence, size32_t len, const void *val, unsigned numRows, bool extend)
  1342. {
  1343. Owned<IWUResult> r = updateResult(name, sequence);
  1344. if (r)
  1345. {
  1346. __int64 totalRows = numRows;
  1347. if (extend)
  1348. {
  1349. totalRows += r->getResultRowCount();
  1350. r->addResultRaw(len, val, ResultFormatRaw);
  1351. }
  1352. else
  1353. r->setResultRaw(len, val, ResultFormatRaw);
  1354. r->setResultStatus(ResultStatusCalculated);
  1355. r->setResultRowCount(totalRows);
  1356. r->setResultTotalRowCount(totalRows);
  1357. }
  1358. }
  1359. protected:
  1360. IWUResult *updateResult(const char *name, unsigned sequence)
  1361. {
  1362. Owned <IWUResult> result = updateWorkUnitResult(this, name, sequence);
  1363. if (result)
  1364. {
  1365. SCMStringBuffer rname;
  1366. if (!result->getResultName(rname).length())
  1367. result->setResultName(name);
  1368. }
  1369. return result.getClear();
  1370. }
  1371. void doSetResultString(type_t type, const char *name, unsigned sequence, int len, const char *val)
  1372. {
  1373. Owned<IWUResult> r = updateResult(name, sequence);
  1374. if (r)
  1375. {
  1376. r->setResultString(val, len);
  1377. r->setResultStatus(ResultStatusCalculated);
  1378. }
  1379. }
  1380. };
  1381. class CLocalWUAssociated : public CInterface, implements IConstWUAssociatedFile
  1382. {
  1383. Owned<IPropertyTree> p;
  1384. public:
  1385. IMPLEMENT_IINTERFACE;
  1386. CLocalWUAssociated(IPropertyTree *p);
  1387. virtual WUFileType getType() const;
  1388. virtual IStringVal & getDescription(IStringVal & ret) const;
  1389. virtual IStringVal & getIp(IStringVal & ret) const;
  1390. virtual IStringVal & getName(IStringVal & ret) const;
  1391. virtual IStringVal & getNameTail(IStringVal & ret) const;
  1392. virtual unsigned getCrc() const;
  1393. };
  1394. class CLocalWUQuery : public CInterface, implements IWUQuery
  1395. {
  1396. Owned<IPropertyTree> p;
  1397. mutable IArrayOf<IConstWUAssociatedFile> associated;
  1398. mutable CriticalSection crit;
  1399. mutable bool associatedCached;
  1400. private:
  1401. void addSpecialCaseAssociated(WUFileType type, const char * propname, unsigned crc) const;
  1402. void loadAssociated() const;
  1403. public:
  1404. IMPLEMENT_IINTERFACE;
  1405. CLocalWUQuery(IPropertyTree *p);
  1406. virtual WUQueryType getQueryType() const;
  1407. virtual IStringVal& getQueryText(IStringVal &str) const;
  1408. virtual IStringVal& getQueryShortText(IStringVal &str) const;
  1409. virtual IStringVal& getQueryName(IStringVal &str) const;
  1410. virtual IStringVal & getQueryMainDefinition(IStringVal & str) const;
  1411. virtual IStringVal& getQueryDllName(IStringVal &str) const;
  1412. virtual unsigned getQueryDllCrc() const;
  1413. virtual IStringVal& getQueryCppName(IStringVal &str) const;
  1414. virtual IStringVal& getQueryResTxtName(IStringVal &str) const;
  1415. virtual IConstWUAssociatedFile * getAssociatedFile(WUFileType type, unsigned index) const;
  1416. virtual IConstWUAssociatedFileIterator& getAssociatedFiles() const;
  1417. virtual void setQueryType(WUQueryType qt);
  1418. virtual void setQueryText(const char *pstr);
  1419. virtual void setQueryName(const char *);
  1420. virtual void setQueryMainDefinition(const char * str);
  1421. virtual void addAssociatedFile(WUFileType type, const char * name, const char * ip, const char * desc, unsigned crc);
  1422. virtual void removeAssociatedFiles();
  1423. };
  1424. class CLocalWUWebServicesInfo : public CInterface, implements IWUWebServicesInfo
  1425. {
  1426. Owned<IPropertyTree> p;
  1427. mutable CriticalSection crit;
  1428. private:
  1429. public:
  1430. IMPLEMENT_IINTERFACE;
  1431. CLocalWUWebServicesInfo(IPropertyTree *p);
  1432. virtual IStringVal& getModuleName(IStringVal &str) const;
  1433. virtual IStringVal& getAttributeName(IStringVal &str) const;
  1434. virtual IStringVal& getDefaultName(IStringVal &str) const;
  1435. virtual IStringVal& getInfo(const char *name, IStringVal &str) const;
  1436. virtual unsigned getWebServicesCRC() const;
  1437. virtual void setModuleName(const char *);
  1438. virtual void setAttributeName(const char *);
  1439. virtual void setDefaultName(const char *);
  1440. virtual void setInfo(const char *name, const char *info);
  1441. virtual void setWebServicesCRC(unsigned);
  1442. };
  1443. class CLocalWURoxieQueryInfo : public CInterface, implements IWURoxieQueryInfo
  1444. {
  1445. Owned<IPropertyTree> p;
  1446. mutable CriticalSection crit;
  1447. private:
  1448. public:
  1449. IMPLEMENT_IINTERFACE;
  1450. CLocalWURoxieQueryInfo(IPropertyTree *p);
  1451. virtual IStringVal& getQueryInfo(IStringVal &str) const;
  1452. virtual IStringVal& getDefaultPackageInfo(IStringVal &str) const;
  1453. virtual IStringVal& getRoxieClusterName(IStringVal &str) const;
  1454. virtual IStringVal& getWuid(IStringVal &str) const;
  1455. virtual void setQueryInfo(const char *info);
  1456. virtual void setDefaultPackageInfo(const char *, int len);
  1457. virtual void setRoxieClusterName(const char *str);
  1458. virtual void setWuid(const char *str);
  1459. };
  1460. class CLocalWUResult : public CInterface, implements IWUResult
  1461. {
  1462. friend class CLocalWorkUnit;
  1463. mutable CriticalSection crit;
  1464. Owned<IPropertyTree> p;
  1465. Owned<IProperties> xmlns;
  1466. void getSchema(TypeInfoArray &types, StringAttrArray &names, IStringVal * ecl=NULL) const;
  1467. public:
  1468. IMPLEMENT_IINTERFACE;
  1469. CLocalWUResult(IPropertyTree *props);
  1470. ~CLocalWUResult() { try { p.clear(); } catch (IException *E) {E->Release();}}
  1471. virtual WUResultStatus getResultStatus() const;
  1472. virtual IStringVal& getResultName(IStringVal &str) const;
  1473. virtual int getResultSequence() const;
  1474. virtual bool isResultScalar() const;
  1475. virtual IStringVal& getResultXml(IStringVal &str) const;
  1476. virtual unsigned getResultFetchSize() const;
  1477. virtual __int64 getResultTotalRowCount() const;
  1478. virtual __int64 getResultRowCount() const;
  1479. virtual void getResultDataset(IStringVal & ecl, IStringVal & defs) const;
  1480. virtual IStringVal& getResultLogicalName(IStringVal &ecl) const;
  1481. virtual IStringVal& getResultKeyField(IStringVal& ecl) const;
  1482. virtual unsigned getResultRequestedRows() const;
  1483. virtual __int64 getResultInt() const;
  1484. virtual bool getResultBool() const;
  1485. virtual double getResultReal() const;
  1486. virtual IStringVal& getResultString(IStringVal & str) const;
  1487. virtual IDataVal& getResultRaw(IDataVal & data, IXmlToRawTransformer * xmlTransformer, ICsvToRawTransformer * csvTransformer) const;
  1488. virtual IDataVal& getResultUnicode(IDataVal & data) const;
  1489. virtual void getResultDecimal(void * val, unsigned length, unsigned precision, bool isSigned) const;
  1490. virtual IStringVal& getResultEclSchema(IStringVal & str) const;
  1491. virtual __int64 getResultRawSize(IXmlToRawTransformer * xmlTransformer, ICsvToRawTransformer * csvTransformer) const;
  1492. virtual IDataVal& getResultRaw(IDataVal & data, __int64 from, __int64 length, IXmlToRawTransformer * xmlTransformer, ICsvToRawTransformer * csvTransformer) const;
  1493. virtual IStringVal& getResultRecordSizeEntry(IStringVal & str) const;
  1494. virtual IStringVal& getResultTransformerEntry(IStringVal & str) const;
  1495. virtual __int64 getResultRowLimit() const;
  1496. virtual IStringVal& getResultFilename(IStringVal & str) const;
  1497. virtual WUResultFormat getResultFormat() const;
  1498. virtual unsigned getResultHash() const;
  1499. virtual bool getResultIsAll() const;
  1500. virtual IProperties *queryXmlns();
  1501. // interface IWUResult
  1502. virtual void setResultStatus(WUResultStatus status);
  1503. virtual void setResultName(const char *name);
  1504. virtual void setResultSequence(unsigned seq);
  1505. virtual void setResultSchemaRaw(unsigned len, const void *schema);
  1506. virtual void setResultScalar(bool isScalar);
  1507. virtual void setResultRaw(unsigned len, const void *xml, WUResultFormat format);
  1508. virtual void setResultFetchSize(unsigned rows); // 0 means file-loaded
  1509. virtual void setResultTotalRowCount(__int64 rows); // -1 means unknown
  1510. virtual void setResultRowCount(__int64 rows);
  1511. virtual void setResultDataset(const char *ecl, const char *defs);
  1512. virtual void setResultLogicalName(const char *logicalName);
  1513. virtual void setResultKeyField(const char * name);
  1514. virtual void setResultRequestedRows(unsigned req);
  1515. virtual void setResultRecordSizeEntry(const char * val);
  1516. virtual void setResultTransformerEntry(const char * val);
  1517. virtual void setResultInt(__int64 val);
  1518. virtual void setResultReal(double val);
  1519. virtual void setResultBool(bool val);
  1520. virtual void setResultString(const char * val, unsigned length);
  1521. virtual void setResultUnicode(const void * val, unsigned length);
  1522. virtual void setResultData(const void * val, unsigned length);
  1523. virtual void setResultDecimal(const void * val, unsigned length);
  1524. virtual void addResultRaw(unsigned len, const void * data, WUResultFormat format);
  1525. virtual void setResultRowLimit(__int64 value);
  1526. virtual void setResultFilename(const char * name);
  1527. virtual void setResultUInt(unsigned __int64 val);
  1528. virtual void setResultIsAll(bool value);
  1529. virtual void setResultFormat(WUResultFormat format);
  1530. virtual void setResultXML(const char *val);
  1531. virtual void setResultRow(unsigned len, const void * data);
  1532. virtual void setXmlns(const char *prefix, const char *uri);
  1533. };
  1534. class CLocalWUPlugin : public CInterface, implements IWUPlugin
  1535. {
  1536. Owned<IPropertyTree> p;
  1537. public:
  1538. IMPLEMENT_IINTERFACE;
  1539. CLocalWUPlugin(IPropertyTree *p);
  1540. virtual IStringVal& getPluginName(IStringVal &str) const;
  1541. virtual IStringVal& getPluginVersion(IStringVal &str) const;
  1542. virtual bool getPluginThor() const;
  1543. virtual bool getPluginHole() const;
  1544. virtual void setPluginName(const char *str);
  1545. virtual void setPluginVersion(const char *str);
  1546. virtual void setPluginThor(bool on);
  1547. virtual void setPluginHole(bool on);
  1548. };
  1549. class CLocalWULibrary : public CInterface, implements IWULibrary
  1550. {
  1551. Owned<IPropertyTree> p;
  1552. public:
  1553. IMPLEMENT_IINTERFACE;
  1554. CLocalWULibrary(IPropertyTree *p);
  1555. virtual IStringVal & getName(IStringVal & str) const;
  1556. virtual IConstWULibraryActivityIterator * getActivities() const;
  1557. virtual void setName(const char * str);
  1558. virtual void addActivity(unsigned id);
  1559. };
  1560. class CLocalWUGraph : public CInterface, implements IWUGraph
  1561. {
  1562. const CLocalWorkUnit &owner;
  1563. Owned<IPropertyTree> p;
  1564. mutable Owned<IPropertyTree> graph; // cached copy of graph xgmml
  1565. mutable Linked<IConstWUGraphProgress> progress;
  1566. StringAttr wuid;
  1567. unsigned wuidVersion;
  1568. void mergeProgress(IPropertyTree &tree, IPropertyTree &progressTree, const unsigned &progressV) const;
  1569. public:
  1570. IMPLEMENT_IINTERFACE;
  1571. CLocalWUGraph(const CLocalWorkUnit &owner, IPropertyTree *p);
  1572. virtual IStringVal & getXGMML(IStringVal & ret, bool mergeProgress) const;
  1573. virtual IStringVal & getDOT(IStringVal & ret) const;
  1574. virtual IStringVal & getName(IStringVal & ret) const;
  1575. virtual IStringVal & getLabel(IStringVal & ret) const;
  1576. virtual IStringVal & getTypeName(IStringVal & ret) const;
  1577. virtual WUGraphType getType() const;
  1578. virtual IPropertyTree * getXGMMLTree(bool mergeProgress) const;
  1579. virtual IPropertyTree * getXGMMLTreeRaw() const;
  1580. virtual bool isValid() const;
  1581. virtual void setName(const char *str);
  1582. virtual void setLabel(const char *str);
  1583. virtual void setType(WUGraphType type);
  1584. virtual void setXGMML(const char *str);
  1585. virtual void setXGMMLTree(IPropertyTree * tree, bool compress=true);
  1586. };
  1587. class CLocalWUActivity : public CInterface, implements IWUActivity
  1588. {
  1589. Owned<IPropertyTree> p;
  1590. public:
  1591. IMPLEMENT_IINTERFACE;
  1592. CLocalWUActivity(IPropertyTree *p, __int64 id = 0);
  1593. virtual __int64 getId() const;
  1594. virtual unsigned getKind() const;
  1595. virtual IStringVal & getHelper(IStringVal & ret) const;
  1596. virtual void setKind(unsigned id);
  1597. virtual void setHelper(const char * str);
  1598. };
  1599. class CLocalWUException : public CInterface, implements IWUException
  1600. {
  1601. Owned<IPropertyTree> p;
  1602. public:
  1603. IMPLEMENT_IINTERFACE;
  1604. CLocalWUException(IPropertyTree *p);
  1605. virtual IStringVal& getExceptionSource(IStringVal &str) const;
  1606. virtual IStringVal& getExceptionMessage(IStringVal &str) const;
  1607. virtual unsigned getExceptionCode() const;
  1608. virtual WUExceptionSeverity getSeverity() const;
  1609. virtual IStringVal & getTimeStamp(IStringVal & dt) const;
  1610. virtual IStringVal & getExceptionFileName(IStringVal & str) const;
  1611. virtual unsigned getExceptionLineNo() const;
  1612. virtual unsigned getExceptionColumn() const;
  1613. virtual void setExceptionSource(const char *str);
  1614. virtual void setExceptionMessage(const char *str);
  1615. virtual void setExceptionCode(unsigned code);
  1616. virtual void setSeverity(WUExceptionSeverity level);
  1617. virtual void setTimeStamp(const char * dt);
  1618. virtual void setExceptionFileName(const char *str);
  1619. virtual void setExceptionLineNo(unsigned r);
  1620. virtual void setExceptionColumn(unsigned c);
  1621. };
  1622. //==========================================================================================
  1623. extern WORKUNIT_API bool isSpecialResultSequence(unsigned sequence)
  1624. {
  1625. switch (sequence)
  1626. {
  1627. case ResultSequenceInternal:
  1628. case ResultSequenceOnce:
  1629. case ResultSequencePersist:
  1630. case ResultSequenceStored:
  1631. return true;
  1632. default:
  1633. assertex(sequence <= INT_MAX);
  1634. return false;
  1635. }
  1636. }
  1637. struct mapEnums { int val; const char *str; };
  1638. const char *getEnumText(int value, mapEnums *map)
  1639. {
  1640. const char *defval = map->str;
  1641. while (map->str)
  1642. {
  1643. if (value==map->val)
  1644. return map->str;
  1645. map++;
  1646. }
  1647. assertex(!"Unexpected value in setEnum");
  1648. return defval;
  1649. }
  1650. void setEnum(IPropertyTree *p, const char *propname, int value, mapEnums *map)
  1651. {
  1652. const char *defval = map->str;
  1653. while (map->str)
  1654. {
  1655. if (value==map->val)
  1656. {
  1657. p->setProp(propname, map->str);
  1658. return;
  1659. }
  1660. map++;
  1661. }
  1662. assertex(!"Unexpected value in setEnum");
  1663. p->setProp(propname, defval);
  1664. }
  1665. static int getEnum(const char *v, mapEnums *map)
  1666. {
  1667. if (v)
  1668. {
  1669. while (map->str)
  1670. {
  1671. if (stricmp(v, map->str)==0)
  1672. return map->val;
  1673. map++;
  1674. }
  1675. assertex(!"Unexpected value in getEnum");
  1676. }
  1677. return 0;
  1678. }
  1679. static int getEnum(const IPropertyTree *p, const char *propname, mapEnums *map)
  1680. {
  1681. return getEnum(p->queryProp(propname),map);
  1682. }
  1683. //==========================================================================================
  1684. class CConstWUArrayIterator : public CInterface, implements IConstWorkUnitIterator
  1685. {
  1686. IArrayOf<IPropertyTree> trees;
  1687. Owned<IConstWorkUnit> cur;
  1688. unsigned curTreeNum;
  1689. Linked<IRemoteConnection> conn;
  1690. Linked<ISecManager> secmgr;
  1691. Linked<ISecUser> secuser;
  1692. void setCurrent()
  1693. {
  1694. cur.setown(new CLocalWorkUnit(LINK(conn), LINK(&trees.item(curTreeNum)), secmgr, secuser));
  1695. }
  1696. public:
  1697. IMPLEMENT_IINTERFACE;
  1698. CConstWUArrayIterator(IRemoteConnection *_conn, IArrayOf<IPropertyTree> &_trees, ISecManager *_secmgr=NULL, ISecUser *_secuser=NULL)
  1699. : conn(_conn), secmgr(_secmgr), secuser(_secuser)
  1700. {
  1701. ForEachItemIn(t, _trees)
  1702. trees.append(*LINK(&_trees.item(t)));
  1703. curTreeNum = 0;
  1704. }
  1705. bool first()
  1706. {
  1707. curTreeNum = 0;
  1708. return next();
  1709. }
  1710. bool isValid()
  1711. {
  1712. return (NULL != cur.get());
  1713. }
  1714. bool next()
  1715. {
  1716. if (curTreeNum >= trees.ordinality())
  1717. {
  1718. cur.clear();
  1719. return false;
  1720. }
  1721. setCurrent();
  1722. ++curTreeNum;
  1723. return true;
  1724. }
  1725. IConstWorkUnit & query() { return *cur; }
  1726. };
  1727. //==========================================================================================
  1728. class CStringArrayIterator : public CInterface, implements IStringIterator
  1729. {
  1730. StringArray strings;
  1731. unsigned idx;
  1732. public:
  1733. IMPLEMENT_IINTERFACE;
  1734. CStringArrayIterator() { idx = 0; };
  1735. void append(const char *str) { strings.append(str); }
  1736. virtual bool first() { idx = 0; return strings.isItem(idx); }
  1737. virtual bool next() { idx ++; return strings.isItem(idx); }
  1738. virtual bool isValid() { return strings.isItem(idx); }
  1739. virtual IStringVal & str(IStringVal &s) { s.set(strings.item(idx)); return s; }
  1740. };
  1741. class CCachedJobNameIterator : public CInterface, implements IStringIterator
  1742. {
  1743. Owned<IPropertyTreeIterator> it;
  1744. public:
  1745. IMPLEMENT_IINTERFACE;
  1746. CCachedJobNameIterator(IPropertyTreeIterator *p) : it(p) {};
  1747. virtual bool first() { return it->first(); }
  1748. virtual bool next() { return it->next(); }
  1749. virtual bool isValid() { return it->isValid(); }
  1750. virtual IStringVal & str(IStringVal &s) { s.set(it->query().queryName()+1); return s; }
  1751. };
  1752. class CEmptyStringIterator : public CInterface, implements IStringIterator
  1753. {
  1754. public:
  1755. IMPLEMENT_IINTERFACE;
  1756. virtual bool first() { return false; }
  1757. virtual bool next() { return false; }
  1758. virtual bool isValid() { return false; }
  1759. virtual IStringVal & str(IStringVal &s) { s.clear(); return s; }
  1760. };
  1761. mapEnums workunitSortFields[] =
  1762. {
  1763. { WUSFuser, "@submitID" },
  1764. { WUSFcluster, "@clusterName" },
  1765. { WUSFjob, "@jobName" },
  1766. { WUSFstate, "@state" },
  1767. { WUSFpriority, "@priorityClass" },
  1768. { WUSFprotected, "@protected" },
  1769. { WUSFwuid, "@" },
  1770. { WUSFecl, "Query/Text" },
  1771. { WUSFfileread, "FilesRead/File/@name" },
  1772. { WUSFroxiecluster, "RoxieQueryInfo/@roxieClusterName" },
  1773. { WUSFbatchloginid, "Application/Dispatcher/FTPUserID" },
  1774. { WUSFbatchcustomername, "Application/Dispatcher/CustomerName" },
  1775. { WUSFbatchpriority, "Application/Dispatcher/JobPriority" },
  1776. { WUSFbatchinputreccount, "Application/Dispatcher/InputRecords" },
  1777. { WUSFbatchtimeuploaded, "Application/Dispatcher/TimeUploaded" },
  1778. { WUSFbatchtimecompleted, "Application/Dispatcher/TimeCompleted" },
  1779. { WUSFbatchmachine, "Application/Dispatcher/Machine" },
  1780. { WUSFbatchinputfile, "Application/Dispatcher/InputFileName" },
  1781. { WUSFbatchoutputfile, "Application/Dispatcher/OutputFileName" },
  1782. { WUSFtotalthortime, "Statistics/Statistic[@desc='Total thor time']/@value|Timings/Timing[@name='Total thor time']/@duration" },//Use Statistics first. If not found, use Timings
  1783. { WUSFterm, NULL }
  1784. };
  1785. mapEnums querySortFields[] =
  1786. {
  1787. { WUQSFId, "@id" },
  1788. { WUQSFwuid, "@wuid" },
  1789. { WUQSFname, "@name" },
  1790. { WUQSFdll, "@dll" },
  1791. { WUQSFmemoryLimit, "@memoryLimit" },
  1792. { WUQSFmemoryLimitHi, "@memoryLimit" },
  1793. { WUQSFtimeLimit, "@timeLimit" },
  1794. { WUQSFtimeLimitHi, "@timeLimit" },
  1795. { WUQSFwarnTimeLimit, "@warnTimeLimit" },
  1796. { WUQSFwarnTimeLimitHi, "@warnTimeLimit" },
  1797. { WUQSFpriority, "@priority" },
  1798. { WUQSFpriorityHi, "@priority" },
  1799. { WUQSFQuerySet, "../@id" },
  1800. { WUQSFActivited, "@activated" },
  1801. { WUQSFSuspendedByUser, "@suspended" },
  1802. { WUQSFterm, NULL }
  1803. };
  1804. class asyncRemoveDllWorkItem: public CInterface, implements IWorkQueueItem // class only used in asyncRemoveDll
  1805. {
  1806. StringAttr name;
  1807. public:
  1808. IMPLEMENT_IINTERFACE;
  1809. asyncRemoveDllWorkItem(const char * _name) : name(_name)
  1810. {
  1811. }
  1812. void execute()
  1813. {
  1814. PROGLOG("WU removeDll %s", name.get());
  1815. queryDllServer().removeDll(name, true, true); // <name>, removeDlls=true, removeDirectory=true
  1816. }
  1817. };
  1818. class asyncRemoveRemoteFileWorkItem: public CInterface, implements IWorkQueueItem // class only used in asyncRemoveFile
  1819. {
  1820. RemoteFilename name;
  1821. public:
  1822. IMPLEMENT_IINTERFACE;
  1823. asyncRemoveRemoteFileWorkItem(const char * _ip, const char * _name)
  1824. {
  1825. SocketEndpoint ep(_ip);
  1826. name.setPath(ep, _name);
  1827. }
  1828. void execute()
  1829. {
  1830. Owned<IFile> file = createIFile(name);
  1831. PROGLOG("WU removeDll %s",file->queryFilename());
  1832. file->remove();
  1833. }
  1834. };
  1835. //==========================================================================================
  1836. class CConstQuerySetQueryIterator : public CInterface, implements IConstQuerySetQueryIterator
  1837. {
  1838. IArrayOf<IPropertyTree> trees;
  1839. unsigned index;
  1840. public:
  1841. IMPLEMENT_IINTERFACE;
  1842. CConstQuerySetQueryIterator(IArrayOf<IPropertyTree> &_trees)
  1843. {
  1844. ForEachItemIn(t, _trees)
  1845. trees.append(*LINK(&_trees.item(t)));
  1846. index = 0;
  1847. }
  1848. ~CConstQuerySetQueryIterator()
  1849. {
  1850. trees.kill();
  1851. }
  1852. bool first()
  1853. {
  1854. index = 0;
  1855. return (trees.ordinality()!=0);
  1856. }
  1857. bool next()
  1858. {
  1859. index++;
  1860. return (index<trees.ordinality());
  1861. }
  1862. bool isValid()
  1863. {
  1864. return (index<trees.ordinality());
  1865. }
  1866. IPropertyTree &query()
  1867. {
  1868. return trees.item(index);
  1869. }
  1870. };
  1871. #define WUID_VERSION 2 // recorded in each wuid created, useful for bkwd compat. checks
  1872. class CWorkUnitFactory : public CInterface, implements IWorkUnitFactory, implements IDaliClientShutdown
  1873. {
  1874. Owned<IWorkQueueThread> deletedllworkq;
  1875. public:
  1876. IMPLEMENT_IINTERFACE;
  1877. CWorkUnitFactory()
  1878. {
  1879. // Assumes dali client configuration has already been done
  1880. sdsManager = &querySDS();
  1881. session = myProcessSession();
  1882. deletedllworkq.setown(createWorkQueueThread());
  1883. addShutdownHook(*this);
  1884. }
  1885. ~CWorkUnitFactory()
  1886. {
  1887. removeShutdownHook(*this);
  1888. // deletepool->joinAll();
  1889. }
  1890. void clientShutdown();
  1891. SessionId startSession()
  1892. {
  1893. // Temporary placeholder until startSession is implemented
  1894. #ifdef _WIN32
  1895. return GetTickCount();
  1896. #else
  1897. struct timeval tm;
  1898. gettimeofday(&tm,NULL);
  1899. return tm.tv_usec;
  1900. #endif
  1901. }
  1902. IWorkUnit* ensureNamedWorkUnit(const char *name)
  1903. {
  1904. if (workUnitTraceLevel > 1)
  1905. PrintLog("ensureNamedWorkUnit created %s", name);
  1906. StringBuffer wuRoot;
  1907. getXPath(wuRoot, name);
  1908. IRemoteConnection* conn = sdsManager->connect(wuRoot.str(), session, RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  1909. conn->queryRoot()->setProp("@xmlns:xsi", "http://www.w3.org/1999/XMLSchema-instance");
  1910. Owned<CLocalWorkUnit> cw = new CLocalWorkUnit(conn, (ISecManager *)NULL, NULL, (const char *)NULL);
  1911. return &cw->lockRemote(false);
  1912. }
  1913. virtual IWorkUnit* createNamedWorkUnit(const char *wuid,const char *parentWuid, const char *app, const char *user)
  1914. {
  1915. StringBuffer wuRoot;
  1916. getXPath(wuRoot, wuid);
  1917. IRemoteConnection *conn;
  1918. if (queryDaliServerVersion().compare("2.0") >= 0)
  1919. conn = sdsManager->connect(wuRoot.str(), session, RTM_LOCK_WRITE|RTM_CREATE_UNIQUE, SDS_LOCK_TIMEOUT);
  1920. else
  1921. conn = sdsManager->connect(wuRoot.str(), session, RTM_LOCK_WRITE|RTM_CREATE, SDS_LOCK_TIMEOUT);
  1922. conn->queryRoot()->setProp("@xmlns:xsi", "http://www.w3.org/1999/XMLSchema-instance");
  1923. conn->queryRoot()->setPropInt("@wuidVersion", WUID_VERSION);
  1924. Owned<CLocalWorkUnit> cw = new CLocalWorkUnit(conn, (ISecManager*)NULL, NULL, parentWuid);
  1925. IWorkUnit* ret = &cw->lockRemote(false);
  1926. ret->setDebugValue("CREATED_BY", app, true);
  1927. ret->setDebugValue("CREATED_FOR", user, true);
  1928. if (user)
  1929. cw->setWuScope(user);
  1930. return ret;
  1931. }
  1932. virtual IWorkUnit* createWorkUnit(const char *parentWuid, const char *app, const char *user)
  1933. {
  1934. StringBuffer wuid("W");
  1935. char result[32];
  1936. time_t ltime;
  1937. time( &ltime );
  1938. tm *today = localtime( &ltime ); // MORE - this is not threadsafe. But I probably don't care that much!
  1939. strftime(result, sizeof(result), "%Y%m%d-%H%M%S", today);
  1940. wuid.append(result);
  1941. if (queryDaliServerVersion().compare("2.0") < 0)
  1942. wuid.append('-').append(startSession());
  1943. if (workUnitTraceLevel > 1)
  1944. PrintLog("createWorkUnit created %s", wuid.str());
  1945. IWorkUnit* ret = createNamedWorkUnit(wuid.str(),parentWuid, app, user);
  1946. if (workUnitTraceLevel > 1)
  1947. {
  1948. SCMStringBuffer wuidName;
  1949. ret->getWuid(wuidName);
  1950. PrintLog("createWorkUnit created %s", wuidName.str());
  1951. }
  1952. ret->addTimeStamp("workunit", NULL, "Created");
  1953. return ret;
  1954. }
  1955. bool secDeleteWorkUnit(const char * wuid, ISecManager *secmgr, ISecUser *secuser, bool raiseexceptions)
  1956. {
  1957. if (workUnitTraceLevel > 1)
  1958. PrintLog("deleteWorkUnit %s", wuid);
  1959. StringBuffer wuRoot;
  1960. getXPath(wuRoot, wuid);
  1961. IRemoteConnection *conn = sdsManager->connect(wuRoot.str(), session, RTM_LOCK_WRITE|RTM_LOCK_SUB, SDS_LOCK_TIMEOUT);
  1962. if (!conn)
  1963. {
  1964. if (workUnitTraceLevel > 0)
  1965. PrintLog("deleteWorkUnit %s not found", wuid);
  1966. return false;
  1967. }
  1968. Owned<CLocalWorkUnit> cw = new CLocalWorkUnit(conn, secmgr, secuser); // takes ownership of conn
  1969. if (secmgr && !checkWuSecAccess(*cw.get(), *secmgr, secuser, SecAccess_Full, "delete", true, true)) {
  1970. if (raiseexceptions) {
  1971. // perhaps raise exception here?
  1972. }
  1973. return false;
  1974. }
  1975. if (raiseexceptions) {
  1976. try
  1977. {
  1978. cw->cleanupAndDelete(true,true);
  1979. }
  1980. catch (IException *E)
  1981. {
  1982. StringBuffer s;
  1983. LOG(MCexception(E, MSGCLS_warning), E, s.append("Exception during deleteWorkUnit: ").append(wuid).str());
  1984. E->Release();
  1985. return false;
  1986. }
  1987. }
  1988. else
  1989. cw->cleanupAndDelete(true,true);
  1990. removeWorkUnitFromAllQueues(wuid); //known active workunits wouldn't make it this far
  1991. return true;
  1992. }
  1993. virtual bool deleteWorkUnitEx(const char * wuid)
  1994. {
  1995. return secDeleteWorkUnit(wuid,NULL,NULL,true);
  1996. }
  1997. virtual bool deleteWorkUnit(const char * wuid)
  1998. {
  1999. return secDeleteWorkUnit(wuid,NULL,NULL,false);
  2000. }
  2001. virtual IConstWorkUnitIterator * getWorkUnitsByOwner(const char * owner)
  2002. {
  2003. // Not sure what to do about customerID vs user etc
  2004. StringBuffer path("*");
  2005. if (owner && *owner)
  2006. path.append("[@submitID=\"").append(owner).append("\"]");
  2007. return getWorkUnitsByXPath(path.str());
  2008. }
  2009. virtual IConstWorkUnitIterator * getWorkUnitsByState(WUState state);
  2010. virtual IConstWorkUnitIterator * getWorkUnitsByECL(const char* ecl)
  2011. {
  2012. StringBuffer path("*");
  2013. if (ecl && *ecl)
  2014. path.append("[Query/Text=~\"*").append(ecl).append("*\"]");
  2015. return getWorkUnitsByXPath(path.str());
  2016. }
  2017. virtual IConstWorkUnitIterator * getWorkUnitsByCluster(const char* cluster)
  2018. {
  2019. StringBuffer path("*");
  2020. if (cluster && *cluster)
  2021. path.append("[@clusterName=\"").append(cluster).append("\"]");
  2022. return getWorkUnitsByXPath(path.str());
  2023. }
  2024. virtual IConstWorkUnitIterator * getChildWorkUnits(const char *parent)
  2025. {
  2026. StringBuffer path("*[@parent=\"");
  2027. path.append(parent).append("\"]");
  2028. return getWorkUnitsByXPath(path.str());
  2029. }
  2030. virtual IConstWorkUnit* secOpenWorkUnit(const char *wuid, bool lock, ISecManager *secmgr=NULL, ISecUser *secuser=NULL)
  2031. {
  2032. StringBuffer wuidStr(wuid);
  2033. wuidStr.trim();
  2034. if (!wuidStr.length())
  2035. return NULL;
  2036. if (workUnitTraceLevel > 1)
  2037. PrintLog("openWorkUnit %s", wuidStr.str());
  2038. StringBuffer wuRoot;
  2039. getXPath(wuRoot, wuidStr.str());
  2040. IRemoteConnection* conn = sdsManager->connect(wuRoot.str(), session, lock ? RTM_LOCK_READ|RTM_LOCK_SUB : 0, SDS_LOCK_TIMEOUT);
  2041. if (conn)
  2042. {
  2043. CLocalWorkUnit *wu = new CLocalWorkUnit(conn, secmgr, secuser);
  2044. if (secmgr && wu)
  2045. {
  2046. if (!checkWuSecAccess(*wu, *secmgr, secuser, SecAccess_Read, "opening", true, true))
  2047. {
  2048. delete wu;
  2049. return NULL;
  2050. }
  2051. }
  2052. return wu;
  2053. }
  2054. else
  2055. {
  2056. if (workUnitTraceLevel > 0)
  2057. PrintLog("openWorkUnit %s not found", wuidStr.str());
  2058. return NULL;
  2059. }
  2060. }
  2061. virtual IConstWorkUnit* openWorkUnit(const char *wuid, bool lock)
  2062. {
  2063. return secOpenWorkUnit(wuid, lock);
  2064. }
  2065. virtual IWorkUnit* secUpdateWorkUnit(const char *wuid, ISecManager *secmgr=NULL, ISecUser *secuser=NULL)
  2066. {
  2067. if (workUnitTraceLevel > 1)
  2068. PrintLog("updateWorkUnit %s", wuid);
  2069. StringBuffer wuRoot;
  2070. getXPath(wuRoot, wuid);
  2071. IRemoteConnection* conn = sdsManager->connect(wuRoot.str(), session, RTM_LOCK_WRITE|RTM_LOCK_SUB, SDS_LOCK_TIMEOUT);
  2072. if (conn)
  2073. {
  2074. Owned<CLocalWorkUnit> cw = new CLocalWorkUnit(conn, secmgr, secuser);
  2075. if (secmgr && cw)
  2076. {
  2077. if (!checkWuSecAccess(*cw.get(), *secmgr, secuser, SecAccess_Write, "updating", true, true))
  2078. return NULL;
  2079. }
  2080. return &cw->lockRemote(false);
  2081. }
  2082. else
  2083. {
  2084. if (workUnitTraceLevel > 0)
  2085. PrintLog("updateWorkUnit %s not found", wuid);
  2086. return NULL;
  2087. }
  2088. }
  2089. virtual IWorkUnit* updateWorkUnit(const char *wuid)
  2090. {
  2091. return secUpdateWorkUnit(wuid);
  2092. }
  2093. virtual int setTracingLevel(int newLevel)
  2094. {
  2095. if (newLevel)
  2096. PrintLog("Setting workunit trace level to %d", newLevel);
  2097. int level = workUnitTraceLevel;
  2098. workUnitTraceLevel = newLevel;
  2099. return level;
  2100. }
  2101. IConstWorkUnitIterator * getWorkUnitsByXPath(const char *xpath)
  2102. {
  2103. return getWorkUnitsByXPath(xpath,NULL,NULL);
  2104. }
  2105. void descheduleAllWorkUnits()
  2106. {
  2107. Owned<IRemoteConnection> conn = querySDS().connect("/Schedule", myProcessSession(), RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  2108. if(!conn) return;
  2109. Owned<IPropertyTree> root(conn->queryRoot()->getBranch("."));
  2110. KeptAtomTable entries;
  2111. Owned<IPropertyTreeIterator> iter(root->getElements("*/*/*/*"));
  2112. StringBuffer wuid;
  2113. for(iter->first(); iter->isValid(); iter->next())
  2114. {
  2115. char const * entry = iter->query().queryName();
  2116. if(!entries.find(entry))
  2117. {
  2118. entries.addAtom(entry);
  2119. ncnameUnescape(entry, wuid.clear());
  2120. Owned<IWorkUnit> wu = updateWorkUnit(wuid);
  2121. if(wu && (wu->getState() == WUStateWait))
  2122. wu->setState(WUStateCompleted);
  2123. }
  2124. }
  2125. bool more;
  2126. do more = root->removeProp("*"); while(more);
  2127. }
  2128. IConstWorkUnitIterator * getWorkUnitsByXPath(const char *xpath, ISecManager *secmgr, ISecUser *secuser)
  2129. {
  2130. Owned<IRemoteConnection> conn = sdsManager->connect("/WorkUnits", session, 0, SDS_LOCK_TIMEOUT);
  2131. if (conn)
  2132. {
  2133. CDaliVersion serverVersionNeeded("3.2");
  2134. Owned<IPropertyTreeIterator> iter(queryDaliServerVersion().compare(serverVersionNeeded) < 0 ?
  2135. conn->queryRoot()->getElements(xpath) :
  2136. conn->getElements(xpath));
  2137. return new CConstWUIterator(conn, iter, secmgr, secuser);
  2138. }
  2139. else
  2140. return NULL;
  2141. }
  2142. IConstWorkUnitIterator* getWorkUnitsSorted( WUSortField *sortorder, // list of fields to sort by (terminated by WUSFterm)
  2143. WUSortField *filters, // NULL or list of fields to folteron (terminated by WUSFterm)
  2144. const void *filterbuf, // (appended) string values for filters
  2145. unsigned startoffset,
  2146. unsigned maxnum,
  2147. const char *queryowner,
  2148. __int64 *cachehint,
  2149. ISecManager *secmgr,
  2150. ISecUser *secuser,
  2151. unsigned *total)
  2152. {
  2153. class CWorkUnitsPager : public CSimpleInterface, implements IElementsPager
  2154. {
  2155. StringAttr xPath;
  2156. StringAttr sortOrder;
  2157. StringAttr nameFilterLo;
  2158. StringAttr nameFilterHi;
  2159. StringArray unknownAttributes;
  2160. public:
  2161. IMPLEMENT_IINTERFACE_USING(CSimpleInterface);
  2162. CWorkUnitsPager(const char* _xPath, const char *_sortOrder, const char* _nameFilterLo, const char* _nameFilterHi, StringArray& _unknownAttributes)
  2163. : xPath(_xPath), sortOrder(_sortOrder), nameFilterLo(_nameFilterLo), nameFilterHi(_nameFilterHi)
  2164. {
  2165. ForEachItemIn(x, _unknownAttributes)
  2166. unknownAttributes.append(_unknownAttributes.item(x));
  2167. }
  2168. virtual IRemoteConnection* getElements(IArrayOf<IPropertyTree> &elements)
  2169. {
  2170. Owned<IRemoteConnection> conn = querySDS().connect("WorkUnits", myProcessSession(), 0, SDS_LOCK_TIMEOUT);
  2171. if (!conn)
  2172. return NULL;
  2173. Owned<IPropertyTreeIterator> iter = conn->getElements(xPath);
  2174. if (!iter)
  2175. return NULL;
  2176. sortElements(iter, sortOrder.get(), nameFilterLo.get(), nameFilterHi.get(), unknownAttributes, elements);
  2177. return conn.getClear();
  2178. }
  2179. };
  2180. class CScopeChecker : public CSimpleInterface, implements ISortedElementsTreeFilter
  2181. {
  2182. UniqueScopes done;
  2183. ISecManager *secmgr;
  2184. ISecUser *secuser;
  2185. CriticalSection crit;
  2186. public:
  2187. IMPLEMENT_IINTERFACE_USING(CSimpleInterface);
  2188. CScopeChecker(ISecManager *_secmgr,ISecUser *_secuser)
  2189. {
  2190. secmgr = _secmgr;
  2191. secuser = _secuser;
  2192. }
  2193. bool isOK(IPropertyTree &tree)
  2194. {
  2195. const char *scopename = tree.queryProp("@scope");
  2196. if (!scopename||!*scopename)
  2197. return true;
  2198. {
  2199. CriticalBlock block(crit);
  2200. const bool *b = done.getValue(scopename);
  2201. if (b)
  2202. return *b;
  2203. }
  2204. bool ret = checkWuScopeSecAccess(scopename,*secmgr,secuser,SecAccess_Read,"iterating",false,false);
  2205. {
  2206. // conceivably could have already been checked and added, but ok.
  2207. CriticalBlock block(crit);
  2208. done.setValue(scopename,ret);
  2209. }
  2210. return ret;
  2211. }
  2212. };
  2213. Owned<ISortedElementsTreeFilter> sc = new CScopeChecker(secmgr,secuser);
  2214. StringBuffer query;
  2215. StringBuffer so;
  2216. StringAttr namefilter("*");
  2217. StringAttr namefilterlo;
  2218. StringAttr namefilterhi;
  2219. StringArray unknownAttributes;
  2220. if (filters) {
  2221. const char *fv = (const char *)filterbuf;
  2222. for (unsigned i=0;filters[i]!=WUSFterm;i++) {
  2223. int fmt = filters[i];
  2224. int subfmt = (fmt&0xff);
  2225. if (subfmt==WUSFwuid)
  2226. namefilterlo.set(fv);
  2227. else if (subfmt==WUSFwuidhigh)
  2228. namefilterhi.set(fv);
  2229. else if (subfmt==WUSFwildwuid)
  2230. namefilter.set(fv);
  2231. else if (subfmt==WUSFcustom)
  2232. query.append("[").append(fv).append("]");
  2233. else if (!fv || !*fv)
  2234. unknownAttributes.append(getEnumText(subfmt,workunitSortFields));
  2235. else {
  2236. query.append('[').append(getEnumText(subfmt,workunitSortFields)).append('=');
  2237. if (fmt&WUSFnocase)
  2238. query.append('?');
  2239. if (fmt&WUSFwild)
  2240. query.append('~');
  2241. query.append('"').append(fv).append("\"]");
  2242. }
  2243. fv = fv + strlen(fv)+1;
  2244. }
  2245. }
  2246. query.insert(0, namefilter.get());
  2247. if (sortorder) {
  2248. for (unsigned i=0;sortorder[i]!=WUSFterm;i++) {
  2249. if (so.length())
  2250. so.append(',');
  2251. int fmt = sortorder[i];
  2252. if (fmt&WUSFreverse)
  2253. so.append('-');
  2254. if (fmt&WUSFnocase)
  2255. so.append('?');
  2256. if (fmt&WUSFnumeric)
  2257. so.append('#');
  2258. so.append(getEnumText(fmt&0xff,workunitSortFields));
  2259. }
  2260. }
  2261. IArrayOf<IPropertyTree> results;
  2262. Owned<IElementsPager> elementsPager = new CWorkUnitsPager(query.str(), so.length()?so.str():NULL, namefilterlo.get(), namefilterhi.get(), unknownAttributes);
  2263. Owned<IRemoteConnection> conn=getElementsPaged(elementsPager,startoffset,maxnum,secmgr?sc:NULL,queryowner,cachehint,results,total);
  2264. return new CConstWUArrayIterator(conn, results, secmgr, secuser);
  2265. }
  2266. IConstWorkUnitIterator* getWorkUnitsSorted( WUSortField *sortorder, // list of fields to sort by (terminated by WUSFterm)
  2267. WUSortField *filters, // NULL or list of fields to filter on (terminated by WUSFterm)
  2268. const void *filterbuf, // (appended) string values for filters
  2269. unsigned startoffset,
  2270. unsigned maxnum,
  2271. const char *queryowner,
  2272. __int64 *cachehint,
  2273. unsigned *total)
  2274. {
  2275. return getWorkUnitsSorted(sortorder,filters,filterbuf,startoffset,maxnum,queryowner,cachehint, NULL, NULL, total);
  2276. }
  2277. IConstQuerySetQueryIterator* getQuerySetQueriesSorted( WUQuerySortField *sortorder, // list of fields to sort by (terminated by WUSFterm)
  2278. WUQuerySortField *filters, // NULL or list of fields to filter on (terminated by WUSFterm)
  2279. const void *filterbuf, // (appended) string values for filters
  2280. unsigned startoffset,
  2281. unsigned maxnum,
  2282. __int64 *cachehint,
  2283. unsigned *total,
  2284. const MapStringTo<bool> *_subset)
  2285. {
  2286. struct PostFilters
  2287. {
  2288. WUQueryFilterBoolean activatedFilter;
  2289. WUQueryFilterBoolean suspendedByUserFilter;
  2290. PostFilters()
  2291. {
  2292. activatedFilter = WUQFSAll;
  2293. suspendedByUserFilter = WUQFSAll;
  2294. };
  2295. } postFilters;
  2296. class CQuerySetQueriesPager : public CSimpleInterface, implements IElementsPager
  2297. {
  2298. StringAttr querySet;
  2299. StringAttr xPath;
  2300. StringAttr sortOrder;
  2301. PostFilters postFilters;
  2302. StringArray unknownAttributes;
  2303. const MapStringTo<bool> *subset;
  2304. void populateQueryTree(IPropertyTree* queryRegistry, const char* querySetId, IPropertyTree* querySetTree, const char *xPath, IPropertyTree* queryTree)
  2305. {
  2306. Owned<IPropertyTreeIterator> iter = querySetTree->getElements(xPath);
  2307. ForEach(*iter)
  2308. {
  2309. IPropertyTree &query = iter->query();
  2310. bool activated = false;
  2311. const char* queryId = query.queryProp("@id");
  2312. if (queryId && *queryId)
  2313. {
  2314. if (subset)
  2315. {
  2316. VStringBuffer match("%s/%s", querySetId, queryId);
  2317. if (!subset->getValue(match))
  2318. continue;
  2319. }
  2320. VStringBuffer xPath("Alias[@id='%s']", queryId);
  2321. IPropertyTree *alias = queryRegistry->queryPropTree(xPath.str());
  2322. if (alias)
  2323. activated = true;
  2324. }
  2325. if (activated && (postFilters.activatedFilter == WUQFSNo))
  2326. continue;
  2327. if (!activated && (postFilters.activatedFilter == WUQFSYes))
  2328. continue;
  2329. if ((postFilters.suspendedByUserFilter == WUQFSNo) && query.hasProp(getEnumText(WUQSFSuspendedByUser,querySortFields)))
  2330. continue;
  2331. if ((postFilters.suspendedByUserFilter == WUQFSYes) && !query.hasProp(getEnumText(WUQSFSuspendedByUser,querySortFields)))
  2332. continue;
  2333. IPropertyTree *queryWithSetId = queryTree->addPropTree("Query", LINK(&query));
  2334. queryWithSetId->addProp("@querySetId", querySetId);
  2335. queryWithSetId->addPropBool("@activated", activated);
  2336. }
  2337. }
  2338. IPropertyTree* getAllQuerySetQueries(IRemoteConnection* conn, const char *querySet, const char *xPath)
  2339. {
  2340. Owned<IPropertyTree> queryTree = createPTree("Queries");
  2341. IPropertyTree* root = conn->queryRoot();
  2342. if (querySet && *querySet)
  2343. {
  2344. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySet, false);
  2345. VStringBuffer path("QuerySet[@id='%s']/Query%s", querySet, xPath);
  2346. populateQueryTree(queryRegistry, querySet, root, path.str(), queryTree);
  2347. }
  2348. else
  2349. {
  2350. Owned<IPropertyTreeIterator> iter = root->getElements("QuerySet");
  2351. ForEach(*iter)
  2352. {
  2353. IPropertyTree &querySetTree = iter->query();
  2354. const char* id = querySetTree.queryProp("@id");
  2355. if (id && *id)
  2356. {
  2357. Owned<IPropertyTree> queryRegistry = getQueryRegistry(id, false);
  2358. VStringBuffer path("Query%s", xPath);
  2359. populateQueryTree(queryRegistry, id, &querySetTree, path.str(), queryTree);
  2360. }
  2361. }
  2362. }
  2363. return queryTree.getClear();
  2364. }
  2365. public:
  2366. IMPLEMENT_IINTERFACE_USING(CSimpleInterface);
  2367. CQuerySetQueriesPager(const char* _querySet, const char* _xPath, const char *_sortOrder, PostFilters& _postFilters, StringArray& _unknownAttributes, const MapStringTo<bool> *_subset)
  2368. : querySet(_querySet), xPath(_xPath), sortOrder(_sortOrder), subset(_subset)
  2369. {
  2370. postFilters.activatedFilter = _postFilters.activatedFilter;
  2371. postFilters.suspendedByUserFilter = _postFilters.suspendedByUserFilter;
  2372. ForEachItemIn(x, _unknownAttributes)
  2373. unknownAttributes.append(_unknownAttributes.item(x));
  2374. }
  2375. virtual IRemoteConnection* getElements(IArrayOf<IPropertyTree> &elements)
  2376. {
  2377. Owned<IRemoteConnection> conn = querySDS().connect("QuerySets", myProcessSession(), 0, SDS_LOCK_TIMEOUT);
  2378. if (!conn)
  2379. return NULL;
  2380. Owned<IPropertyTree> elementTree = getAllQuerySetQueries(conn, querySet.get(), xPath.get());
  2381. if (!elementTree)
  2382. return NULL;
  2383. Owned<IPropertyTreeIterator> iter = elementTree->getElements("*");
  2384. if (!iter)
  2385. return NULL;
  2386. sortElements(iter, sortOrder.get(), NULL, NULL, unknownAttributes, elements);
  2387. return conn.getClear();
  2388. }
  2389. };
  2390. StringAttr querySet;
  2391. StringBuffer xPath;
  2392. StringBuffer so;
  2393. StringArray unknownAttributes;
  2394. if (filters)
  2395. {
  2396. const char *fv = (const char *)filterbuf;
  2397. for (unsigned i=0;filters[i]!=WUQSFterm;i++) {
  2398. int fmt = filters[i];
  2399. int subfmt = (fmt&0xff);
  2400. if (subfmt==WUQSFQuerySet)
  2401. querySet.set(fv);
  2402. else if ((subfmt==WUQSFmemoryLimit) || (subfmt==WUQSFtimeLimit) || (subfmt==WUQSFwarnTimeLimit) || (subfmt==WUQSFpriority))
  2403. xPath.append('[').append(getEnumText(subfmt,querySortFields)).append(">=").append(fv).append("]");
  2404. else if ((subfmt==WUQSFmemoryLimitHi) || (subfmt==WUQSFtimeLimitHi) || (subfmt==WUQSFwarnTimeLimitHi) || (subfmt==WUQSFpriorityHi))
  2405. xPath.append('[').append(getEnumText(subfmt,querySortFields)).append("<=").append(fv).append("]");
  2406. else if (subfmt==WUQSFActivited)
  2407. postFilters.activatedFilter = (WUQueryFilterBoolean) atoi(fv);
  2408. else if (subfmt==WUQSFSuspendedByUser)
  2409. postFilters.suspendedByUserFilter = (WUQueryFilterBoolean) atoi(fv);
  2410. else if (!fv || !*fv)
  2411. unknownAttributes.append(getEnumText(subfmt,querySortFields));
  2412. else {
  2413. xPath.append('[').append(getEnumText(subfmt,querySortFields)).append('=');
  2414. if (fmt&WUQSFnocase)
  2415. xPath.append('?');
  2416. if (fmt&WUQSFnumeric)
  2417. xPath.append('#');
  2418. if (fmt&WUQSFwild)
  2419. xPath.append('~');
  2420. xPath.append('"').append(fv).append("\"]");
  2421. }
  2422. fv = fv + strlen(fv)+1;
  2423. }
  2424. }
  2425. if (sortorder) {
  2426. for (unsigned i=0;sortorder[i]!=WUQSFterm;i++) {
  2427. if (so.length())
  2428. so.append(',');
  2429. int fmt = sortorder[i];
  2430. if (fmt&WUQSFreverse)
  2431. so.append('-');
  2432. if (fmt&WUQSFnocase)
  2433. so.append('?');
  2434. if (fmt&WUQSFnumeric)
  2435. so.append('#');
  2436. so.append(getEnumText(fmt&0xff,querySortFields));
  2437. }
  2438. }
  2439. IArrayOf<IPropertyTree> results;
  2440. Owned<IElementsPager> elementsPager = new CQuerySetQueriesPager(querySet.get(), xPath.str(), so.length()?so.str():NULL, postFilters, unknownAttributes, _subset);
  2441. Owned<IRemoteConnection> conn=getElementsPaged(elementsPager,startoffset,maxnum,NULL,"",cachehint,results,total);
  2442. return new CConstQuerySetQueryIterator(results);
  2443. }
  2444. virtual unsigned numWorkUnits()
  2445. {
  2446. Owned<IRemoteConnection> conn = sdsManager->connect("/WorkUnits", session, 0, SDS_LOCK_TIMEOUT);
  2447. if (!conn)
  2448. return 0;
  2449. IPropertyTree *root = conn->queryRoot();
  2450. return root->numChildren();
  2451. }
  2452. virtual unsigned numWorkUnitsFiltered(WUSortField *filters,
  2453. const void *filterbuf,
  2454. ISecManager *secmgr,
  2455. ISecUser *secuser)
  2456. {
  2457. unsigned total;
  2458. Owned<IConstWorkUnitIterator> iter = getWorkUnitsSorted( NULL,filters,filterbuf,0,0x7fffffff,NULL,NULL,secmgr,secuser,&total);
  2459. return total;
  2460. }
  2461. virtual unsigned numWorkUnitsFiltered(WUSortField *filters,const void *filterbuf)
  2462. {
  2463. if (!filters)
  2464. return numWorkUnits();
  2465. return numWorkUnitsFiltered(filters,filterbuf,NULL,NULL);
  2466. }
  2467. void asyncRemoveDll(const char * name)
  2468. {
  2469. deletedllworkq->post(new asyncRemoveDllWorkItem(name));
  2470. }
  2471. void asyncRemoveFile(const char * ip, const char * name)
  2472. {
  2473. deletedllworkq->post(new asyncRemoveRemoteFileWorkItem(ip, name));
  2474. }
  2475. ISDSManager *sdsManager;
  2476. SessionId session;
  2477. ISecManager *secMgr;
  2478. private:
  2479. void deleteChildren(IPropertyTree *root, const char *wuid)
  2480. {
  2481. StringBuffer kids("*[@parent=\"");
  2482. kids.append(wuid).append("\"]");
  2483. Owned<IPropertyTreeIterator> it = root->getElements(kids.str());
  2484. ForEach (*it)
  2485. {
  2486. deleteChildren(root, it->query().queryName());
  2487. }
  2488. root->removeProp(wuid);
  2489. }
  2490. class CConstWUIterator : public CInterface, implements IConstWorkUnitIterator
  2491. {
  2492. Owned<IConstWorkUnit> cur;
  2493. Linked<IRemoteConnection> conn;
  2494. Linked<IPropertyTreeIterator> ptreeIter;
  2495. Linked<ISecManager> secmgr;
  2496. Linked<ISecUser> secuser;
  2497. Owned<ISecResourceList> scopes;
  2498. void setCurrent()
  2499. {
  2500. cur.setown(new CLocalWorkUnit(LINK(conn), LINK(&ptreeIter->query()), secmgr, secuser));
  2501. }
  2502. bool getNext() // scan for a workunit with permissions
  2503. {
  2504. if (!scopes)
  2505. {
  2506. setCurrent();
  2507. return true;
  2508. }
  2509. do
  2510. {
  2511. const char *scopeName = ptreeIter->query().queryProp("@scope");
  2512. if (!scopeName || !*scopeName || checkWuScopeListSecAccess(scopeName, scopes, SecAccess_Read, "iterating", false, false))
  2513. {
  2514. setCurrent();
  2515. return true;
  2516. }
  2517. }
  2518. while (ptreeIter->next());
  2519. cur.clear();
  2520. return false;
  2521. }
  2522. public:
  2523. IMPLEMENT_IINTERFACE;
  2524. CConstWUIterator(IRemoteConnection *_conn, IPropertyTreeIterator *_ptreeIter, ISecManager *_secmgr=NULL, ISecUser *_secuser=NULL)
  2525. : conn(_conn), ptreeIter(_ptreeIter), secmgr(_secmgr), secuser(_secuser)
  2526. {
  2527. UniqueScopes us;
  2528. if (secmgr /* && secmgr->authTypeRequired(RT_WORKUNIT_SCOPE) tbd */)
  2529. {
  2530. scopes.setown(secmgr->createResourceList("wuscopes"));
  2531. ForEach(*ptreeIter)
  2532. {
  2533. const char *scopeName = ptreeIter->query().queryProp("@scope");
  2534. if (scopeName && *scopeName && !us.getValue(scopeName))
  2535. {
  2536. scopes->addResource(scopeName);
  2537. us.setValue(scopeName, true);
  2538. }
  2539. }
  2540. if (scopes->count())
  2541. secmgr->authorizeEx(RT_WORKUNIT_SCOPE, *secuser, scopes);
  2542. else
  2543. scopes.clear();
  2544. }
  2545. }
  2546. bool first()
  2547. {
  2548. if (!ptreeIter->first())
  2549. {
  2550. cur.clear();
  2551. return false;
  2552. }
  2553. return getNext();
  2554. }
  2555. bool isValid()
  2556. {
  2557. return (NULL != cur.get());
  2558. }
  2559. bool next()
  2560. {
  2561. if (!ptreeIter->next())
  2562. {
  2563. cur.clear();
  2564. return false;
  2565. }
  2566. return getNext();
  2567. }
  2568. IConstWorkUnit & query() { return *cur; }
  2569. };
  2570. IRemoteConnection* connect(const char *xpath, unsigned flags)
  2571. {
  2572. return sdsManager->connect(xpath, session, flags, SDS_LOCK_TIMEOUT);
  2573. }
  2574. };
  2575. static Owned<CWorkUnitFactory> factory;
  2576. void CWorkUnitFactory::clientShutdown()
  2577. {
  2578. factory.clear();
  2579. }
  2580. void clientShutdownWorkUnit()
  2581. {
  2582. factory.clear();
  2583. }
  2584. extern WORKUNIT_API IWorkUnitFactory * getWorkUnitFactory()
  2585. {
  2586. if (!factory)
  2587. factory.setown(new CWorkUnitFactory());
  2588. return factory.getLink();
  2589. }
  2590. class CSecureWorkUnitFactory : public CInterface, implements IWorkUnitFactory
  2591. {
  2592. public:
  2593. IMPLEMENT_IINTERFACE;
  2594. CSecureWorkUnitFactory(ISecManager &secmgr, ISecUser &secuser)
  2595. {
  2596. if (!factory)
  2597. factory.setown(new CWorkUnitFactory());
  2598. secMgr.set(&secmgr);
  2599. secUser.set(&secuser);
  2600. }
  2601. virtual IWorkUnit* createNamedWorkUnit(const char *wuid,const char *parentWuid, const char *app, const char *user)
  2602. {
  2603. checkWuScopeSecAccess(user, *secMgr.get(), secUser.get(), SecAccess_Write, "Create", true, true);
  2604. IWorkUnit *wu=factory->createNamedWorkUnit(wuid, parentWuid, app, user);
  2605. if (wu)
  2606. {
  2607. CLockedWorkUnit* lw = dynamic_cast<CLockedWorkUnit*>(wu);
  2608. if (lw)
  2609. lw->setSecIfcs(secMgr.get(), secUser.get());
  2610. }
  2611. return wu;
  2612. }
  2613. virtual IWorkUnit* createWorkUnit(const char *parentWuid, const char *app, const char *user)
  2614. {
  2615. checkWuScopeSecAccess(user, *secMgr.get(), secUser.get(), SecAccess_Write, "Create", true, true);
  2616. IWorkUnit *wu=factory->createWorkUnit(parentWuid, app, user);
  2617. if (wu)
  2618. {
  2619. CLockedWorkUnit* lw = dynamic_cast<CLockedWorkUnit*>(wu);
  2620. if (lw)
  2621. lw->setSecIfcs(secMgr.get(), secUser.get());
  2622. }
  2623. return wu;
  2624. }
  2625. virtual bool deleteWorkUnitEx(const char * wuid)
  2626. {
  2627. return factory->secDeleteWorkUnit(wuid, secMgr.get(), secUser.get(), true);
  2628. }
  2629. virtual bool deleteWorkUnit(const char * wuid)
  2630. {
  2631. return factory->secDeleteWorkUnit(wuid, secMgr.get(), secUser.get(), false);
  2632. }
  2633. virtual IConstWorkUnit* openWorkUnit(const char *wuid, bool lock)
  2634. {
  2635. return factory->secOpenWorkUnit(wuid, lock, secMgr.get(), secUser.get());
  2636. }
  2637. virtual IWorkUnit* updateWorkUnit(const char *wuid)
  2638. {
  2639. return factory->secUpdateWorkUnit(wuid, secMgr.get(), secUser.get());
  2640. }
  2641. //make cached workunits a non secure pass through for now.
  2642. virtual IConstWorkUnitIterator * getWorkUnitsByOwner(const char * owner)
  2643. {
  2644. // Not sure what to do about customerID vs user etc
  2645. StringBuffer path("*");
  2646. if (owner && *owner)
  2647. path.append("[@submitID=\"").append(owner).append("\"]");
  2648. return factory->getWorkUnitsByXPath(path.str(), secMgr.get(), secUser.get());
  2649. }
  2650. virtual IConstWorkUnitIterator * getWorkUnitsByState(WUState state);
  2651. virtual IConstWorkUnitIterator * getWorkUnitsByECL(const char* ecl)
  2652. {
  2653. return factory->getWorkUnitsByECL(ecl);
  2654. }
  2655. virtual IConstWorkUnitIterator * getWorkUnitsByCluster(const char* cluster)
  2656. {
  2657. return factory->getWorkUnitsByCluster(cluster);
  2658. }
  2659. virtual IConstWorkUnitIterator * getWorkUnitsByXPath(const char * xpath)
  2660. {
  2661. return factory->getWorkUnitsByXPath(xpath, secMgr.get(), secUser.get());
  2662. }
  2663. virtual void descheduleAllWorkUnits()
  2664. {
  2665. factory->descheduleAllWorkUnits();
  2666. }
  2667. virtual IConstWorkUnitIterator * getChildWorkUnits(const char *parent)
  2668. {
  2669. StringBuffer path("*[@parent=\"");
  2670. path.append(parent).append("\"]");
  2671. return factory->getWorkUnitsByXPath(path.str(), secMgr.get(), secUser.get());
  2672. }
  2673. virtual int setTracingLevel(int newLevel)
  2674. {
  2675. return factory->setTracingLevel(newLevel);
  2676. }
  2677. virtual IConstWorkUnitIterator* getWorkUnitsSorted( WUSortField *sortorder, // list of fields to sort by (terminated by WUSFterm)
  2678. WUSortField *filters, // NULL or list of fields to filter on (terminated by WUSFterm)
  2679. const void *filterbuf, // (appended) string values for filters
  2680. unsigned startoffset,
  2681. unsigned maxnum,
  2682. const char *queryowner,
  2683. __int64 *cachehint,
  2684. unsigned *total)
  2685. {
  2686. return factory->getWorkUnitsSorted(sortorder,filters,filterbuf,startoffset,maxnum,queryowner,cachehint, secMgr.get(), secUser.get(), total);
  2687. }
  2688. virtual IConstQuerySetQueryIterator* getQuerySetQueriesSorted( WUQuerySortField *sortorder,
  2689. WUQuerySortField *filters,
  2690. const void *filterbuf,
  2691. unsigned startoffset,
  2692. unsigned maxnum,
  2693. __int64 *cachehint,
  2694. unsigned *total,
  2695. const MapStringTo<bool> *subset)
  2696. {
  2697. return factory->getQuerySetQueriesSorted(sortorder,filters,filterbuf,startoffset,maxnum,cachehint,total, subset);
  2698. }
  2699. virtual unsigned numWorkUnits()
  2700. {
  2701. return factory->numWorkUnits();
  2702. }
  2703. virtual unsigned numWorkUnitsFiltered(WUSortField *filters,
  2704. const void *filterbuf)
  2705. {
  2706. return factory->numWorkUnitsFiltered(filters,filterbuf,secMgr.get(), secUser.get());
  2707. }
  2708. private:
  2709. Owned<CWorkUnitFactory> base_factory;
  2710. Owned<ISecManager> secMgr;
  2711. Owned<ISecUser> secUser;
  2712. };
  2713. extern WORKUNIT_API IWorkUnitFactory * getSecWorkUnitFactory(ISecManager &secmgr, ISecUser &secuser)
  2714. {
  2715. return new CSecureWorkUnitFactory(secmgr, secuser);
  2716. }
  2717. extern WORKUNIT_API IWorkUnitFactory * getWorkUnitFactory(ISecManager *secmgr, ISecUser *secuser)
  2718. {
  2719. if (secmgr && secuser)
  2720. return getSecWorkUnitFactory(*secmgr, *secuser);
  2721. else
  2722. return getWorkUnitFactory();
  2723. }
  2724. //==========================================================================================
  2725. class CStringPTreeIterator : public CInterface, implements IStringIterator
  2726. {
  2727. Owned<IPropertyTreeIterator> it;
  2728. public:
  2729. IMPLEMENT_IINTERFACE;
  2730. CStringPTreeIterator(IPropertyTreeIterator *p) : it(p) {};
  2731. virtual bool first() { return it->first(); }
  2732. virtual bool next() { return it->next(); }
  2733. virtual bool isValid() { return it->isValid(); }
  2734. virtual IStringVal & str(IStringVal &s) { s.set(it->query().queryProp(NULL)); return s; }
  2735. };
  2736. class CStringPTreeTagIterator : public CInterface, implements IStringIterator
  2737. {
  2738. Owned<IPropertyTreeIterator> it;
  2739. public:
  2740. IMPLEMENT_IINTERFACE;
  2741. CStringPTreeTagIterator(IPropertyTreeIterator *p) : it(p) {};
  2742. virtual bool first() { return it->first(); }
  2743. virtual bool next() { return it->next(); }
  2744. virtual bool isValid() { return it->isValid(); }
  2745. virtual IStringVal & str(IStringVal &s) { s.set(it->query().queryName()); return s; }
  2746. };
  2747. class CStringPTreeAttrIterator : public CInterface, implements IStringIterator
  2748. {
  2749. Owned<IPropertyTreeIterator> it;
  2750. StringAttr name;
  2751. public:
  2752. IMPLEMENT_IINTERFACE;
  2753. CStringPTreeAttrIterator(IPropertyTreeIterator *p, const char *_name) : it(p), name(_name) {};
  2754. virtual bool first() { return it->first(); }
  2755. virtual bool next() { return it->next(); }
  2756. virtual bool isValid() { return it->isValid(); }
  2757. virtual IStringVal & str(IStringVal &s) { s.set(it->query().queryProp(name)); return s; }
  2758. };
  2759. //==========================================================================================
  2760. CLocalWorkUnit::CLocalWorkUnit(IRemoteConnection *_conn, ISecManager *secmgr, ISecUser *secuser, const char *parentWuid) : connection(_conn)
  2761. {
  2762. connectAtRoot = true;
  2763. init();
  2764. p.setown(connection->getRoot());
  2765. if (parentWuid)
  2766. p->setProp("@parent", parentWuid);
  2767. secMgr.set(secmgr);
  2768. secUser.set(secuser);
  2769. }
  2770. CLocalWorkUnit::CLocalWorkUnit(IRemoteConnection *_conn, IPropertyTree* root, ISecManager *secmgr, ISecUser *secuser) : connection(_conn)
  2771. {
  2772. connectAtRoot = false;
  2773. init();
  2774. p.setown(root);
  2775. secMgr.set(secmgr);
  2776. secUser.set(secuser);
  2777. }
  2778. void CLocalWorkUnit::init()
  2779. {
  2780. p.clear();
  2781. cachedGraphs.clear();
  2782. workflowIterator.clear();
  2783. query.clear();
  2784. graphs.kill();
  2785. results.kill();
  2786. variables.kill();
  2787. plugins.kill();
  2788. libraries.kill();
  2789. activities.kill();
  2790. exceptions.kill();
  2791. temporaries.kill();
  2792. timers.kill();
  2793. roxieQueryInfo.clear();
  2794. webServicesInfo.clear();
  2795. workflowIteratorCached = false;
  2796. resultsCached = false;
  2797. temporariesCached = false;
  2798. variablesCached = false;
  2799. exceptionsCached = false;
  2800. pluginsCached = false;
  2801. librariesCached = false;
  2802. activitiesCached = false;
  2803. webServicesInfoCached = false;
  2804. roxieQueryInfoCached = false;
  2805. timersCached = false;
  2806. dirty = false;
  2807. abortDirty = true;
  2808. abortState = false;
  2809. }
  2810. // Dummy workunit support
  2811. CLocalWorkUnit::CLocalWorkUnit(const char *_wuid, const char *parentWuid, ISecManager *secmgr, ISecUser *secuser)
  2812. {
  2813. connectAtRoot = true;
  2814. init();
  2815. p.setown(createPTree(_wuid));
  2816. p->setProp("@xmlns:xsi", "http://www.w3.org/1999/XMLSchema-instance");
  2817. if (parentWuid)
  2818. p->setProp("@parentWuid", parentWuid);
  2819. secMgr.set(secmgr);
  2820. secUser.set(secuser);
  2821. }
  2822. CLocalWorkUnit::~CLocalWorkUnit()
  2823. {
  2824. if (workUnitTraceLevel > 1)
  2825. {
  2826. PrintLog("Releasing workunit %s mode %x", p->queryName(), connection ? connection->queryMode() :0);
  2827. }
  2828. try
  2829. {
  2830. unsubscribe();
  2831. query.clear();
  2832. webServicesInfo.clear();
  2833. roxieQueryInfo.clear();
  2834. workflowIterator.clear();
  2835. activities.kill();
  2836. plugins.kill();
  2837. libraries.kill();
  2838. exceptions.kill();
  2839. graphs.kill();
  2840. results.kill();
  2841. temporaries.kill();
  2842. variables.kill();
  2843. timestamps.kill();
  2844. appvalues.kill();
  2845. statistics.kill();
  2846. userDesc.clear();
  2847. secMgr.clear();
  2848. secUser.clear();
  2849. cachedGraphs.clear();
  2850. p.clear();
  2851. connection.clear();
  2852. }
  2853. catch (IException *E) { LOG(MCexception(E, MSGCLS_warning), E, "Exception during ~CLocalWorkUnit"); E->Release(); }
  2854. }
  2855. void CLocalWorkUnit::cleanupAndDelete(bool deldll, bool deleteOwned, const StringArray *deleteExclusions)
  2856. {
  2857. TIME_SECTION("WUDELETE cleanupAndDelete total");
  2858. // Delete any related things in SDS etc that might otherwise be forgotten
  2859. assertex(connectAtRoot); // make sure we don't delete entire workunit tree!
  2860. if (p->getPropBool("@protected", false))
  2861. throw MakeStringException(WUERR_WorkunitProtected, "%s: Workunit is protected",p->queryName());
  2862. switch (getState())
  2863. {
  2864. case WUStateAborted:
  2865. case WUStateCompleted:
  2866. case WUStateFailed:
  2867. case WUStateArchived:
  2868. break;
  2869. case WUStateCompiled:
  2870. if (getAction()==WUActionRun || getAction()==WUActionUnknown)
  2871. throw MakeStringException(WUERR_WorkunitActive, "%s: Workunit is active",p->queryName());
  2872. break;
  2873. case WUStateWait:
  2874. throw MakeStringException(WUERR_WorkunitScheduled, "%s: Workunit is scheduled",p->queryName());
  2875. default:
  2876. throw MakeStringException(WUERR_WorkunitActive, "%s: Workunit is active",p->queryName());
  2877. break;
  2878. }
  2879. if (getIsQueryService())
  2880. {
  2881. Owned<IPropertyTree> registry = getQueryRegistryRoot();
  2882. if (registry)
  2883. {
  2884. VStringBuffer xpath("QuerySet/Query[@wuid='%s']", p->queryName());
  2885. if (registry->hasProp(xpath.str()))
  2886. throw MakeStringException(WUERR_WorkunitPublished, "%s: Workunit is published",p->queryName());
  2887. }
  2888. }
  2889. try
  2890. {
  2891. if (deldll && !p->getPropBool("@isClone", false))
  2892. {
  2893. Owned<IConstWUQuery> q = getQuery();
  2894. if (q)
  2895. {
  2896. Owned<IConstWUAssociatedFileIterator> iter = &q->getAssociatedFiles();
  2897. SCMStringBuffer name;
  2898. ForEach(*iter)
  2899. {
  2900. IConstWUAssociatedFile & cur = iter->query();
  2901. cur.getNameTail(name);
  2902. if (!deleteExclusions || (NotFound == deleteExclusions->find(name.str())))
  2903. {
  2904. Owned<IDllEntry> entry = queryDllServer().getEntry(name.str());
  2905. if (entry.get())
  2906. factory->asyncRemoveDll(name.str());
  2907. else
  2908. {
  2909. SCMStringBuffer ip, localPath;
  2910. cur.getName(localPath);
  2911. cur.getIp(ip);
  2912. factory->asyncRemoveFile(ip.str(), localPath.str());
  2913. }
  2914. }
  2915. }
  2916. }
  2917. }
  2918. StringBuffer apath;
  2919. // PROGLOG("wuid dll files removed");
  2920. {
  2921. apath.append("/WorkUnitAborts/").append(p->queryName());
  2922. Owned<IRemoteConnection> acon = factory->sdsManager->connect(apath.str(), factory->session, RTM_LOCK_WRITE | RTM_DELETE_ON_DISCONNECT, SDS_LOCK_TIMEOUT);
  2923. acon.clear();
  2924. }
  2925. // PROGLOG("wuid WorkUnitAborts entry removed");
  2926. deleteTempFiles(NULL, deleteOwned, true); // all, any remaining.
  2927. }
  2928. catch(IException *E)
  2929. {
  2930. StringBuffer s;
  2931. LOG(MCexception(E, MSGCLS_warning), E, s.append("Exception during cleanupAndDelete: ").append(p->queryName()).str());
  2932. E->Release();
  2933. }
  2934. catch (...)
  2935. {
  2936. WARNLOG("Unknown exception during cleanupAndDelete: %s", p->queryName());
  2937. }
  2938. CConstGraphProgress::deleteWuidProgress(p->queryName());
  2939. connection->close(true);
  2940. PROGLOG("WUID %s removed",p->queryName());
  2941. connection.clear();
  2942. }
  2943. void CLocalWorkUnit::setTimeScheduled(const IJlibDateTime &val)
  2944. {
  2945. SCMStringBuffer strval;
  2946. val.getGmtString(strval);
  2947. p->setProp("@timescheduled",strval.str());
  2948. }
  2949. IJlibDateTime & CLocalWorkUnit::getTimeScheduled(IJlibDateTime &val) const
  2950. {
  2951. StringBuffer str;
  2952. p->getProp("@timescheduled",str);
  2953. if(str.length())
  2954. val.setGmtString(str.str());
  2955. return val;
  2956. }
  2957. bool modifyAndWriteWorkUnitXML(char const * wuid, StringBuffer & buf, StringBuffer & extra, IFileIO * fileio)
  2958. {
  2959. // kludge in extra chunks of XML such as GraphProgress and GeneratedDlls
  2960. if(extra.length())
  2961. {
  2962. size32_t l = (size32_t)strlen(wuid);
  2963. size32_t p = buf.length()-l-4; // bit of a kludge
  2964. assertex(memcmp(buf.str()+p+2,wuid,l)==0);
  2965. StringAttr tail(buf.str()+p);
  2966. buf.setLength(p);
  2967. buf.append(extra);
  2968. buf.append(tail);
  2969. }
  2970. return (fileio->write(0,buf.length(),buf.str()) == buf.length());
  2971. }
  2972. bool CLocalWorkUnit::archiveWorkUnit(const char *base,bool del,bool ignoredllerrors,bool deleteOwned)
  2973. {
  2974. CriticalBlock block(crit);
  2975. StringBuffer path(base);
  2976. if (!p)
  2977. return false;
  2978. const char *wuid = p->queryName();
  2979. if (!wuid||!*wuid)
  2980. return false;
  2981. addPathSepChar(path).append(wuid).append(".xml");
  2982. Owned<IFile> file = createIFile(path.str());
  2983. if (!file)
  2984. return false;
  2985. Owned<IFileIO> fileio = file->open(IFOcreate);
  2986. if (!fileio)
  2987. return false;
  2988. StringBuffer buf;
  2989. exportWorkUnitToXML(this, buf, false, false);
  2990. StringBuffer extraWorkUnitXML;
  2991. StringBuffer xpath("/GraphProgress/");
  2992. xpath.append(wuid);
  2993. Owned<IRemoteConnection> conn = querySDS().connect(xpath.str(), myProcessSession(), RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  2994. if (conn)
  2995. {
  2996. Owned<IPropertyTree> tmp = createPTree("GraphProgress");
  2997. mergePTree(tmp,conn->queryRoot());
  2998. toXML(tmp,extraWorkUnitXML,1,XML_Format);
  2999. conn->close();
  3000. }
  3001. Owned<IConstWUQuery> q = getQuery();
  3002. if (!q)
  3003. {
  3004. if (!modifyAndWriteWorkUnitXML(wuid, buf, extraWorkUnitXML, fileio))
  3005. return false;
  3006. if (del)
  3007. {
  3008. if (getState()==WUStateUnknown)
  3009. setState(WUStateArchived); // to allow delete
  3010. cleanupAndDelete(false,deleteOwned); // no query, may as well delete
  3011. }
  3012. return false;
  3013. }
  3014. StringArray deleteExclusions; // associated files not to delete, added if failure to copy
  3015. Owned<IConstWUAssociatedFileIterator> iter = &q->getAssociatedFiles();
  3016. Owned<IPropertyTree> generatedDlls = createPTree("GeneratedDlls");
  3017. ForEach(*iter)
  3018. {
  3019. IConstWUAssociatedFile & cur = iter->query();
  3020. SCMStringBuffer name;
  3021. cur.getNameTail(name);
  3022. if (name.length())
  3023. {
  3024. Owned<IDllEntry> entry = queryDllServer().getEntry(name.str());
  3025. SCMStringBuffer curPath, curIp;
  3026. cur.getName(curPath);
  3027. cur.getIp(curIp);
  3028. SocketEndpoint curEp(curIp.str());
  3029. RemoteFilename curRfn;
  3030. curRfn.setPath(curEp, curPath.str());
  3031. StringBuffer dst(base);
  3032. addPathSepChar(dst);
  3033. curRfn.getTail(dst);
  3034. Owned<IFile> dstFile = createIFile(dst.str());
  3035. if (entry.get())
  3036. {
  3037. Owned<IException> exception;
  3038. Owned<IDllLocation> loc;
  3039. Owned<IPropertyTree> generatedDllBranch = createPTree();
  3040. generatedDllBranch->setProp("@name", entry->queryName());
  3041. generatedDllBranch->setProp("@kind", entry->queryKind());
  3042. try
  3043. {
  3044. loc.setown(entry->getBestLocation()); //throws exception if no readable locations
  3045. }
  3046. catch(IException * e)
  3047. {
  3048. exception.setown(e);
  3049. loc.setown(entry->getBestLocationCandidate()); //this will be closest of the unreadable locations
  3050. }
  3051. RemoteFilename filename;
  3052. loc->getDllFilename(filename);
  3053. if (!exception)
  3054. {
  3055. Owned<IFile> srcfile = createIFile(filename);
  3056. try
  3057. {
  3058. if (dstFile->exists())
  3059. {
  3060. if (streq(srcfile->queryFilename(), dstFile->queryFilename()))
  3061. deleteExclusions.append(name.str()); // restored workunit, referencing archive location for query dll (no longer true post HPCC-11191 fix)
  3062. // still want to delete if already archived but there are source file copies
  3063. }
  3064. else
  3065. copyFile(dstFile, srcfile);
  3066. }
  3067. catch(IException * e)
  3068. {
  3069. exception.setown(e);
  3070. }
  3071. }
  3072. if (exception)
  3073. {
  3074. if (ignoredllerrors)
  3075. {
  3076. EXCLOG(exception.get(), "archiveWorkUnit (copying associated file)");
  3077. //copy failed, so don't delete the registred dll files
  3078. deleteExclusions.append(name.str());
  3079. }
  3080. else
  3081. throw exception.getClear();
  3082. }
  3083. // Record Associated path to restore back to
  3084. StringBuffer restorePath;
  3085. curRfn.getRemotePath(restorePath);
  3086. generatedDllBranch->setProp("@location", restorePath.str());
  3087. generatedDlls->addPropTree("GeneratedDll", generatedDllBranch.getClear());
  3088. }
  3089. else // no generated dll entry
  3090. {
  3091. Owned<IFile> srcFile = createIFile(curRfn);
  3092. try
  3093. {
  3094. copyFile(dstFile, srcFile);
  3095. }
  3096. catch (IException *e)
  3097. {
  3098. VStringBuffer msg("Failed to archive associated file '%s' to destination '%s'", srcFile->queryFilename(), dstFile->queryFilename());
  3099. EXCLOG(e, msg.str());
  3100. e->Release();
  3101. deleteExclusions.append(name.str());
  3102. }
  3103. }
  3104. }
  3105. }
  3106. iter.clear();
  3107. if (generatedDlls->numChildren())
  3108. toXML(generatedDlls, extraWorkUnitXML, 1, XML_Format);
  3109. if (!modifyAndWriteWorkUnitXML(wuid, buf, extraWorkUnitXML, fileio))
  3110. return false;
  3111. if (del)
  3112. {
  3113. //setState(WUStateArchived); // this isn't useful as about to delete it!
  3114. q.clear();
  3115. cleanupAndDelete(true, deleteOwned, &deleteExclusions);
  3116. }
  3117. return true;
  3118. }
  3119. void CLocalWorkUnit::packWorkUnit(bool pack)
  3120. {
  3121. // only packs Graph info currently
  3122. CriticalBlock block(crit);
  3123. if (!p)
  3124. return;
  3125. const char *wuid = p->queryName();
  3126. if (!wuid||!*wuid)
  3127. return;
  3128. if (pack) {
  3129. if (!p->hasProp("PackedGraphs")) {
  3130. cachedGraphs.clear();
  3131. IPropertyTree *t = p->queryPropTree("Graphs");
  3132. if (t) {
  3133. MemoryBuffer buf;
  3134. t->serialize(buf);
  3135. p->setPropBin("PackedGraphs",buf.length(),buf.bufferBase());
  3136. p->removeTree(t);
  3137. }
  3138. }
  3139. }
  3140. else {
  3141. ensureGraphsUnpacked();
  3142. }
  3143. CConstGraphProgress::packProgress(wuid,pack);
  3144. }
  3145. IPropertyTree * pruneBranch(IPropertyTree * from, char const * xpath)
  3146. {
  3147. Owned<IPropertyTree> ret;
  3148. IPropertyTree * branch = from->queryPropTree(xpath);
  3149. if(branch) {
  3150. ret.setown(createPTreeFromIPT(branch));
  3151. from->removeTree(branch);
  3152. }
  3153. return ret.getClear();
  3154. }
  3155. bool restoreWorkUnit(const char *base,const char *wuid)
  3156. {
  3157. StringBuffer path(base);
  3158. if (!wuid||!*wuid)
  3159. return false;
  3160. addPathSepChar(path).append(wuid).append(".xml");
  3161. Owned<IFile> file = createIFile(path.str());
  3162. if (!file)
  3163. return false;
  3164. Owned<IFileIO> fileio = file->open(IFOread);
  3165. if (!fileio)
  3166. return false;
  3167. Owned<IPropertyTree> pt = createPTree(*fileio);
  3168. if (!pt)
  3169. return false;
  3170. CDateTime dt;
  3171. dt.setNow();
  3172. StringBuffer dts;
  3173. dt.getString(dts);
  3174. pt->setProp("@restoredDate", dts.str());
  3175. VStringBuffer xpath("/WorkUnits/%s", wuid);
  3176. Owned<IRemoteConnection> conn = querySDS().connect(xpath.str(), myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  3177. if (!conn)
  3178. {
  3179. ERRLOG("restoreWorkUnit could not create to %s", xpath.str());
  3180. return false;
  3181. }
  3182. IPropertyTree *root = conn->queryRoot();
  3183. if (root->hasChildren())
  3184. {
  3185. ERRLOG("restoreWorkUnit WUID %s already exists", wuid);
  3186. return false;
  3187. }
  3188. Owned<IPropertyTree> gprogress = pruneBranch(pt, "GraphProgress[1]");
  3189. Owned<IPropertyTree> generatedDlls = pruneBranch(pt, "GeneratedDlls[1]");
  3190. Owned<IPropertyTree> associatedFiles;
  3191. IPropertyTree *srcAssociated = pt->queryPropTree("Query/Associated");
  3192. if (srcAssociated)
  3193. associatedFiles.setown(createPTreeFromIPT(srcAssociated));
  3194. root->setPropTree(NULL, pt.getClear());
  3195. conn.clear();
  3196. // now kludge back GraphProgress and GeneratedDlls
  3197. if (gprogress)
  3198. {
  3199. VStringBuffer xpath("/GraphProgress/%s", wuid);
  3200. conn.setown(querySDS().connect(xpath, myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT));
  3201. if (conn)
  3202. {
  3203. IPropertyTree *groot = conn->queryRoot();
  3204. if (groot->hasChildren())
  3205. WARNLOG("restoreWorkUnit WUID %s graphprogress already exists, replacing",wuid);
  3206. groot->setPropTree(NULL, gprogress.getClear());
  3207. }
  3208. }
  3209. if (generatedDlls)
  3210. {
  3211. Owned<IPropertyTreeIterator> dlls = generatedDlls->getElements("GeneratedDll");
  3212. for(dlls->first(); dlls->isValid(); dlls->next())
  3213. {
  3214. IPropertyTree & dll = dlls->query();
  3215. char const * name = dll.queryProp("@name");
  3216. char const * kind = dll.queryProp("@kind");
  3217. char const * location = dll.queryProp("@location");
  3218. Owned<IDllEntry> got = queryDllServer().getEntry(name);
  3219. if (!got)
  3220. {
  3221. RemoteFilename dstRfn;
  3222. dstRfn.setRemotePath(location);
  3223. StringBuffer srcPath(base);
  3224. addPathSepChar(srcPath);
  3225. dstRfn.getTail(srcPath);
  3226. OwnedIFile srcFile = createIFile(srcPath);
  3227. OwnedIFile dstFile = createIFile(dstRfn);
  3228. copyFile(dstFile, srcFile);
  3229. queryDllServer().registerDll(name, kind, location);
  3230. }
  3231. }
  3232. }
  3233. if (associatedFiles)
  3234. {
  3235. Owned<IPropertyTreeIterator> associated = associatedFiles->getElements("*");
  3236. ForEach(*associated)
  3237. {
  3238. IPropertyTree &file = associated->query();
  3239. const char *filename = file.queryProp("@filename");
  3240. SocketEndpoint ep(file.queryProp("@ip"));
  3241. RemoteFilename rfn;
  3242. rfn.setPath(ep, filename);
  3243. OwnedIFile dstFile = createIFile(rfn);
  3244. StringBuffer srcPath(base), name;
  3245. addPathSepChar(srcPath);
  3246. rfn.getTail(name);
  3247. srcPath.append(name);
  3248. if (generatedDlls)
  3249. {
  3250. VStringBuffer gDllPath("GeneratedDll[@name=\"%s\"]", name.str());
  3251. if (generatedDlls->hasProp(gDllPath))
  3252. continue; // generated dlls handled separately - see above
  3253. }
  3254. OwnedIFile srcFile = createIFile(srcPath);
  3255. if (srcFile->exists())
  3256. {
  3257. try
  3258. {
  3259. copyFile(dstFile, srcFile);
  3260. }
  3261. catch (IException *e)
  3262. {
  3263. VStringBuffer msg("Failed to restore associated file '%s' to destination '%s'", srcFile->queryFilename(), dstFile->queryFilename());
  3264. EXCLOG(e, msg.str());
  3265. e->Release();
  3266. }
  3267. }
  3268. }
  3269. }
  3270. return true;
  3271. }
  3272. void CLocalWorkUnit::loadXML(const char *xml)
  3273. {
  3274. CriticalBlock block(crit);
  3275. init();
  3276. assertex(xml);
  3277. p.setown(createPTreeFromXMLString(xml));
  3278. }
  3279. void CLocalWorkUnit::serialize(MemoryBuffer &tgt)
  3280. {
  3281. CriticalBlock block(crit);
  3282. StringBuffer x;
  3283. tgt.append(exportWorkUnitToXML(this, x, false, false).str());
  3284. }
  3285. void CLocalWorkUnit::deserialize(MemoryBuffer &src)
  3286. {
  3287. CriticalBlock block(crit);
  3288. StringAttr value;
  3289. src.read(value);
  3290. loadXML(value);
  3291. }
  3292. void CLocalWorkUnit::notify(SubscriptionId id, const char *xpath, SDSNotifyFlags flags, unsigned valueLen, const void *valueData)
  3293. {
  3294. dirty = true;
  3295. }
  3296. void CLocalWorkUnit::abort()
  3297. {
  3298. abortDirty = true;
  3299. }
  3300. void CLocalWorkUnit::requestAbort()
  3301. {
  3302. CriticalBlock block(crit);
  3303. abortWorkUnit(p->queryName());
  3304. }
  3305. void CLocalWorkUnit::subscribe(WUSubscribeOptions options)
  3306. {
  3307. CriticalBlock block(crit);
  3308. bool subscribeAbort = false;
  3309. bool subscribeChange = false;
  3310. bool needChildren = true;
  3311. switch (options)
  3312. {
  3313. case SubscribeOptionAbort:
  3314. subscribeAbort = true;
  3315. break;
  3316. case SubscribeOptionRunningState:
  3317. needChildren = false;
  3318. case SubscribeOptionAnyState:
  3319. subscribeAbort = true;
  3320. subscribeChange = true;
  3321. break;
  3322. case SubscribeOptionProgress:
  3323. case SubscribeOptionAll:
  3324. subscribeChange = true;
  3325. break;
  3326. }
  3327. if (subscribeChange)
  3328. {
  3329. if (changeWatcher && changeWatcher->watchingChildren() != needChildren)
  3330. {
  3331. changeWatcher->unsubscribe();
  3332. changeWatcher.clear();
  3333. }
  3334. if (!changeWatcher)
  3335. {
  3336. changeWatcher.setown(new CWorkUnitWatcher(this, p->queryName(), needChildren));
  3337. dirty = true;
  3338. }
  3339. }
  3340. if (subscribeAbort && !abortWatcher)
  3341. {
  3342. abortWatcher.setown(new CWorkUnitAbortWatcher(this, p->queryName()));
  3343. abortDirty = true;
  3344. }
  3345. }
  3346. void CLocalWorkUnit::forceReload()
  3347. {
  3348. dirty = true;
  3349. reload();
  3350. }
  3351. bool CLocalWorkUnit::reload()
  3352. {
  3353. CriticalBlock block(crit);
  3354. if (dirty)
  3355. {
  3356. if (!connectAtRoot)
  3357. {
  3358. StringBuffer wuRoot;
  3359. getXPath(wuRoot, p->queryName());
  3360. IRemoteConnection *newconn = factory->sdsManager->connect(wuRoot.str(), factory->session, 0, SDS_LOCK_TIMEOUT);
  3361. if (!newconn)
  3362. throw MakeStringException(WUERR_ConnectFailed, "Could not connect to workunit %s (deleted?)",p->queryName());
  3363. connection.setown(newconn);
  3364. connectAtRoot = true;
  3365. }
  3366. else
  3367. connection->reload();
  3368. init();
  3369. p.setown(connection->getRoot());
  3370. return true;
  3371. }
  3372. return false;
  3373. }
  3374. void CLocalWorkUnit::unsubscribe()
  3375. {
  3376. CriticalBlock block(crit);
  3377. if (abortWatcher)
  3378. {
  3379. abortWatcher->unsubscribe();
  3380. abortWatcher.clear();
  3381. }
  3382. if (changeWatcher)
  3383. {
  3384. changeWatcher->unsubscribe();
  3385. changeWatcher.clear();
  3386. }
  3387. }
  3388. void CLocalWorkUnit::unlockRemote(bool commit)
  3389. {
  3390. CriticalBlock block(crit);
  3391. locked.unlock();
  3392. if (commit)
  3393. {
  3394. try
  3395. {
  3396. assertex(connectAtRoot);
  3397. setTimeStamp("workunit", NULL, "Modified",false);
  3398. try { connection->commit(); }
  3399. catch (IException *e)
  3400. {
  3401. EXCLOG(e, "Error during workunit commit");
  3402. connection->rollback();
  3403. connection->changeMode(0, SDS_LOCK_TIMEOUT);
  3404. throw;
  3405. }
  3406. connection->changeMode(0, SDS_LOCK_TIMEOUT);
  3407. }
  3408. catch (IException *E)
  3409. {
  3410. StringBuffer s;
  3411. PrintLog("Failed to release write lock on workunit: %s", E->errorMessage(s).str());
  3412. throw;
  3413. }
  3414. }
  3415. }
  3416. IWorkUnit &CLocalWorkUnit::lockRemote(bool commit)
  3417. {
  3418. if (secMgr)
  3419. checkWuSecAccess(*this, *secMgr.get(), secUser.get(), SecAccess_Write, "write lock", true, true);
  3420. locked.lock();
  3421. CriticalBlock block(crit);
  3422. if (commit)
  3423. {
  3424. try
  3425. {
  3426. StringBuffer wuRoot;
  3427. getXPath(wuRoot, p->queryName());
  3428. if (connection&&connectAtRoot)
  3429. connection->changeMode(RTM_LOCK_WRITE,SDS_LOCK_TIMEOUT);
  3430. else
  3431. connection.setown(factory->sdsManager->connect(wuRoot.str(), factory->session, RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT));
  3432. if (!connection)
  3433. throw MakeStringException(WUERR_LockFailed, "Failed to get connection for xpath %s", wuRoot.str());
  3434. connectAtRoot = true;
  3435. init();
  3436. p.setown(connection->getRoot());
  3437. }
  3438. catch (IException *E)
  3439. {
  3440. StringBuffer s;
  3441. PrintLog("Failed to get write lock on workunit: %s", E->errorMessage(s).str());
  3442. locked.unlock();
  3443. throw;
  3444. }
  3445. }
  3446. return *new CLockedWorkUnit(LINK(this));
  3447. }
  3448. void CLocalWorkUnit::commit()
  3449. {
  3450. CriticalBlock block(crit);
  3451. assertex(connectAtRoot);
  3452. if (connection)
  3453. connection->commit();
  3454. }
  3455. IWorkUnit& CLocalWorkUnit::lock()
  3456. {
  3457. return lockRemote(true);
  3458. }
  3459. IStringVal& CLocalWorkUnit::getWuid(IStringVal &str) const
  3460. {
  3461. CriticalBlock block(crit);
  3462. str.set(p->queryName());
  3463. return str;
  3464. }
  3465. unsigned CLocalWorkUnit::getDebugAgentListenerPort() const
  3466. {
  3467. CriticalBlock block(crit);
  3468. return p->getPropInt("@DebugListenerPort", 0);
  3469. }
  3470. void CLocalWorkUnit::setDebugAgentListenerPort(unsigned port)
  3471. {
  3472. CriticalBlock block(crit);
  3473. p->setPropInt("@DebugListenerPort", port);
  3474. }
  3475. IStringVal& CLocalWorkUnit::getDebugAgentListenerIP(IStringVal &ip) const
  3476. {
  3477. CriticalBlock block(crit);
  3478. ip.set(p->queryProp("@DebugListenerIP"));
  3479. return ip;
  3480. }
  3481. void CLocalWorkUnit::setDebugAgentListenerIP(const char * ip)
  3482. {
  3483. CriticalBlock block(crit);
  3484. p->setProp("@DebugListenerIP", ip);
  3485. }
  3486. IStringVal& CLocalWorkUnit::getSecurityToken(IStringVal &str) const
  3487. {
  3488. CriticalBlock block(crit);
  3489. str.set(p->queryProp("@token"));
  3490. return str;
  3491. }
  3492. void CLocalWorkUnit::setSecurityToken(const char *value)
  3493. {
  3494. CriticalBlock block(crit);
  3495. p->setProp("@token", value);
  3496. }
  3497. bool CLocalWorkUnit::getRunningGraph(IStringVal &graphName, WUGraphIDType &subId) const
  3498. {
  3499. return CConstGraphProgress::getRunningGraph(p->queryName(), graphName, subId);
  3500. }
  3501. void CLocalWorkUnit::setJobName(const char *value)
  3502. {
  3503. CriticalBlock block(crit);
  3504. p->setProp("@jobName", value);
  3505. }
  3506. IStringVal& CLocalWorkUnit::getJobName(IStringVal &str) const
  3507. {
  3508. CriticalBlock block(crit);
  3509. str.set(p->queryProp("@jobName"));
  3510. return str;
  3511. }
  3512. void CLocalWorkUnit::setClusterName(const char *value)
  3513. {
  3514. CriticalBlock block(crit);
  3515. p->setProp("@clusterName", value);
  3516. }
  3517. IStringVal& CLocalWorkUnit::getClusterName(IStringVal &str) const
  3518. {
  3519. CriticalBlock block(crit);
  3520. str.set(p->queryProp("@clusterName"));
  3521. return str;
  3522. }
  3523. void CLocalWorkUnit::setAllowedClusters(const char *value)
  3524. {
  3525. setDebugValue("allowedclusters",value, true);
  3526. }
  3527. IStringVal& CLocalWorkUnit::getAllowedClusters(IStringVal &str) const
  3528. {
  3529. CriticalBlock block(crit);
  3530. getDebugValue("allowedclusters",str);
  3531. if (str.length()!=0)
  3532. return str;
  3533. str.set(p->queryProp("@clusterName"));
  3534. return str;
  3535. }
  3536. void CLocalWorkUnit::setAllowAutoQueueSwitch(bool val)
  3537. {
  3538. setDebugValueInt("allowautoqueueswitch",val?1:0,true);
  3539. }
  3540. bool CLocalWorkUnit::getAllowAutoQueueSwitch() const
  3541. {
  3542. CriticalBlock block(crit);
  3543. return getDebugValueBool("allowautoqueueswitch",false);
  3544. }
  3545. void CLocalWorkUnit::setLibraryInformation(const char * name, unsigned interfaceHash, unsigned definitionHash)
  3546. {
  3547. StringBuffer suffix;
  3548. if (name && *name)
  3549. setApplicationValue("LibraryModule", "name", name, true);
  3550. setApplicationValueInt("LibraryModule", "interfaceHash", interfaceHash, true);
  3551. setApplicationValueInt("LibraryModule", "definitionHash", definitionHash, true);
  3552. setApplicationValue("LibraryModule", "platform", appendLibrarySuffix(suffix).str(), true);
  3553. }
  3554. void CLocalWorkUnit::remoteCheckAccess(IUserDescriptor *user, bool writeaccess) const
  3555. {
  3556. unsigned auditflags = DALI_LDAP_AUDIT_REPORT|DALI_LDAP_READ_WANTED;
  3557. if (writeaccess)
  3558. auditflags |= DALI_LDAP_WRITE_WANTED;
  3559. int perm = 255;
  3560. const char *scopename = p->queryProp("@scope");
  3561. if (scopename&&*scopename) {
  3562. if (!user)
  3563. user = queryUserDescriptor();
  3564. perm = querySessionManager().getPermissionsLDAP("workunit",scopename,user,auditflags);
  3565. if (perm<0) {
  3566. if (perm==-1)
  3567. perm = 255;
  3568. else
  3569. perm = 0;
  3570. }
  3571. }
  3572. if (!HASREADPERMISSION(perm)) {
  3573. SCMStringBuffer wuid;
  3574. getWuid(wuid);
  3575. throw MakeStringException(WUERR_WorkunitAccessDenied, "Read access denied for workunit %s",wuid.s.str());
  3576. }
  3577. if (writeaccess&&!HASWRITEPERMISSION(perm)) {
  3578. SCMStringBuffer wuid;
  3579. getWuid(wuid);
  3580. throw MakeStringException(WUERR_WorkunitAccessDenied, "Write access denied for workunit %s",wuid.s.str());
  3581. }
  3582. }
  3583. IStringVal& CLocalWorkUnit::getParentWuid(IStringVal &str) const
  3584. {
  3585. CriticalBlock block(crit);
  3586. str.set(p->queryProp("@parent"));
  3587. return str;
  3588. }
  3589. void CLocalWorkUnit::setUser(const char * value)
  3590. {
  3591. CriticalBlock block(crit);
  3592. p->setProp("@submitID", value);
  3593. }
  3594. IStringVal& CLocalWorkUnit::getUser(IStringVal &str) const
  3595. {
  3596. CriticalBlock block(crit);
  3597. str.set(p->queryProp("@submitID"));
  3598. return str;
  3599. }
  3600. void CLocalWorkUnit::setWuScope(const char * value)
  3601. {
  3602. CriticalBlock block(crit);
  3603. p->setProp("@scope", value);
  3604. }
  3605. IStringVal& CLocalWorkUnit::getWuScope(IStringVal &str) const
  3606. {
  3607. CriticalBlock block(crit);
  3608. str.set(p->queryProp("@scope"));
  3609. return str;
  3610. }
  3611. void CLocalWorkUnit::setCustomerId(const char * value)
  3612. {
  3613. CriticalBlock block(crit);
  3614. p->setProp("CustomerID", value);
  3615. }
  3616. IStringVal& CLocalWorkUnit::getCustomerId(IStringVal &str) const
  3617. {
  3618. CriticalBlock block(crit);
  3619. str.set(p->queryProp("CustomerID"));
  3620. return str;
  3621. }
  3622. mapEnums priorityClasses[] = {
  3623. { PriorityClassUnknown, "unknown" },
  3624. { PriorityClassLow, "low" },
  3625. { PriorityClassNormal, "normal" },
  3626. { PriorityClassHigh, "high" },
  3627. { PriorityClassSize, NULL },
  3628. };
  3629. void CLocalWorkUnit::setPriority(WUPriorityClass cls)
  3630. {
  3631. CriticalBlock block(crit);
  3632. setEnum(p, "@priorityClass", cls, priorityClasses);
  3633. }
  3634. WUPriorityClass CLocalWorkUnit::getPriority() const
  3635. {
  3636. CriticalBlock block(crit);
  3637. return (WUPriorityClass) getEnum(p, "@priorityClass", priorityClasses);
  3638. }
  3639. mapEnums states[] = {
  3640. { WUStateUnknown, "unknown" },
  3641. { WUStateCompiled, "compiled" },
  3642. { WUStateRunning, "running" },
  3643. { WUStateCompleted, "completed" },
  3644. { WUStateFailed, "failed" },
  3645. { WUStateArchived, "archived" },
  3646. { WUStateAborting, "aborting" },
  3647. { WUStateAborted, "aborted" },
  3648. { WUStateBlocked, "blocked" },
  3649. { WUStateSubmitted, "submitted" },
  3650. { WUStateScheduled, "scheduled" },
  3651. { WUStateCompiling, "compiling" },
  3652. { WUStateWait, "wait" },
  3653. { WUStateUploadingFiles, "uploading_files" },
  3654. { WUStateDebugPaused, "debugging" },
  3655. { WUStateDebugRunning, "debug_running" },
  3656. { WUStatePaused, "paused" },
  3657. { WUStateSize, NULL }
  3658. };
  3659. const char * getWorkunitStateStr(WUState state)
  3660. {
  3661. return states[state].str;
  3662. }
  3663. IConstWorkUnitIterator * CWorkUnitFactory::getWorkUnitsByState(WUState state)
  3664. {
  3665. StringBuffer path("*");
  3666. path.append("[@state=\"").append(getEnumText(state, states)).append("\"]");
  3667. return getWorkUnitsByXPath(path.str());
  3668. }
  3669. IConstWorkUnitIterator * CSecureWorkUnitFactory::getWorkUnitsByState(WUState state)
  3670. {
  3671. StringBuffer path("*");
  3672. path.append("[@state=\"").append(getEnumText(state, states)).append("\"]");
  3673. return factory->getWorkUnitsByXPath(path.str(), secMgr.get(), secUser.get());
  3674. }
  3675. void CLocalWorkUnit::setState(WUState value)
  3676. {
  3677. CriticalBlock block(crit);
  3678. if (value==WUStateAborted || value==WUStatePaused || value==WUStateCompleted || value==WUStateFailed || value==WUStateSubmitted || value==WUStateWait)
  3679. {
  3680. if (abortWatcher)
  3681. {
  3682. abortWatcher->unsubscribe();
  3683. abortWatcher.clear();
  3684. }
  3685. StringBuffer apath;
  3686. apath.append("/WorkUnitAborts/").append(p->queryName());
  3687. if(factory)
  3688. {
  3689. Owned<IRemoteConnection> acon = factory->sdsManager->connect(apath.str(), factory->session, RTM_LOCK_WRITE|RTM_LOCK_SUB, SDS_LOCK_TIMEOUT);
  3690. if (acon)
  3691. acon->close(true);
  3692. }
  3693. }
  3694. setEnum(p, "@state", value, states);
  3695. if (getDebugValueBool("monitorWorkunit", false))
  3696. {
  3697. switch(value)
  3698. {
  3699. case WUStateAborted:
  3700. FLLOG(MCoperatorWarning, "Workunit %s aborted", p->queryName());
  3701. break;
  3702. case WUStateCompleted:
  3703. FLLOG(MCoperatorProgress, "Workunit %s completed", p->queryName());
  3704. break;
  3705. case WUStateFailed:
  3706. FLLOG(MCoperatorProgress, "Workunit %s failed", p->queryName());
  3707. break;
  3708. }
  3709. }
  3710. p->removeProp("@stateEx");
  3711. }
  3712. void CLocalWorkUnit::setStateEx(const char * text)
  3713. {
  3714. CriticalBlock block(crit);
  3715. p->setProp("@stateEx", text);
  3716. }
  3717. void CLocalWorkUnit::setAgentSession(__int64 sessionId)
  3718. {
  3719. CriticalBlock block(crit);
  3720. p->setPropInt64("@agentSession", sessionId);
  3721. }
  3722. bool CLocalWorkUnit::aborting() const
  3723. {
  3724. CriticalBlock block(crit);
  3725. if (abortDirty)
  3726. {
  3727. if (factory)
  3728. {
  3729. StringBuffer apath;
  3730. apath.append("/WorkUnitAborts/").append(p->queryName());
  3731. Owned<IRemoteConnection> acon = factory->sdsManager->connect(apath.str(), factory->session, 0, SDS_LOCK_TIMEOUT);
  3732. if (acon)
  3733. abortState = acon->queryRoot()->getPropInt(NULL)!=0;
  3734. else
  3735. abortState = false;
  3736. }
  3737. abortDirty = false;
  3738. }
  3739. return abortState;
  3740. }
  3741. bool CLocalWorkUnit::getIsQueryService() const
  3742. {
  3743. CriticalBlock block(crit);
  3744. return p->getPropBool("@isQueryService", false);
  3745. }
  3746. void CLocalWorkUnit::setIsQueryService(bool value)
  3747. {
  3748. CriticalBlock block(crit);
  3749. p->setPropBool("@isQueryService", value);
  3750. }
  3751. void CLocalWorkUnit::checkAgentRunning(WUState & state)
  3752. {
  3753. if (queryDaliServerVersion().compare("2.1")<0)
  3754. return;
  3755. switch(state)
  3756. {
  3757. case WUStateRunning:
  3758. case WUStateDebugPaused:
  3759. case WUStateDebugRunning:
  3760. case WUStateBlocked:
  3761. case WUStateAborting:
  3762. case WUStateCompiling:
  3763. case WUStatePaused:
  3764. {
  3765. SessionId agent = getAgentSession();
  3766. if((agent>0) && querySessionManager().sessionStopped(agent, 0))
  3767. {
  3768. forceReload();
  3769. state = (WUState) getEnum(p, "@state", states);
  3770. bool isecl=state==WUStateCompiling;
  3771. if (aborting())
  3772. state = WUStateAborted;
  3773. else if (state==WUStateRunning || state==WUStatePaused || state==WUStateDebugPaused || state==WUStateDebugRunning || state==WUStateBlocked || state==WUStateCompiling)
  3774. state = WUStateFailed;
  3775. else
  3776. return;
  3777. WARNLOG("checkAgentRunning terminated: %"I64F"d state = %d",(__int64)agent,(int)state);
  3778. Owned<IWorkUnit> w = &lock();
  3779. w->setState(state);
  3780. Owned<IWUException> e = w->createException();
  3781. WUAction action = w->getAction();
  3782. switch (action)
  3783. {
  3784. case WUActionPause:
  3785. case WUActionPauseNow:
  3786. case WUActionResume:
  3787. w->setAction(WUActionUnknown);
  3788. }
  3789. if(isecl)
  3790. {
  3791. e->setExceptionCode(1001);
  3792. e->setExceptionMessage("EclServer terminated unexpectedly");
  3793. }
  3794. else
  3795. {
  3796. e->setExceptionCode(1000);
  3797. e->setExceptionMessage("Workunit terminated unexpectedly");
  3798. }
  3799. }
  3800. }
  3801. }
  3802. }
  3803. WUState CLocalWorkUnit::getState() const
  3804. {
  3805. CriticalBlock block(crit);
  3806. WUState state = (WUState) getEnum(p, "@state", states);
  3807. switch (state)
  3808. {
  3809. case WUStateRunning:
  3810. case WUStateDebugPaused:
  3811. case WUStateDebugRunning:
  3812. case WUStateBlocked:
  3813. case WUStateCompiling:
  3814. if (aborting())
  3815. state = WUStateAborting;
  3816. break;
  3817. case WUStateSubmitted:
  3818. if (aborting())
  3819. state = WUStateAborted;
  3820. break;
  3821. }
  3822. const_cast<CLocalWorkUnit *>(this)->checkAgentRunning(state); //need const_cast as will change state if agent has died
  3823. return state;
  3824. }
  3825. IStringVal& CLocalWorkUnit::getStateEx(IStringVal & str) const
  3826. {
  3827. CriticalBlock block(crit);
  3828. str.set(p->queryProp("@stateEx"));
  3829. return str;
  3830. }
  3831. __int64 CLocalWorkUnit::getAgentSession() const
  3832. {
  3833. CriticalBlock block(crit);
  3834. return p->getPropInt64("@agentSession", -1);
  3835. }
  3836. unsigned CLocalWorkUnit::getAgentPID() const
  3837. {
  3838. CriticalBlock block(crit);
  3839. return p->getPropInt("@agentPID", -1);
  3840. }
  3841. IStringVal& CLocalWorkUnit::getStateDesc(IStringVal &str) const
  3842. {
  3843. // MORE - not sure about this - may prefer a separate interface
  3844. CriticalBlock block(crit);
  3845. try
  3846. {
  3847. str.set(getEnumText(getState(), states));
  3848. }
  3849. catch (...)
  3850. {
  3851. str.set("???");
  3852. }
  3853. return str;
  3854. }
  3855. mapEnums actions[] = {
  3856. { WUActionUnknown, "unknown" },
  3857. { WUActionCompile, "compile" },
  3858. { WUActionCheck, "check" },
  3859. { WUActionRun, "run" },
  3860. { WUActionExecuteExisting, "execute" },
  3861. { WUActionPause, "pause" },
  3862. { WUActionPauseNow, "pausenow" },
  3863. { WUActionResume, "resume" },
  3864. { WUActionSize, NULL },
  3865. };
  3866. void CLocalWorkUnit::setAction(WUAction value)
  3867. {
  3868. CriticalBlock block(crit);
  3869. setEnum(p, "Action", value, actions);
  3870. }
  3871. WUAction CLocalWorkUnit::getAction() const
  3872. {
  3873. CriticalBlock block(crit);
  3874. return (WUAction) getEnum(p, "Action", actions);
  3875. }
  3876. IStringVal& CLocalWorkUnit::getActionEx(IStringVal & str) const
  3877. {
  3878. CriticalBlock block(crit);
  3879. str.set(p->queryProp("Action"));
  3880. return str;
  3881. }
  3882. IStringVal& CLocalWorkUnit::getApplicationValue(const char *app, const char *propname, IStringVal &str) const
  3883. {
  3884. CriticalBlock block(crit);
  3885. StringBuffer prop("Application/");
  3886. prop.append(app).append('/').append(propname);
  3887. str.set(p->queryProp(prop.str()));
  3888. return str;
  3889. }
  3890. int CLocalWorkUnit::getApplicationValueInt(const char *app, const char *propname, int defVal) const
  3891. {
  3892. CriticalBlock block(crit);
  3893. StringBuffer prop("Application/");
  3894. prop.append(app).append('/').append(propname);
  3895. return p->getPropInt(prop.str(), defVal);
  3896. }
  3897. IConstWUAppValueIterator& CLocalWorkUnit::getApplicationValues() const
  3898. {
  3899. CriticalBlock block(crit);
  3900. appvalues.load(p,"Application/*");
  3901. return *new CArrayIteratorOf<IConstWUAppValue,IConstWUAppValueIterator> (appvalues, 0, (IConstWorkUnit *) this);
  3902. }
  3903. void CLocalWorkUnit::setApplicationValue(const char *app, const char *propname, const char *value, bool overwrite)
  3904. {
  3905. CriticalBlock block(crit);
  3906. StringBuffer prop("Application/");
  3907. prop.append(app).append('/').append(propname);
  3908. if (overwrite || !p->hasProp(prop.str()))
  3909. {
  3910. // MORE - not sure these lines should be needed....
  3911. StringBuffer sp;
  3912. p->setProp(sp.append("Application").str(), "");
  3913. p->setProp(sp.append('/').append(app).str(), "");
  3914. p->setProp(prop.str(), value);
  3915. }
  3916. }
  3917. void CLocalWorkUnit::setApplicationValueInt(const char *app, const char *propname, int value, bool overwrite)
  3918. {
  3919. CriticalBlock block(crit);
  3920. StringBuffer prop("Application/");
  3921. prop.append(app).append('/').append(propname);
  3922. if (overwrite || !p->hasProp(prop.str()))
  3923. {
  3924. // MORE - not sure these lines should be needed....
  3925. StringBuffer sp;
  3926. p->setProp(sp.append("Application").str(), "");
  3927. p->setProp(sp.append('/').append(app).str(), "");
  3928. p->setPropInt(prop.str(), value);
  3929. }
  3930. }
  3931. void CLocalWorkUnit::setPriorityLevel(int level)
  3932. {
  3933. CriticalBlock block(crit);
  3934. p->setPropInt("PriorityFlag", level);
  3935. }
  3936. int CLocalWorkUnit::getPriorityLevel() const
  3937. {
  3938. CriticalBlock block(crit);
  3939. return p->getPropInt("PriorityFlag");
  3940. }
  3941. int calcPriorityValue(const IPropertyTree * p)
  3942. {
  3943. int priority = p->getPropInt("PriorityFlag");
  3944. switch((WUPriorityClass) getEnum(p, "@priorityClass", priorityClasses))
  3945. {
  3946. case PriorityClassLow:
  3947. priority -= 100;
  3948. break;
  3949. case PriorityClassHigh:
  3950. priority += 100;
  3951. break;
  3952. }
  3953. return priority;
  3954. }
  3955. int CLocalWorkUnit::getPriorityValue() const
  3956. {
  3957. CriticalBlock block(crit);
  3958. return calcPriorityValue(p);
  3959. }
  3960. void CLocalWorkUnit::setRescheduleFlag(bool value)
  3961. {
  3962. CriticalBlock block(crit);
  3963. p->setPropInt("RescheduleFlag", (int) value);
  3964. }
  3965. bool CLocalWorkUnit::getRescheduleFlag() const
  3966. {
  3967. CriticalBlock block(crit);
  3968. return p->getPropInt("RescheduleFlag") != 0;
  3969. }
  3970. class NullIStringIterator : public CInterface, extends IStringIterator
  3971. {
  3972. public:
  3973. IMPLEMENT_IINTERFACE;
  3974. bool first() { return false; }
  3975. bool next() { return false; }
  3976. bool isValid() { return false; }
  3977. IStringVal & str(IStringVal & str) { return str; }
  3978. };
  3979. ClusterType getClusterType(const char * platform, ClusterType dft)
  3980. {
  3981. if (stricmp(platform, "thor") == 0)
  3982. return ThorLCRCluster;
  3983. if (stricmp(platform, "thorlcr") == 0)
  3984. return ThorLCRCluster;
  3985. if (stricmp(platform, "hthor") == 0)
  3986. return HThorCluster;
  3987. if (stricmp(platform, "roxie") == 0)
  3988. return RoxieCluster;
  3989. return dft;
  3990. }
  3991. const char *clusterTypeString(ClusterType clusterType, bool lcrSensitive)
  3992. {
  3993. switch (clusterType)
  3994. {
  3995. case ThorLCRCluster:
  3996. if (lcrSensitive)
  3997. return "thorlcr";
  3998. return "thor";
  3999. case RoxieCluster:
  4000. return "roxie";
  4001. case HThorCluster:
  4002. return "hthor";
  4003. }
  4004. throwUnexpected();
  4005. }
  4006. IPropertyTree *queryRoxieProcessTree(IPropertyTree *environment, const char *process)
  4007. {
  4008. if (!process || !*process)
  4009. return NULL;
  4010. VStringBuffer xpath("Software/RoxieCluster[@name=\"%s\"]", process);
  4011. return environment->queryPropTree(xpath.str());
  4012. }
  4013. void getRoxieProcessServers(IPropertyTree *roxie, SocketEndpointArray &endpoints)
  4014. {
  4015. if (!roxie)
  4016. return;
  4017. Owned<IPropertyTreeIterator> servers = roxie->getElements("RoxieServerProcess");
  4018. ForEach(*servers)
  4019. {
  4020. IPropertyTree &server = servers->query();
  4021. const char *netAddress = server.queryProp("@netAddress");
  4022. if (netAddress && *netAddress)
  4023. {
  4024. SocketEndpoint ep(netAddress, server.getPropInt("@port", 9876));
  4025. endpoints.append(ep);
  4026. }
  4027. }
  4028. }
  4029. void getRoxieProcessServers(const char *process, SocketEndpointArray &servers)
  4030. {
  4031. Owned<IRemoteConnection> conn = querySDS().connect("Environment", myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT);
  4032. if (!conn)
  4033. return;
  4034. getRoxieProcessServers(queryRoxieProcessTree(conn->queryRoot(), process), servers);
  4035. }
  4036. class CEnvironmentClusterInfo: public CInterface, implements IConstWUClusterInfo
  4037. {
  4038. StringAttr name;
  4039. StringAttr serverQueue;
  4040. StringAttr agentQueue;
  4041. StringAttr roxieProcess;
  4042. SocketEndpointArray roxieServers;
  4043. StringAttr thorQueue;
  4044. StringArray thorProcesses;
  4045. StringArray primaryThorProcesses;
  4046. StringAttr prefix;
  4047. StringAttr ldapUser;
  4048. StringBuffer ldapPassword;
  4049. ClusterType platform;
  4050. unsigned clusterWidth;
  4051. public:
  4052. IMPLEMENT_IINTERFACE;
  4053. CEnvironmentClusterInfo(const char *_name, const char *_prefix, IPropertyTree *agent, IArrayOf<IPropertyTree> &thors, IPropertyTree *roxie)
  4054. : name(_name), prefix(_prefix)
  4055. {
  4056. StringBuffer queue;
  4057. if (thors.ordinality())
  4058. {
  4059. thorQueue.set(getClusterThorQueueName(queue.clear(), name));
  4060. clusterWidth = 0;
  4061. bool isMultiThor = (thors.length() > 1);
  4062. ForEachItemIn(i,thors)
  4063. {
  4064. IPropertyTree &thor = thors.item(i);
  4065. const char* thorName = thor.queryProp("@name");
  4066. thorProcesses.append(thorName);
  4067. if (!isMultiThor)
  4068. primaryThorProcesses.append(thorName);
  4069. else
  4070. {
  4071. const char *nodeGroup = thor.queryProp("@nodeGroup");
  4072. if (!nodeGroup || strieq(nodeGroup, thorName))
  4073. primaryThorProcesses.append(thorName);
  4074. }
  4075. unsigned nodes = thor.getCount("ThorSlaveProcess");
  4076. if (!nodes)
  4077. throw MakeStringException(WUERR_MismatchClusterSize,"CEnvironmentClusterInfo: Thor cluster can not have 0 slave processes");
  4078. unsigned ts = nodes * thor.getPropInt("@slavesPerNode", 1);
  4079. if (clusterWidth && (ts!=clusterWidth))
  4080. throw MakeStringException(WUERR_MismatchClusterSize,"CEnvironmentClusterInfo: mismatched thor sizes in cluster");
  4081. clusterWidth = ts;
  4082. bool islcr = !thor.getPropBool("@Legacy");
  4083. if (!islcr)
  4084. throw MakeStringException(WUERR_MismatchThorType,"CEnvironmentClusterInfo: Legacy Thor no longer supported");
  4085. }
  4086. platform = ThorLCRCluster;
  4087. }
  4088. else if (roxie)
  4089. {
  4090. roxieProcess.set(roxie->queryProp("@name"));
  4091. platform = RoxieCluster;
  4092. getRoxieProcessServers(roxie, roxieServers);
  4093. clusterWidth = roxieServers.length();
  4094. ldapUser.set(roxie->queryProp("@ldapUser"));
  4095. StringBuffer encPassword = roxie->queryProp("@ldapPassword");
  4096. if (encPassword.length())
  4097. decrypt(ldapPassword, encPassword);
  4098. }
  4099. else
  4100. {
  4101. clusterWidth = 1;
  4102. platform = HThorCluster;
  4103. }
  4104. if (agent)
  4105. {
  4106. assertex(!roxie);
  4107. agentQueue.set(getClusterEclAgentQueueName(queue.clear(), name));
  4108. }
  4109. else if (roxie)
  4110. agentQueue.set(getClusterRoxieQueueName(queue.clear(), name));
  4111. // MORE - does this need to be conditional?
  4112. serverQueue.set(getClusterEclCCServerQueueName(queue.clear(), name));
  4113. }
  4114. IStringVal & getName(IStringVal & str) const
  4115. {
  4116. str.set(name.get());
  4117. return str;
  4118. }
  4119. IStringVal & getScope(IStringVal & str) const
  4120. {
  4121. str.set(prefix.get());
  4122. return str;
  4123. }
  4124. IStringVal & getAgentQueue(IStringVal & str) const
  4125. {
  4126. str.set(agentQueue);
  4127. return str;
  4128. }
  4129. virtual IStringVal & getServerQueue(IStringVal & str) const
  4130. {
  4131. str.set(serverQueue);
  4132. return str;
  4133. }
  4134. IStringVal & getThorQueue(IStringVal & str) const
  4135. {
  4136. str.set(thorQueue);
  4137. return str;
  4138. }
  4139. unsigned getSize() const
  4140. {
  4141. return clusterWidth;
  4142. }
  4143. virtual ClusterType getPlatform() const
  4144. {
  4145. return platform;
  4146. }
  4147. IStringVal & getRoxieProcess(IStringVal & str) const
  4148. {
  4149. str.set(roxieProcess.get());
  4150. return str;
  4151. }
  4152. const StringArray & getThorProcesses() const
  4153. {
  4154. return thorProcesses;
  4155. }
  4156. const StringArray & getPrimaryThorProcesses() const
  4157. {
  4158. return primaryThorProcesses;
  4159. }
  4160. const SocketEndpointArray & getRoxieServers() const
  4161. {
  4162. return roxieServers;
  4163. }
  4164. const char *getLdapUser() const
  4165. {
  4166. return ldapUser.get();
  4167. }
  4168. virtual const char *getLdapPassword() const
  4169. {
  4170. return ldapPassword.str();
  4171. }
  4172. };
  4173. IStringVal &getProcessQueueNames(IStringVal &ret, const char *process, const char *type, const char *suffix)
  4174. {
  4175. if (process)
  4176. {
  4177. Owned<IRemoteConnection> conn = querySDS().connect("Environment", myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT);
  4178. if (conn)
  4179. {
  4180. StringBuffer queueNames;
  4181. StringBuffer xpath;
  4182. xpath.appendf("%s[@process=\"%s\"]", type, process);
  4183. Owned<IPropertyTreeIterator> targets = conn->queryRoot()->getElements("Software/Topology/Cluster");
  4184. ForEach(*targets)
  4185. {
  4186. IPropertyTree &target = targets->query();
  4187. if (target.hasProp(xpath))
  4188. {
  4189. if (queueNames.length())
  4190. queueNames.append(',');
  4191. queueNames.append(target.queryProp("@name")).append(suffix);
  4192. }
  4193. }
  4194. ret.set(queueNames);
  4195. }
  4196. }
  4197. return ret;
  4198. }
  4199. #define ROXIE_QUEUE_EXT ".roxie"
  4200. #define THOR_QUEUE_EXT ".thor"
  4201. #define ECLCCSERVER_QUEUE_EXT ".eclserver"
  4202. #define ECLSERVER_QUEUE_EXT ECLCCSERVER_QUEUE_EXT
  4203. #define ECLSCHEDULER_QUEUE_EXT ".eclscheduler"
  4204. #define ECLAGENT_QUEUE_EXT ".agent"
  4205. extern WORKUNIT_API IStringVal &getEclCCServerQueueNames(IStringVal &ret, const char *process)
  4206. {
  4207. return getProcessQueueNames(ret, process, "EclCCServerProcess", ECLCCSERVER_QUEUE_EXT);
  4208. }
  4209. extern WORKUNIT_API IStringVal &getEclServerQueueNames(IStringVal &ret, const char *process)
  4210. {
  4211. return getProcessQueueNames(ret, process, "EclServerProcess", ECLSERVER_QUEUE_EXT); // shares queue name with EclCCServer
  4212. }
  4213. extern WORKUNIT_API IStringVal &getEclSchedulerQueueNames(IStringVal &ret, const char *process)
  4214. {
  4215. return getProcessQueueNames(ret, process, "EclSchedulerProcess", ECLSCHEDULER_QUEUE_EXT); // Shares deployment/config with EclCCServer
  4216. }
  4217. extern WORKUNIT_API IStringVal &getAgentQueueNames(IStringVal &ret, const char *process)
  4218. {
  4219. return getProcessQueueNames(ret, process, "EclAgentProcess", ECLAGENT_QUEUE_EXT);
  4220. }
  4221. extern WORKUNIT_API IStringVal &getRoxieQueueNames(IStringVal &ret, const char *process)
  4222. {
  4223. return getProcessQueueNames(ret, process, "RoxieCluster", ROXIE_QUEUE_EXT);
  4224. }
  4225. extern WORKUNIT_API IStringVal &getThorQueueNames(IStringVal &ret, const char *process)
  4226. {
  4227. return getProcessQueueNames(ret, process, "ThorCluster", THOR_QUEUE_EXT);
  4228. }
  4229. extern WORKUNIT_API StringBuffer &getClusterThorQueueName(StringBuffer &ret, const char *cluster)
  4230. {
  4231. return ret.append(cluster).append(THOR_QUEUE_EXT);
  4232. }
  4233. extern WORKUNIT_API StringBuffer &getClusterThorGroupName(StringBuffer &ret, const char *cluster)
  4234. {
  4235. StringBuffer path;
  4236. Owned<IRemoteConnection> conn = querySDS().connect(path.append("Environment/Software/ThorCluster[@name=\"").append(cluster).append("\"]").str(), myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT);
  4237. if (conn)
  4238. {
  4239. getClusterGroupName(*conn->queryRoot(), ret);
  4240. }
  4241. return ret;
  4242. }
  4243. extern WORKUNIT_API StringBuffer &getClusterRoxieQueueName(StringBuffer &ret, const char *cluster)
  4244. {
  4245. return ret.append(cluster).append(ROXIE_QUEUE_EXT);
  4246. }
  4247. extern WORKUNIT_API StringBuffer &getClusterEclCCServerQueueName(StringBuffer &ret, const char *cluster)
  4248. {
  4249. return ret.append(cluster).append(ECLCCSERVER_QUEUE_EXT);
  4250. }
  4251. extern WORKUNIT_API StringBuffer &getClusterEclServerQueueName(StringBuffer &ret, const char *cluster)
  4252. {
  4253. return ret.append(cluster).append(ECLSERVER_QUEUE_EXT);
  4254. }
  4255. extern WORKUNIT_API StringBuffer &getClusterEclAgentQueueName(StringBuffer &ret, const char *cluster)
  4256. {
  4257. return ret.append(cluster).append(ECLAGENT_QUEUE_EXT);
  4258. }
  4259. extern WORKUNIT_API IStringIterator *getTargetClusters(const char *processType, const char *processName)
  4260. {
  4261. Owned<IRemoteConnection> conn = querySDS().connect("Environment", myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT);
  4262. Owned<CStringArrayIterator> ret = new CStringArrayIterator;
  4263. if (conn)
  4264. {
  4265. StringBuffer xpath;
  4266. xpath.appendf("%s", processType ? processType : "*");
  4267. if (processName && *processName)
  4268. xpath.appendf("[@process=\"%s\"]", processName);
  4269. Owned<IPropertyTreeIterator> targets = conn->queryRoot()->getElements("Software/Topology/Cluster");
  4270. ForEach(*targets)
  4271. {
  4272. IPropertyTree &target = targets->query();
  4273. if (target.hasProp(xpath))
  4274. {
  4275. ret->append(target.queryProp("@name"));
  4276. }
  4277. }
  4278. }
  4279. return ret.getClear();
  4280. }
  4281. extern WORKUNIT_API bool isProcessCluster(const char *process)
  4282. {
  4283. if (!process || !*process)
  4284. return false;
  4285. Owned<IRemoteConnection> conn = querySDS().connect("Environment", myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT);
  4286. if (!conn)
  4287. return false;
  4288. VStringBuffer xpath("Software/*Cluster[@name=\"%s\"]", process);
  4289. return conn->queryRoot()->hasProp(xpath.str());
  4290. }
  4291. extern WORKUNIT_API bool isProcessCluster(const char *remoteDali, const char *process)
  4292. {
  4293. if (!remoteDali || !*remoteDali)
  4294. return isProcessCluster(process);
  4295. if (!process || !*process)
  4296. return false;
  4297. Owned<INode> remote = createINode(remoteDali, 7070);
  4298. if (!remote)
  4299. return false;
  4300. VStringBuffer xpath("Environment/Software/*Cluster[@name=\"%s\"]/@name", process);
  4301. try
  4302. {
  4303. Owned<IPropertyTreeIterator> clusters = querySDS().getElementsRaw(xpath, remote, 1000*60*1);
  4304. return clusters->first();
  4305. }
  4306. catch (IException *E)
  4307. {
  4308. StringBuffer msg;
  4309. E->errorMessage(msg);
  4310. DBGLOG("Exception validating cluster %s/%s: %s", remoteDali, xpath.str(), msg.str());
  4311. E->Release();
  4312. }
  4313. return true;
  4314. }
  4315. IConstWUClusterInfo* getTargetClusterInfo(IPropertyTree *environment, IPropertyTree *cluster)
  4316. {
  4317. const char *clustname = cluster->queryProp("@name");
  4318. // MORE - at the moment configenf specifies eclagent and thor queues by (in effect) placing an 'example' thor or eclagent in the topology
  4319. // that uses the queue that will be used.
  4320. // We should and I hope will change that, at which point the code below gets simpler
  4321. StringBuffer prefix(cluster->queryProp("@prefix"));
  4322. prefix.toLowerCase();
  4323. StringBuffer xpath;
  4324. StringBuffer querySetName;
  4325. IPropertyTree *agent = NULL;
  4326. const char *agentName = cluster->queryProp("EclAgentProcess/@process");
  4327. if (agentName)
  4328. {
  4329. xpath.clear().appendf("Software/EclAgentProcess[@name=\"%s\"]", agentName);
  4330. agent = environment->queryPropTree(xpath.str());
  4331. }
  4332. Owned<IPropertyTreeIterator> ti = cluster->getElements("ThorCluster");
  4333. IArrayOf<IPropertyTree> thors;
  4334. ForEach(*ti)
  4335. {
  4336. const char *thorName = ti->query().queryProp("@process");
  4337. if (thorName)
  4338. {
  4339. xpath.clear().appendf("Software/ThorCluster[@name=\"%s\"]", thorName);
  4340. thors.append(*environment->getPropTree(xpath.str()));
  4341. }
  4342. }
  4343. const char *roxieName = cluster->queryProp("RoxieCluster/@process");
  4344. return new CEnvironmentClusterInfo(clustname, prefix, agent, thors, queryRoxieProcessTree(environment, roxieName));
  4345. }
  4346. IPropertyTree* getTopologyCluster(Owned<IRemoteConnection> &conn, const char *clustname)
  4347. {
  4348. if (!clustname || !*clustname)
  4349. return NULL;
  4350. conn.setown(querySDS().connect("Environment", myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT));
  4351. if (!conn)
  4352. return NULL;
  4353. StringBuffer xpath;
  4354. xpath.appendf("Software/Topology/Cluster[@name=\"%s\"]", clustname);
  4355. return conn->queryRoot()->getPropTree(xpath.str());
  4356. }
  4357. bool validateTargetClusterName(const char *clustname)
  4358. {
  4359. Owned<IRemoteConnection> conn;
  4360. Owned<IPropertyTree> cluster = getTopologyCluster(conn, clustname);
  4361. return (cluster.get()!=NULL);
  4362. }
  4363. IConstWUClusterInfo* getTargetClusterInfo(const char *clustname)
  4364. {
  4365. Owned<IRemoteConnection> conn;
  4366. Owned<IPropertyTree> cluster = getTopologyCluster(conn, clustname);
  4367. if (!cluster)
  4368. return NULL;
  4369. return getTargetClusterInfo(conn->queryRoot(), cluster);
  4370. }
  4371. unsigned getEnvironmentClusterInfo(CConstWUClusterInfoArray &clusters)
  4372. {
  4373. Owned<IRemoteConnection> conn = querySDS().connect("Environment", myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT);
  4374. if (!conn)
  4375. return 0;
  4376. return getEnvironmentClusterInfo(conn->queryRoot(), clusters);
  4377. }
  4378. unsigned getEnvironmentClusterInfo(IPropertyTree* environmentRoot, CConstWUClusterInfoArray &clusters)
  4379. {
  4380. if (!environmentRoot)
  4381. return 0;
  4382. Owned<IPropertyTreeIterator> clusterIter = environmentRoot->getElements("Software/Topology/Cluster");
  4383. ForEach(*clusterIter)
  4384. {
  4385. IPropertyTree &node = clusterIter->query();
  4386. Owned<IConstWUClusterInfo> cluster = getTargetClusterInfo(environmentRoot, &node);
  4387. clusters.append(*cluster.getClear());
  4388. }
  4389. return clusters.ordinality();
  4390. }
  4391. const char *getTargetClusterComponentName(const char *clustname, const char *processType, StringBuffer &name)
  4392. {
  4393. if (!clustname)
  4394. return NULL;
  4395. Owned<IRemoteConnection> conn = querySDS().connect("Environment", myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT);
  4396. if (!conn)
  4397. return NULL;
  4398. StringBuffer xpath;
  4399. xpath.appendf("Software/Topology/Cluster[@name=\"%s\"]", clustname);
  4400. Owned<IPropertyTree> cluster = conn->queryRoot()->getPropTree(xpath.str());
  4401. if (!cluster)
  4402. return NULL;
  4403. StringBuffer xpath1;
  4404. xpath1.appendf("%s/@process", processType);
  4405. name.append(cluster->queryProp(xpath1.str()));
  4406. return name.str();
  4407. }
  4408. unsigned getEnvironmentThorClusterNames(StringArray &thorNames, StringArray &groupNames, StringArray &targetNames, StringArray &queueNames)
  4409. {
  4410. Owned<IRemoteConnection> conn = querySDS().connect("Environment", myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT);
  4411. if (!conn)
  4412. return 0;
  4413. Owned<IPropertyTreeIterator> allTargets = conn->queryRoot()->getElements("Software/Topology/Cluster");
  4414. ForEach(*allTargets)
  4415. {
  4416. IPropertyTree &target = allTargets->query();
  4417. const char *targetName = target.queryProp("@name");
  4418. if (targetName && *targetName)
  4419. {
  4420. Owned<IPropertyTreeIterator> thorClusters = target.getElements("ThorCluster");
  4421. ForEach(*thorClusters)
  4422. {
  4423. const char *thorName = thorClusters->query().queryProp("@process");
  4424. VStringBuffer query("Software/ThorCluster[@name=\"%s\"]",thorName);
  4425. IPropertyTree *thorCluster = conn->queryRoot()->queryPropTree(query.str());
  4426. if (thorCluster)
  4427. {
  4428. const char *groupName = thorCluster->queryProp("@nodeGroup");
  4429. if (!groupName||!*groupName)
  4430. groupName = thorName;
  4431. thorNames.append(thorName);
  4432. groupNames.append(groupName);
  4433. targetNames.append(targetName);
  4434. StringBuffer queueName(targetName);
  4435. queueNames.append(queueName.append(THOR_QUEUE_EXT));
  4436. }
  4437. }
  4438. }
  4439. }
  4440. return thorNames.ordinality();
  4441. }
  4442. unsigned getEnvironmentHThorClusterNames(StringArray &eclAgentNames, StringArray &groupNames, StringArray &targetNames)
  4443. {
  4444. Owned<IRemoteConnection> conn = querySDS().connect("Environment", myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT);
  4445. if (!conn)
  4446. return 0;
  4447. Owned<IPropertyTreeIterator> allEclAgents = conn->queryRoot()->getElements("Software/EclAgentProcess");
  4448. ForEach(*allEclAgents)
  4449. {
  4450. IPropertyTree &eclAgent = allEclAgents->query();
  4451. const char *eclAgentName = eclAgent.queryProp("@name");
  4452. if (eclAgentName && *eclAgentName)
  4453. {
  4454. Owned<IPropertyTreeIterator> allTargets = conn->queryRoot()->getElements("Software/Topology/Cluster");
  4455. ForEach(*allTargets)
  4456. {
  4457. IPropertyTree &target = allTargets->query();
  4458. const char *targetName = target.queryProp("@name");
  4459. if (targetName && *targetName)
  4460. {
  4461. StringBuffer xpath;
  4462. xpath.appendf("EclAgentProcess[@process=\"%s\"]", eclAgentName);
  4463. if (target.hasProp(xpath) && !target.hasProp("ThorCluster"))
  4464. {
  4465. StringBuffer groupName("hthor__");
  4466. groupName.append(eclAgentName);
  4467. groupNames.append(groupName);
  4468. eclAgentNames.append(eclAgentName);
  4469. targetNames.append(targetName);
  4470. }
  4471. }
  4472. }
  4473. }
  4474. }
  4475. return eclAgentNames.ordinality();
  4476. }
  4477. IStringVal& CLocalWorkUnit::getScope(IStringVal &str) const
  4478. {
  4479. CriticalBlock block(crit);
  4480. if (p->hasProp("Debug/ForceScope"))
  4481. {
  4482. StringBuffer prefix(p->queryProp("Debug/ForceScope"));
  4483. str.set(prefix.toLowerCase().str());
  4484. }
  4485. else
  4486. {
  4487. Owned <IConstWUClusterInfo> ci = getTargetClusterInfo(p->queryProp("@clusterName"));
  4488. if (ci)
  4489. ci->getScope(str);
  4490. else
  4491. str.clear();
  4492. }
  4493. return str;
  4494. }
  4495. //Queries
  4496. void CLocalWorkUnit::setCodeVersion(unsigned codeVersion, const char * buildVersion, const char * eclVersion)
  4497. {
  4498. CriticalBlock block(crit);
  4499. p->setPropInt("@codeVersion", codeVersion);
  4500. p->setProp("@buildVersion", buildVersion);
  4501. p->setProp("@eclVersion", eclVersion);
  4502. }
  4503. unsigned CLocalWorkUnit::getCodeVersion() const
  4504. {
  4505. CriticalBlock block(crit);
  4506. return p->getPropInt("@codeVersion");
  4507. }
  4508. unsigned CLocalWorkUnit::getWuidVersion() const
  4509. {
  4510. CriticalBlock block(crit);
  4511. return p->getPropInt("@wuidVersion");
  4512. }
  4513. void CLocalWorkUnit::getBuildVersion(IStringVal & buildVersion, IStringVal & eclVersion) const
  4514. {
  4515. CriticalBlock block(crit);
  4516. buildVersion.set(p->queryProp("@buildVersion"));
  4517. eclVersion.set(p->queryProp("@eclVersion"));
  4518. }
  4519. void CLocalWorkUnit::setCloneable(bool value)
  4520. {
  4521. CriticalBlock block(crit);
  4522. p->setPropInt("@cloneable", value);
  4523. }
  4524. void CLocalWorkUnit::setIsClone(bool value)
  4525. {
  4526. CriticalBlock block(crit);
  4527. p->setPropInt("@isClone", value);
  4528. }
  4529. bool CLocalWorkUnit::getCloneable() const
  4530. {
  4531. CriticalBlock block(crit);
  4532. return p->getPropBool("@cloneable", false);
  4533. }
  4534. IUserDescriptor *CLocalWorkUnit::queryUserDescriptor() const
  4535. {
  4536. CriticalBlock block(crit);
  4537. if (!userDesc)
  4538. {
  4539. SCMStringBuffer token, user, password;
  4540. getSecurityToken(token);
  4541. SCMStringBuffer wuid;
  4542. getWuid(wuid);
  4543. extractToken(token.str(), wuid.str(), user, password);
  4544. userDesc.setown(createUserDescriptor());
  4545. userDesc->set(user.str(), password.str());
  4546. }
  4547. return userDesc;
  4548. }
  4549. void CLocalWorkUnit::setCombineQueries(unsigned combine)
  4550. {
  4551. CriticalBlock block(crit);
  4552. p->setPropInt("COMBINE_QUERIES", combine);
  4553. }
  4554. unsigned CLocalWorkUnit::getCombineQueries() const
  4555. {
  4556. CriticalBlock block(crit);
  4557. return p->getPropInt("COMBINE_QUERIES");
  4558. }
  4559. bool CLocalWorkUnit::isProtected() const
  4560. {
  4561. CriticalBlock block(crit);
  4562. return p->getPropBool("@protected", false);
  4563. }
  4564. bool CLocalWorkUnit::isPausing() const
  4565. {
  4566. CriticalBlock block(crit);
  4567. if (WUActionPause == getAction())
  4568. {
  4569. switch (getState())
  4570. {
  4571. case WUStateRunning:
  4572. case WUStateAborting:
  4573. return true;
  4574. }
  4575. }
  4576. return false;
  4577. }
  4578. void CLocalWorkUnit::protect(bool protectMode)
  4579. {
  4580. CriticalBlock block(crit);
  4581. p->setPropBool("@protected", protectMode);
  4582. }
  4583. bool CLocalWorkUnit::isBilled() const
  4584. {
  4585. CriticalBlock block(crit);
  4586. return p->getPropBool("@billed", false);
  4587. }
  4588. void CLocalWorkUnit::setBilled(bool value)
  4589. {
  4590. CriticalBlock block(crit);
  4591. p->setPropBool("@billed", value);
  4592. }
  4593. void CLocalWorkUnit::setResultLimit(unsigned value)
  4594. {
  4595. CriticalBlock block(crit);
  4596. p->setPropInt("resultLimit", value);
  4597. }
  4598. unsigned CLocalWorkUnit::getResultLimit() const
  4599. {
  4600. CriticalBlock block(crit);
  4601. return p->getPropInt("resultLimit");
  4602. }
  4603. void CLocalWorkUnit::setCompareMode(WUCompareMode value)
  4604. {
  4605. CriticalBlock block(crit);
  4606. p->setPropInt("comparemode", (int)value);
  4607. }
  4608. WUCompareMode CLocalWorkUnit::getCompareMode() const
  4609. {
  4610. CriticalBlock block(crit);
  4611. return (WUCompareMode) p->getPropInt("comparemode");
  4612. }
  4613. IStringVal & CLocalWorkUnit::getSnapshot(IStringVal & str) const
  4614. {
  4615. CriticalBlock block(crit);
  4616. str.set(p->queryProp("SNAPSHOT"));
  4617. return str;
  4618. }
  4619. void CLocalWorkUnit::setSnapshot(const char * val)
  4620. {
  4621. CriticalBlock block(crit);
  4622. p->setProp("SNAPSHOT", val);
  4623. }
  4624. static int comparePropTrees(IInterface **ll, IInterface **rr)
  4625. {
  4626. IPropertyTree *l = (IPropertyTree *) *ll;
  4627. IPropertyTree *r = (IPropertyTree *) *rr;
  4628. return stricmp(l->queryName(), r->queryName());
  4629. };
  4630. unsigned CLocalWorkUnit::calculateHash(unsigned crc)
  4631. {
  4632. // Any other values in the WU that could affect generated code should be crc'ed here
  4633. IPropertyTree *tree = p->queryBranch("Debug");
  4634. if (tree)
  4635. {
  4636. Owned<IPropertyTreeIterator> sub = tree->getElements("*");
  4637. ICopyArrayOf<IPropertyTree> subs;
  4638. for(sub->first(); sub->isValid(); sub->next())
  4639. subs.append(sub->query());
  4640. subs.sort(comparePropTrees);
  4641. ForEachItemIn(idx, subs)
  4642. {
  4643. const char *name = subs.item(idx).queryName();
  4644. const char *val = subs.item(idx).queryProp(NULL);
  4645. crc = crc32(name, (size32_t)strlen(name), crc);
  4646. if (val)
  4647. crc = crc32(val, (size32_t)strlen(val), crc);
  4648. }
  4649. }
  4650. Owned<IConstWUPluginIterator> plugins = &getPlugins();
  4651. for (plugins->first();plugins->isValid();plugins->next())
  4652. {
  4653. IConstWUPlugin &thisplugin = plugins->query();
  4654. SCMStringBuffer version;
  4655. thisplugin.getPluginVersion(version);
  4656. crc = crc32(version.str(), version.length(), crc);
  4657. }
  4658. return crc;
  4659. }
  4660. static void updateProp(IPropertyTree * to, const IPropertyTree * from, const char * xpath)
  4661. {
  4662. if (!to->hasProp(xpath) && from->hasProp(xpath))
  4663. to->setProp(xpath, from->queryProp(xpath));
  4664. }
  4665. static void setProp(IPropertyTree * to, const IPropertyTree * from, const char * xpath)
  4666. {
  4667. if (from->hasProp(xpath))
  4668. to->setProp(xpath, from->queryProp(xpath));
  4669. }
  4670. static void copyTree(IPropertyTree * to, const IPropertyTree * from, const char * xpath)
  4671. {
  4672. IPropertyTree * match = from->getBranch(xpath);
  4673. if (match)
  4674. to->setPropTree(xpath, match);
  4675. }
  4676. void CLocalWorkUnit::copyWorkUnit(IConstWorkUnit *cached, bool all)
  4677. {
  4678. CLocalWorkUnit *from = QUERYINTERFACE(cached, CLocalWorkUnit);
  4679. if (!from)
  4680. {
  4681. CLockedWorkUnit *fl = QUERYINTERFACE(cached, CLockedWorkUnit);
  4682. if (!fl)
  4683. throw MakeStringException(WUERR_InternalUnknownImplementation, "Cached workunit not created using workunit dll");
  4684. from = fl->c;
  4685. }
  4686. // Need to copy the query, the results, and the graphs from the cached query.
  4687. // The cache is made before the query is executed so there is no need to clear them.
  4688. if (!cached->getCloneable())
  4689. throw MakeStringException(WUERR_CannotCloneWorkunit, "Source work unit not marked as clonable");
  4690. const IPropertyTree * fromP = from->p;
  4691. IPropertyTree *pt;
  4692. CriticalBlock block(crit);
  4693. query.clear();
  4694. updateProp(p, fromP, "@jobName");
  4695. copyTree(p, fromP, "Query");
  4696. pt = fromP->getBranch("Application/LibraryModule");
  4697. if (pt)
  4698. {
  4699. ensurePTree(p, "Application");
  4700. p->setPropTree("Application/LibraryModule", pt);
  4701. }
  4702. pt = fromP->queryBranch("Debug");
  4703. if (pt)
  4704. {
  4705. IPropertyTree *curDebug = p->queryPropTree("Debug");
  4706. if (curDebug)
  4707. {
  4708. Owned<IPropertyTreeIterator> elems = pt->getElements("*");
  4709. ForEach(*elems)
  4710. {
  4711. IPropertyTree *elem = &elems->query();
  4712. if (!curDebug->hasProp(elem->queryName()))
  4713. curDebug->setPropTree(elem->queryName(),LINK(elem));
  4714. }
  4715. }
  4716. else
  4717. p->setPropTree("Debug", LINK(pt));
  4718. }
  4719. copyTree(p, fromP, "Plugins");
  4720. copyTree(p, fromP, "Libraries");
  4721. copyTree(p, fromP, "Results");
  4722. copyTree(p, fromP, "Graphs");
  4723. copyTree(p, fromP, "Workflow");
  4724. if (all)
  4725. {
  4726. // 'all' mode is used when setting up a dali WU from the embedded wu in a workunit dll
  4727. // Merge timing info from both branches
  4728. pt = fromP->getBranch("Timings");
  4729. if (pt)
  4730. {
  4731. IPropertyTree *tgtTimings = ensurePTree(p, "Timings");
  4732. mergePTree(tgtTimings, pt);
  4733. pt->Release();
  4734. }
  4735. pt = fromP->getBranch("Statistics");
  4736. if (pt)
  4737. {
  4738. IPropertyTree *tgtStatistics = ensurePTree(p, "Statistics");
  4739. mergePTree(tgtStatistics, pt);
  4740. pt->Release();
  4741. }
  4742. }
  4743. updateProp(p, fromP, "@clusterName");
  4744. updateProp(p, fromP, "allowedclusters");
  4745. updateProp(p, fromP, "@submitID");
  4746. updateProp(p, fromP, "CustomerID");
  4747. updateProp(p, fromP, "SNAPSHOT");
  4748. //MORE: This is very adhoc. All options that should be cloned should really be in a common branch
  4749. if (all)
  4750. {
  4751. setProp(p, fromP, "PriorityFlag");
  4752. setProp(p, fromP, "@priorityClass");
  4753. setProp(p, fromP, "@protected");
  4754. setProp(p, fromP, "@clusterName");
  4755. updateProp(p, fromP, "@scope");
  4756. }
  4757. //Variables may have been set up as parameters to the query - so need to preserve any values that were supplied.
  4758. pt = fromP->getBranch("Variables");
  4759. if (pt)
  4760. {
  4761. IPropertyTree *ptTgtVariables = ensurePTree(p, "Variables");
  4762. Owned<IPropertyTreeIterator> ptiVariable = pt->getElements("Variable");
  4763. for (ptiVariable->first(); ptiVariable->isValid(); ptiVariable->next())
  4764. {
  4765. IPropertyTree *ptSrcVariable = &ptiVariable->query();
  4766. const char *name = ptSrcVariable->queryProp("@name");
  4767. assertex(name);
  4768. StringBuffer xpath;
  4769. xpath.append("Variable[@name='").append(name).append("']");
  4770. IPropertyTree *ptTgtVariable = ptTgtVariables->queryPropTree(xpath.str());
  4771. IPropertyTree *merged = createPTreeFromIPT(ptSrcVariable); // clone entire source info...
  4772. merged->removeProp("Value"); // except value and status
  4773. merged->setProp("@status", "undefined");
  4774. if (!merged->getPropBool("@isScalar"))
  4775. merged->removeProp("totalRowCount");
  4776. merged->removeProp("rowCount");
  4777. // If there are any other fields that get set ONLY by eclagent, strip them out here...
  4778. if (ptTgtVariable)
  4779. {
  4780. // copy status and Value from what is already set in target
  4781. merged->setProp("@status", ptTgtVariable->queryProp("@status"));
  4782. MemoryBuffer value;
  4783. if (ptTgtVariable->getPropBin("Value", value))
  4784. merged->setPropBin("Value", value.length(), value.toByteArray());
  4785. ptTgtVariable->removeProp(xpath.str());
  4786. // If there are any other fields in a variable that get set by ws_ecl before submitting, copy them across here...
  4787. }
  4788. ptTgtVariables->addPropTree("Variable", merged);
  4789. }
  4790. pt->Release();
  4791. }
  4792. p->setProp("@codeVersion", fromP->queryProp("@codeVersion"));
  4793. p->setPropBool("@cloneable", true);
  4794. p->setPropBool("@isClone", true);
  4795. resetWorkflow(); // the source Workflow section may have had some parts already executed...
  4796. // resetResults(); // probably should be resetting the results as well... rather than waiting for the rerun to overwrite them
  4797. }
  4798. bool CLocalWorkUnit::hasDebugValue(const char *propname) const
  4799. {
  4800. StringBuffer lower;
  4801. lower.append(propname).toLowerCase();
  4802. CriticalBlock block(crit);
  4803. StringBuffer prop("Debug/");
  4804. return p->hasProp(prop.append(lower));
  4805. }
  4806. IStringVal& CLocalWorkUnit::getDebugValue(const char *propname, IStringVal &str) const
  4807. {
  4808. StringBuffer lower;
  4809. lower.append(propname).toLowerCase();
  4810. CriticalBlock block(crit);
  4811. StringBuffer prop("Debug/");
  4812. str.set(p->queryProp(prop.append(lower).str()));
  4813. return str;
  4814. }
  4815. IStringIterator& CLocalWorkUnit::getDebugValues() const
  4816. {
  4817. return getDebugValues(NULL);
  4818. }
  4819. IStringIterator& CLocalWorkUnit::getDebugValues(const char *prop) const
  4820. {
  4821. CriticalBlock block(crit);
  4822. StringBuffer path("Debug/");
  4823. if (prop)
  4824. {
  4825. StringBuffer lower;
  4826. lower.append(prop).toLowerCase();
  4827. path.append(lower);
  4828. }
  4829. else
  4830. path.append("*");
  4831. return *new CStringPTreeTagIterator(p->getElements(path.str()));
  4832. }
  4833. int CLocalWorkUnit::getDebugValueInt(const char *propname, int defVal) const
  4834. {
  4835. StringBuffer lower;
  4836. lower.append(propname).toLowerCase();
  4837. CriticalBlock block(crit);
  4838. StringBuffer prop("Debug/");
  4839. prop.append(lower);
  4840. return p->getPropInt(prop.str(), defVal);
  4841. }
  4842. __int64 CLocalWorkUnit::getDebugValueInt64(const char *propname, __int64 defVal) const
  4843. {
  4844. StringBuffer lower;
  4845. lower.append(propname).toLowerCase();
  4846. CriticalBlock block(crit);
  4847. StringBuffer prop("Debug/");
  4848. prop.append(lower);
  4849. return p->getPropInt64(prop.str(), defVal);
  4850. }
  4851. bool CLocalWorkUnit::getDebugValueBool(const char * propname, bool defVal) const
  4852. {
  4853. StringBuffer lower;
  4854. lower.append(propname).toLowerCase();
  4855. CriticalBlock block(crit);
  4856. StringBuffer prop("Debug/");
  4857. prop.append(lower);
  4858. return p->getPropBool(prop.str(), defVal);
  4859. }
  4860. IStringIterator *CLocalWorkUnit::getLogs(const char *type, const char *instance) const
  4861. {
  4862. VStringBuffer xpath("Process/%s/", type);
  4863. if (instance)
  4864. xpath.append(instance);
  4865. else
  4866. xpath.append("*");
  4867. CriticalBlock block(crit);
  4868. if (p->getPropInt("@wuidVersion") < 1) // legacy wuid
  4869. {
  4870. // NB: instance unused
  4871. if (streq("EclAgent", type))
  4872. return new CStringPTreeIterator(p->getElements("Debug/eclagentlog"));
  4873. else if (streq("Thor", type))
  4874. return new CStringPTreeIterator(p->getElements("Debug/thorlog*"));
  4875. VStringBuffer xpath("Debug/%s", type);
  4876. return new CStringPTreeIterator(p->getElements(xpath.str()));
  4877. }
  4878. else
  4879. return new CStringPTreeAttrIterator(p->getElements(xpath.str()), "@log");
  4880. }
  4881. IPropertyTreeIterator* CLocalWorkUnit::getProcesses(const char *type, const char *instance) const
  4882. {
  4883. VStringBuffer xpath("Process/%s/", type);
  4884. if (instance)
  4885. xpath.append(instance);
  4886. else
  4887. xpath.append("*");
  4888. CriticalBlock block(crit);
  4889. return p->getElements(xpath.str());
  4890. }
  4891. IStringIterator *CLocalWorkUnit::getProcesses(const char *type) const
  4892. {
  4893. VStringBuffer xpath("Process/%s/*", type);
  4894. CriticalBlock block(crit);
  4895. return new CStringPTreeTagIterator(p->getElements(xpath.str()));
  4896. }
  4897. void CLocalWorkUnit::addProcess(const char *type, const char *instance, unsigned pid, const char *log)
  4898. {
  4899. VStringBuffer processType("Process/%s", type);
  4900. VStringBuffer xpath("%s/%s", processType.str(), instance);
  4901. if (log)
  4902. xpath.appendf("[@log=\"%s\"]", log);
  4903. CriticalBlock block(crit);
  4904. if (!p->hasProp(xpath))
  4905. {
  4906. IPropertyTree *node = ensurePTree(p, processType.str());
  4907. node = node->addPropTree(instance, createPTree());
  4908. node->setProp("@log", log);
  4909. node->setPropInt("@pid", pid);
  4910. }
  4911. }
  4912. void CLocalWorkUnit::setDebugValue(const char *propname, const char *value, bool overwrite)
  4913. {
  4914. StringBuffer lower;
  4915. lower.append(propname).toLowerCase();
  4916. CriticalBlock block(crit);
  4917. StringBuffer prop("Debug/");
  4918. prop.append(lower);
  4919. if (overwrite || !p->hasProp(prop.str()))
  4920. {
  4921. // MORE - not sure this line should be needed....
  4922. p->setProp("Debug", "");
  4923. p->setProp(prop.str(), value);
  4924. }
  4925. }
  4926. void CLocalWorkUnit::setDebugValueInt(const char *propname, int value, bool overwrite)
  4927. {
  4928. StringBuffer lower;
  4929. lower.append(propname).toLowerCase();
  4930. CriticalBlock block(crit);
  4931. StringBuffer prop("Debug/");
  4932. prop.append(lower);
  4933. if (overwrite || !p->hasProp(prop.str()))
  4934. {
  4935. // MORE - not sure this line should be needed....
  4936. p->setProp("Debug", "");
  4937. p->setPropInt(prop.str(), value);
  4938. }
  4939. }
  4940. void CLocalWorkUnit::setTracingValue(const char *propname, const char *value)
  4941. {
  4942. CriticalBlock block(crit);
  4943. // MORE - not sure this line should be needed....
  4944. p->setProp("Tracing", "");
  4945. StringBuffer prop("Tracing/");
  4946. p->setProp(prop.append(propname).str(), value);
  4947. }
  4948. void CLocalWorkUnit::setTracingValueInt(const char *propname, int value)
  4949. {
  4950. CriticalBlock block(crit);
  4951. StringBuffer prop("Tracing/");
  4952. p->setPropInt(prop.append(propname).str(), value);
  4953. }
  4954. IConstWUQuery* CLocalWorkUnit::getQuery() const
  4955. {
  4956. // For this to be legally called, we must have the read-able interface. So we are already locked for (at least) read.
  4957. CriticalBlock block(crit);
  4958. if (!query)
  4959. {
  4960. IPropertyTree *s = p->getPropTree("Query");
  4961. if (s)
  4962. query.setown(new CLocalWUQuery(s));
  4963. }
  4964. return query.getLink();
  4965. }
  4966. IWUQuery* CLocalWorkUnit::updateQuery()
  4967. {
  4968. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  4969. CriticalBlock block(crit);
  4970. if (!query)
  4971. {
  4972. IPropertyTree *s = p->queryPropTree("Query");
  4973. if (!s)
  4974. s = p->addPropTree("Query", createPTreeFromXMLString("<Query fetchEntire='1'/>"));
  4975. s->Link();
  4976. query.setown(new CLocalWUQuery(s));
  4977. }
  4978. return query.getLink();
  4979. }
  4980. void CLocalWorkUnit::loadPlugins() const
  4981. {
  4982. CriticalBlock block(crit);
  4983. if (!pluginsCached)
  4984. {
  4985. assertex(plugins.length() == 0);
  4986. Owned<IPropertyTreeIterator> r = p->getElements("Plugins/Plugin");
  4987. for (r->first(); r->isValid(); r->next())
  4988. {
  4989. IPropertyTree *rp = &r->query();
  4990. rp->Link();
  4991. plugins.append(*new CLocalWUPlugin(rp));
  4992. }
  4993. pluginsCached = true;
  4994. }
  4995. }
  4996. IConstWUPluginIterator& CLocalWorkUnit::getPlugins() const
  4997. {
  4998. CriticalBlock block(crit);
  4999. loadPlugins();
  5000. return *new CArrayIteratorOf<IConstWUPlugin,IConstWUPluginIterator> (plugins, 0, (IConstWorkUnit *) this);
  5001. }
  5002. void CLocalWorkUnit::loadLibraries() const
  5003. {
  5004. CriticalBlock block(crit);
  5005. if (!librariesCached)
  5006. {
  5007. assertex(libraries.length() == 0);
  5008. Owned<IPropertyTreeIterator> r = p->getElements("Libraries/Library");
  5009. ForEach(*r)
  5010. {
  5011. IPropertyTree *rp = &r->query();
  5012. rp->Link();
  5013. libraries.append(*new CLocalWULibrary(rp));
  5014. }
  5015. librariesCached = true;
  5016. }
  5017. }
  5018. IConstWULibraryIterator& CLocalWorkUnit::getLibraries() const
  5019. {
  5020. CriticalBlock block(crit);
  5021. loadLibraries();
  5022. return *new CArrayIteratorOf<IConstWULibrary,IConstWULibraryIterator> (libraries, 0, (IConstWorkUnit *) this);
  5023. }
  5024. IConstWULibrary * CLocalWorkUnit::getLibraryByName(const char * search) const
  5025. {
  5026. CriticalBlock block(crit);
  5027. loadLibraries();
  5028. ForEachItemIn(idx, libraries)
  5029. {
  5030. SCMStringBuffer name;
  5031. IConstWULibrary &cur = libraries.item(idx);
  5032. cur.getName(name);
  5033. if (stricmp(name.str(), search)==0)
  5034. return &OLINK(cur);
  5035. }
  5036. return NULL;
  5037. }
  5038. unsigned CLocalWorkUnit::getTimerDuration(const char *name) const
  5039. {
  5040. Owned<IConstWUStatistic> stat = getStatisticByDescription(name);
  5041. if (stat)
  5042. {
  5043. unsigned __int64 time = stat->getValue();
  5044. return (unsigned)(time / 1000000);
  5045. }
  5046. //Backward compatibility - but only use it if no statistics
  5047. CriticalBlock block(crit);
  5048. if (p->hasProp("Statistics"))
  5049. return 0;
  5050. StringBuffer pname;
  5051. pname.appendf("Timings/Timing[@name=\"%s\"]/@duration", name);
  5052. return p->getPropInt(pname.str(), 0);
  5053. }
  5054. IStringVal & CLocalWorkUnit::getTimerDescription(const char * name, IStringVal & str) const
  5055. {
  5056. Owned<IConstWUStatistic> stat = getStatisticByDescription(name);
  5057. if (stat)
  5058. return stat->getDescription(str);
  5059. //Backward compatibility - but only use it if no statistics
  5060. CriticalBlock block(crit);
  5061. if (p->hasProp("Statistics"))
  5062. {
  5063. str.clear();
  5064. return str;
  5065. }
  5066. str.set(name);
  5067. return str;
  5068. }
  5069. unsigned CLocalWorkUnit::getTimerCount(const char *name) const
  5070. {
  5071. Owned<IConstWUStatistic> stat = getStatisticByDescription(name);
  5072. if (stat)
  5073. return (unsigned)stat->getCount();
  5074. //Backward compatibility - but only use it if no statistics
  5075. CriticalBlock block(crit);
  5076. if (p->hasProp("Statistics"))
  5077. return 0;
  5078. StringBuffer pname;
  5079. pname.appendf("Timings/Timing[@name=\"%s\"]/@count", name);
  5080. return p->getPropInt(pname.str(), 0);
  5081. }
  5082. IStringIterator& CLocalWorkUnit::getTimers() const
  5083. {
  5084. CriticalBlock block(crit);
  5085. if (p->hasProp("Statistics"))
  5086. return *new CStringPTreeAttrIterator(p->getElements("Statistics/Statistic[@unit=\"ns\"]"), "@desc");
  5087. //Backward compatibility - but only use it if no statistics
  5088. return *new CStringPTreeAttrIterator(p->getElements("Timings/Timing"), "@name");
  5089. }
  5090. IConstWUTimerIterator& CLocalWorkUnit::getTimerIterator() const
  5091. {
  5092. CriticalBlock block(crit);
  5093. loadTimers();
  5094. return *new CArrayIteratorOf<IConstWUTimer,IConstWUTimerIterator> (timers, 0, (IConstWorkUnit *) this);
  5095. }
  5096. class CLocalWUTimer : public CInterface, implements IWUTimer
  5097. {
  5098. StringAttr name;
  5099. unsigned count;
  5100. unsigned duration;
  5101. public:
  5102. IMPLEMENT_IINTERFACE;
  5103. CLocalWUTimer(const char* _name, unsigned _count, unsigned _duration) : name(_name), count(_count), duration(_duration) { };
  5104. virtual IStringVal & getName(IStringVal & str) const { str.set(name.get()); return str; } ;
  5105. virtual unsigned getCount() const { return count; };
  5106. virtual unsigned getDuration() const { return duration; };
  5107. virtual void setName(const char * str) { name.set(str); };
  5108. virtual void setCount(unsigned c) { count = c; };
  5109. virtual void setDuration(unsigned d) { duration = d; };
  5110. };
  5111. void CLocalWorkUnit::loadTimers() const
  5112. {
  5113. CriticalBlock block(crit);
  5114. if (timersCached)
  5115. return;
  5116. assertex(timers.length() == 0);
  5117. Owned<IPropertyTreeIterator> r = p->getElements("Timings/Timing");
  5118. ForEach(*r)
  5119. {
  5120. IPropertyTree *rp = &r->query();
  5121. timers.append(*new CLocalWUTimer(rp->queryProp("@name"), rp->getPropInt("@count"), rp->getPropInt("@duration")));
  5122. }
  5123. timersCached = true;
  5124. }
  5125. StringBuffer &formatGraphTimerLabel(StringBuffer &str, const char *graphName, unsigned subGraphNum, unsigned __int64 subId)
  5126. {
  5127. str.append("Graph ").append(graphName);
  5128. if (subGraphNum) str.append(" - ").append(subGraphNum).append(" (").append(subId).append(")");
  5129. else if (subId) str.append(" - id(").append(subId).append(")");
  5130. return str;
  5131. }
  5132. StringBuffer &formatGraphTimerScope(StringBuffer &str, const char *graphName, unsigned subGraphNum, unsigned __int64 subId)
  5133. {
  5134. str.append(graphName);
  5135. if (subId) str.append(":").append(subId);
  5136. return str;
  5137. }
  5138. bool parseGraphTimerLabel(const char *label, StringAttr &graphName, unsigned & graphNum, unsigned &subGraphNum, unsigned &subId)
  5139. {
  5140. // expects format: "Graph <graphname>[ - <subgraphnum> (<subgraphid>)]"
  5141. unsigned len = (size32_t)strlen(label);
  5142. if (len < 6 || (0 != memcmp(label, "Graph ", 6)))
  5143. return false;
  5144. graphNum = 0;
  5145. subGraphNum = 0;
  5146. subId = 0;
  5147. const char *finger = label+6;
  5148. const char *finger2 = strchr(finger, '-');
  5149. if (NULL == finger2) // just graphName
  5150. graphName.set(finger);
  5151. else
  5152. {
  5153. graphName.set(finger, (size32_t)((finger2-1)-finger));
  5154. finger = finger2+2; // skip '-' and space
  5155. finger2 = strchr(finger, ' ');
  5156. if (finger2)
  5157. {
  5158. subGraphNum = atoi_l(finger, (size32_t)(finger2-finger));
  5159. finger = finger2+2; // skip space and '('
  5160. finger2 = strchr(finger, ')');
  5161. if (finger2)
  5162. subId = atoi_l(finger, (size32_t)(finger2-finger));
  5163. }
  5164. else if (((len-(finger-label))>3) && 0 == memcmp(finger, "id(", 3)) // subgraph id only, new format.
  5165. {
  5166. finger += 3;
  5167. finger2 = strchr(finger, ')');
  5168. if (finger2)
  5169. subId = atoi_l(finger, (size32_t)(finger2-finger));
  5170. }
  5171. }
  5172. if (graphName && !memicmp(graphName, "graph", 5))
  5173. graphNum = atoi(graphName + 5);
  5174. return true;
  5175. }
  5176. void CLocalWorkUnit::setTimerInfo(const char *name, unsigned ms, unsigned count, unsigned __int64 max)
  5177. {
  5178. CriticalBlock block(crit);
  5179. IPropertyTree *timings = p->queryPropTree("Timings");
  5180. if (!timings)
  5181. timings = p->addPropTree("Timings", createPTree("Timings"));
  5182. StringBuffer xpath;
  5183. xpath.append("Timing[@name=\"").append(name).append("\"]");
  5184. IPropertyTree *timing = timings->queryPropTree(xpath.str());
  5185. if (!timing)
  5186. {
  5187. timing = timings->addPropTree("Timing", createPTree("Timing"));
  5188. timing->setProp("@name", name);
  5189. }
  5190. timing->setPropInt("@count", count);
  5191. timing->setPropInt("@duration", ms);
  5192. if (!max && 1==count) max = milliToNano(ms); // max is in nanoseconds
  5193. if (max)
  5194. timing->setPropInt64("@max", max);
  5195. }
  5196. void CLocalWorkUnit::setTimeStamp(const char *application, const char *instance, const char *event, bool add)
  5197. {
  5198. CriticalBlock block(crit);
  5199. char timeStamp[64];
  5200. time_t tNow;
  5201. time(&tNow);
  5202. #ifdef _WIN32
  5203. struct tm *gmtNow;
  5204. gmtNow = gmtime(&tNow);
  5205. strftime(timeStamp, 64, "%Y-%m-%dT%H:%M:%SZ", gmtNow);
  5206. #else
  5207. struct tm gmtNow;
  5208. gmtime_r(&tNow, &gmtNow);
  5209. strftime(timeStamp, 64, "%Y-%m-%dT%H:%M:%SZ", &gmtNow);
  5210. #endif //_WIN32
  5211. IPropertyTree *ts = p->queryPropTree("TimeStamps");
  5212. if (!ts) {
  5213. ts = p->addPropTree("TimeStamps", createPTree("TimeStamps"));
  5214. add = true;
  5215. }
  5216. IPropertyTree *t=NULL;
  5217. if (!add) {
  5218. StringBuffer path;
  5219. path.appendf("TimeStamp[@application=\"%s\"]",application);
  5220. t = ts->queryBranch(path.str());
  5221. }
  5222. if (!t) {
  5223. t = createPTree("TimeStamp");
  5224. t->setProp("@application", application);
  5225. add = true;
  5226. }
  5227. if (instance)
  5228. t->setProp("@instance", instance);
  5229. t->setProp(event, timeStamp);
  5230. IPropertyTree *et = t->queryPropTree(event);
  5231. if(et)
  5232. et->setPropInt("@ts",(int)tNow);
  5233. if (add)
  5234. ts->addPropTree("TimeStamp", t);
  5235. }
  5236. mapEnums queryStatMeasure[] =
  5237. {
  5238. { SMEASURE_TIME_NS, "ns" },
  5239. { SMEASURE_COUNT, "cnt" },
  5240. { SMEASURE_MEM_KB, "kb" },
  5241. { SMEASURE_MAX, NULL},
  5242. };
  5243. void CLocalWorkUnit::setStatistic(const char * creator, const char * wuScope, const char * stat, const char * description, StatisticMeasure kind, unsigned __int64 value, unsigned __int64 count, unsigned __int64 maxValue, bool merge)
  5244. {
  5245. if (!wuScope) wuScope = "workunit";
  5246. //creator. scope and name must all be present, and must not contain semi colons.
  5247. assertex(creator && wuScope && stat);
  5248. dbgassertex(!strchr(creator, ';') && !strchr(wuScope, ';') && !strchr(stat, ';'));
  5249. if (count == 1 && maxValue < value)
  5250. maxValue = value;
  5251. StringBuffer fullname;
  5252. fullname.append(creator).append(";").append(wuScope).append(";").append(stat);
  5253. StringBuffer xpath;
  5254. xpath.append("Statistic[@name=\"").append(fullname).append("\"]");
  5255. CriticalBlock block(crit);
  5256. IPropertyTree * stats = p->queryPropTree("Statistics");
  5257. if (!stats)
  5258. stats = p->addPropTree("Statistics", createPTree("Statistics"));
  5259. IPropertyTree * statTree = stats->queryPropTree(xpath.str());
  5260. if (!statTree)
  5261. {
  5262. //MORE: When getTimings is removed the default description could be dynamically calculated
  5263. StringBuffer descriptionText;
  5264. if (!description || !*description)
  5265. {
  5266. bool isDefaultName = streq(stat, "time");
  5267. bool isDefaultScope = streq(wuScope, "workunit");
  5268. descriptionText.append(creator);
  5269. if (isDefaultName || !isDefaultScope)
  5270. descriptionText.append(": ").append(wuScope);
  5271. if (!isDefaultName)
  5272. descriptionText.append(": ").append(stat);
  5273. description = descriptionText;
  5274. }
  5275. statTree = stats->addPropTree("Statistic", createPTree("Statistic"));
  5276. statTree->setProp("@name", fullname.str());
  5277. if (description)
  5278. statTree->setProp("@desc", description);
  5279. setEnum(statTree, "@unit", kind, queryStatMeasure);
  5280. statTree->setPropInt64("@value", value);
  5281. statTree->setPropInt64("@count", count);
  5282. if (maxValue)
  5283. statTree->setPropInt64("@max", maxValue);
  5284. if (statistics.cached)
  5285. statistics.append(LINK(statTree));
  5286. }
  5287. else
  5288. {
  5289. if (merge)
  5290. {
  5291. unsigned __int64 oldValue = statTree->getPropInt64("@value", 0);
  5292. unsigned __int64 oldCount = statTree->getPropInt64("@count", 0);
  5293. unsigned __int64 oldMax = statTree->getPropInt64("@max", 0);
  5294. statTree->setPropInt64("@value", value + oldValue);
  5295. statTree->setPropInt64("@count", count + oldCount);
  5296. if (maxValue > oldMax)
  5297. statTree->setPropInt64("@max", maxValue);
  5298. }
  5299. else
  5300. {
  5301. statTree->setPropInt64("@value", value);
  5302. statTree->setPropInt64("@count", count);
  5303. if (maxValue)
  5304. statTree->setPropInt64("@max", maxValue);
  5305. }
  5306. }
  5307. }
  5308. void CLocalWorkUnit::setTimeStamp(const char *application, const char *instance, const char *event)
  5309. {
  5310. setTimeStamp(application,instance,event,false);
  5311. }
  5312. void CLocalWorkUnit::addTimeStamp(const char *application, const char *instance, const char *event)
  5313. {
  5314. setTimeStamp(application,instance,event,true);
  5315. }
  5316. IStringVal &CLocalWorkUnit::getTimeStamp(const char *name, const char *application, IStringVal &str) const
  5317. {
  5318. CriticalBlock block(crit);
  5319. str.clear();
  5320. StringBuffer pname("TimeStamps/TimeStamp");
  5321. if (application)
  5322. pname.appendf("[@application=\"%s\"]", application);
  5323. pname.appendf("/%s", name);
  5324. Owned<IPropertyTreeIterator> stamps = p->getElements(pname.str());
  5325. if (stamps && stamps->first())
  5326. str.set(stamps->query().queryProp(NULL));
  5327. return str;
  5328. }
  5329. IConstWUTimeStampIterator& CLocalWorkUnit::getTimeStamps() const
  5330. {
  5331. CriticalBlock block(crit);
  5332. timestamps.load(p,"TimeStamps/*");
  5333. return *new CArrayIteratorOf<IConstWUTimeStamp,IConstWUTimeStampIterator> (timestamps, 0, (IConstWorkUnit *) this);
  5334. }
  5335. IConstWUStatisticIterator& CLocalWorkUnit::getStatistics() const
  5336. {
  5337. CriticalBlock block(crit);
  5338. statistics.load(p,"Statistics/*");
  5339. return *new CArrayIteratorOf<IConstWUStatistic,IConstWUStatisticIterator> (statistics, 0, (IConstWorkUnit *) this);
  5340. }
  5341. IConstWUStatistic * CLocalWorkUnit::getStatisticByDescription(const char * desc) const
  5342. {
  5343. StringBuffer xpath;
  5344. xpath.appendf("Statistics/Statistic[@desc=\"%s\"]", desc);
  5345. CriticalBlock block(crit);
  5346. IPropertyTree * match = p->queryPropTree(xpath);
  5347. if (!match)
  5348. return NULL;
  5349. return new CLocalWUStatistic(LINK(match));
  5350. }
  5351. IConstWUStatistic * CLocalWorkUnit::getStatistic(const char * name) const
  5352. {
  5353. StringBuffer xpath;
  5354. xpath.appendf("Statistics/Statistic[@name=\"%s\"]", name);
  5355. CriticalBlock block(crit);
  5356. IPropertyTree * match = p->queryPropTree(xpath);
  5357. if (!match)
  5358. return NULL;
  5359. return new CLocalWUStatistic(LINK(match));
  5360. }
  5361. bool CLocalWorkUnit::getWuDate(unsigned & year, unsigned & month, unsigned& day)
  5362. {
  5363. CriticalBlock block(crit);
  5364. SCMStringBuffer wuidstr;
  5365. const char *wuid = getWuid(wuidstr).str();
  5366. if (sscanf(wuid, "W%4u%2u%2u", &year, &month, &day)==3)
  5367. {
  5368. }
  5369. return false;
  5370. }
  5371. IWUPlugin* CLocalWorkUnit::updatePluginByName(const char *qname)
  5372. {
  5373. CriticalBlock block(crit);
  5374. IConstWUPlugin *existing = getPluginByName(qname);
  5375. if (existing)
  5376. return (IWUPlugin *) existing;
  5377. if (!plugins.length())
  5378. p->addPropTree("Plugins", createPTree("Plugins"));
  5379. IPropertyTree *pl = p->queryPropTree("Plugins");
  5380. IPropertyTree *s = pl->addPropTree("Plugin", createPTree("Plugin"));
  5381. s->Link();
  5382. IWUPlugin* q = new CLocalWUPlugin(s);
  5383. q->Link();
  5384. plugins.append(*q);
  5385. q->setPluginName(qname);
  5386. return q;
  5387. }
  5388. IConstWUPlugin* CLocalWorkUnit::getPluginByName(const char *qname) const
  5389. {
  5390. CriticalBlock block(crit);
  5391. loadPlugins();
  5392. ForEachItemIn(idx, plugins)
  5393. {
  5394. SCMStringBuffer name;
  5395. IConstWUPlugin &cur = plugins.item(idx);
  5396. cur.getPluginName(name);
  5397. if (stricmp(name.str(), qname)==0)
  5398. {
  5399. cur.Link();
  5400. return &cur;
  5401. }
  5402. }
  5403. return NULL;
  5404. }
  5405. IWULibrary* CLocalWorkUnit::updateLibraryByName(const char *qname)
  5406. {
  5407. CriticalBlock block(crit);
  5408. IConstWULibrary *existing = getLibraryByName(qname);
  5409. if (existing)
  5410. return (IWULibrary *) existing;
  5411. if (!libraries.length())
  5412. p->addPropTree("Libraries", createPTree("Libraries"));
  5413. IPropertyTree *pl = p->queryPropTree("Libraries");
  5414. IPropertyTree *s = pl->addPropTree("Library", createPTree("Library"));
  5415. s->Link();
  5416. IWULibrary* q = new CLocalWULibrary(s);
  5417. q->Link();
  5418. libraries.append(*q);
  5419. q->setName(qname);
  5420. return q;
  5421. }
  5422. void CLocalWorkUnit::loadExceptions() const
  5423. {
  5424. CriticalBlock block(crit);
  5425. if (!exceptionsCached)
  5426. {
  5427. assertex(exceptions.length() == 0);
  5428. Owned<IPropertyTreeIterator> r = p->getElements("Exceptions/Exception");
  5429. for (r->first(); r->isValid(); r->next())
  5430. {
  5431. IPropertyTree *rp = &r->query();
  5432. rp->Link();
  5433. exceptions.append(*new CLocalWUException(rp));
  5434. }
  5435. exceptionsCached = true;
  5436. }
  5437. }
  5438. IConstWUExceptionIterator& CLocalWorkUnit::getExceptions() const
  5439. {
  5440. CriticalBlock block(crit);
  5441. loadExceptions();
  5442. return *new CArrayIteratorOf<IConstWUException,IConstWUExceptionIterator> (exceptions, 0, (IConstWorkUnit *) this);
  5443. }
  5444. unsigned CLocalWorkUnit::getExceptionCount() const
  5445. {
  5446. CriticalBlock block(crit);
  5447. loadExceptions();
  5448. return exceptions.length();
  5449. }
  5450. void CLocalWorkUnit::clearExceptions()
  5451. {
  5452. CriticalBlock block(crit);
  5453. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  5454. exceptions.kill();
  5455. exceptionsCached = true;
  5456. p->removeProp("Exceptions");
  5457. }
  5458. IWUException* CLocalWorkUnit::createException()
  5459. {
  5460. CriticalBlock block(crit);
  5461. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  5462. loadExceptions();
  5463. if (!exceptions.length())
  5464. p->addPropTree("Exceptions", createPTree("Exceptions"));
  5465. IPropertyTree *r = p->queryPropTree("Exceptions");
  5466. IPropertyTree *s = r->addPropTree("Exception", createPTree("Exception"));
  5467. IWUException* q = new CLocalWUException(LINK(s));
  5468. exceptions.append(*LINK(q));
  5469. Owned<IJlibDateTime> now = createDateTimeNow();
  5470. SCMStringBuffer temp;
  5471. now->getString(temp);
  5472. q->setTimeStamp(temp.str());
  5473. return q;
  5474. }
  5475. IConstWUWebServicesInfo* CLocalWorkUnit::getWebServicesInfo() const
  5476. {
  5477. // For this to be legally called, we must have the read-able interface. So we are already locked for (at least) read.
  5478. CriticalBlock block(crit);
  5479. if (!webServicesInfoCached)
  5480. {
  5481. assertex(!webServicesInfo);
  5482. IPropertyTree *s = p->getPropTree("WebServicesInfo");
  5483. if (s)
  5484. webServicesInfo.setown(new CLocalWUWebServicesInfo(s));
  5485. webServicesInfoCached = true;
  5486. }
  5487. return webServicesInfo.getLink();
  5488. }
  5489. IWUWebServicesInfo* CLocalWorkUnit::updateWebServicesInfo(bool create)
  5490. {
  5491. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  5492. CriticalBlock block(crit);
  5493. if (!webServicesInfoCached)
  5494. {
  5495. IPropertyTree *s = p->queryPropTree("WebServicesInfo");
  5496. if (!s)
  5497. {
  5498. if (create)
  5499. s = p->addPropTree("WebServicesInfo", createPTreeFromXMLString("<WebServicesInfo />"));
  5500. else
  5501. return NULL;
  5502. }
  5503. s->Link();
  5504. webServicesInfo.setown(new CLocalWUWebServicesInfo(s));
  5505. webServicesInfoCached = true;
  5506. }
  5507. return webServicesInfo.getLink();
  5508. }
  5509. IConstWURoxieQueryInfo* CLocalWorkUnit::getRoxieQueryInfo() const
  5510. {
  5511. // For this to be legally called, we must have the read-able interface. So we are already locked for (at least) read.
  5512. CriticalBlock block(crit);
  5513. if (!roxieQueryInfoCached)
  5514. {
  5515. assertex(!roxieQueryInfo);
  5516. IPropertyTree *s = p->getPropTree("RoxieQueryInfo");
  5517. if (s)
  5518. roxieQueryInfo.setown(new CLocalWURoxieQueryInfo(s));
  5519. roxieQueryInfoCached = true;
  5520. }
  5521. return roxieQueryInfo.getLink();
  5522. }
  5523. IWURoxieQueryInfo* CLocalWorkUnit::updateRoxieQueryInfo(const char *wuid, const char *roxieClusterName)
  5524. {
  5525. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  5526. CriticalBlock block(crit);
  5527. if (!roxieQueryInfoCached)
  5528. {
  5529. IPropertyTree *s = p->queryPropTree("RoxieQueryInfo");
  5530. if (!s)
  5531. s = p->addPropTree("RoxieQueryInfo", createPTreeFromXMLString("<RoxieQueryInfo />"));
  5532. if (wuid && *wuid)
  5533. s->addProp("@wuid", wuid);
  5534. if (roxieClusterName && *roxieClusterName)
  5535. s->addProp("@roxieClusterName", roxieClusterName);
  5536. s->Link();
  5537. roxieQueryInfo.setown(new CLocalWURoxieQueryInfo(s));
  5538. roxieQueryInfoCached = true;
  5539. }
  5540. return roxieQueryInfo.getLink();
  5541. }
  5542. static int compareResults(IInterface **ll, IInterface **rr)
  5543. {
  5544. CLocalWUResult *l = (CLocalWUResult *) *ll;
  5545. CLocalWUResult *r = (CLocalWUResult *) *rr;
  5546. return l->getResultSequence() - r->getResultSequence();
  5547. }
  5548. void CLocalWorkUnit::loadResults() const
  5549. {
  5550. CriticalBlock block(crit);
  5551. if (!resultsCached)
  5552. {
  5553. assertex(results.length() == 0);
  5554. Owned<IPropertyTreeIterator> r = p->getElements("Results/Result");
  5555. for (r->first(); r->isValid(); r->next())
  5556. {
  5557. IPropertyTree *rp = &r->query();
  5558. rp->Link();
  5559. results.append(*new CLocalWUResult(rp));
  5560. }
  5561. results.sort(compareResults);
  5562. resultsCached = true;
  5563. }
  5564. }
  5565. void CLocalWorkUnit::loadVariables() const
  5566. {
  5567. CriticalBlock block(crit);
  5568. if (!variablesCached)
  5569. {
  5570. assertex(variables.length() == 0);
  5571. Owned<IPropertyTreeIterator> r = p->getElements("Variables/Variable");
  5572. for (r->first(); r->isValid(); r->next())
  5573. {
  5574. IPropertyTree *rp = &r->query();
  5575. rp->Link();
  5576. variables.append(*new CLocalWUResult(rp));
  5577. }
  5578. variablesCached = true;
  5579. }
  5580. }
  5581. void CLocalWorkUnit::loadTemporaries() const
  5582. {
  5583. CriticalBlock block(crit);
  5584. if (!temporariesCached)
  5585. {
  5586. assertex(temporaries.length() == 0);
  5587. Owned<IPropertyTreeIterator> r = p->getElements("Temporaries/Variable");
  5588. for (r->first(); r->isValid(); r->next())
  5589. {
  5590. IPropertyTree *rp = &r->query();
  5591. rp->Link();
  5592. temporaries.append(*new CLocalWUResult(rp));
  5593. }
  5594. temporariesCached = true;
  5595. }
  5596. }
  5597. void CLocalWorkUnit::deleteTemporaries()
  5598. {
  5599. CriticalBlock block(crit);
  5600. if (temporariesCached)
  5601. {
  5602. temporaries.kill();
  5603. temporariesCached = false;
  5604. }
  5605. p->removeProp("Temporaries");
  5606. }
  5607. IWUResult* CLocalWorkUnit::createResult()
  5608. {
  5609. CriticalBlock block(crit);
  5610. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  5611. loadResults();
  5612. if (!results.length())
  5613. p->addPropTree("Results", createPTree("Results"));
  5614. IPropertyTree *r = p->queryPropTree("Results");
  5615. IPropertyTree *s = r->addPropTree("Result", createPTree());
  5616. s->Link();
  5617. IWUResult* q = new CLocalWUResult(s);
  5618. q->Link();
  5619. results.append(*q);
  5620. return q;
  5621. }
  5622. IWUResult* CLocalWorkUnit::updateResultByName(const char *qname)
  5623. {
  5624. CriticalBlock block(crit);
  5625. IConstWUResult *existing = getResultByName(qname);
  5626. if (existing)
  5627. return (IWUResult *) existing;
  5628. IWUResult* q = createResult();
  5629. q->setResultName(qname);
  5630. return q;
  5631. }
  5632. IWUResult* CLocalWorkUnit::updateResultBySequence(unsigned seq)
  5633. {
  5634. CriticalBlock block(crit);
  5635. IConstWUResult *existing = getResultBySequence(seq);
  5636. if (existing)
  5637. return (IWUResult *) existing;
  5638. IWUResult* q = createResult();
  5639. q->setResultSequence(seq);
  5640. return q;
  5641. }
  5642. IConstWUResultIterator& CLocalWorkUnit::getResults() const
  5643. {
  5644. CriticalBlock block(crit);
  5645. loadResults();
  5646. return *new CArrayIteratorOf<IConstWUResult,IConstWUResultIterator> (results, 0, (IConstWorkUnit *) this);
  5647. }
  5648. IConstWUResult* CLocalWorkUnit::getResultByName(const char *qname) const
  5649. {
  5650. CriticalBlock block(crit);
  5651. loadResults();
  5652. ForEachItemIn(idx, results)
  5653. {
  5654. SCMStringBuffer name;
  5655. IConstWUResult &cur = results.item(idx);
  5656. cur.getResultName(name);
  5657. if (stricmp(name.str(), qname)==0)
  5658. {
  5659. cur.Link();
  5660. return &cur;
  5661. }
  5662. }
  5663. return NULL;
  5664. }
  5665. IConstWUResult* CLocalWorkUnit::getResultBySequence(unsigned seq) const
  5666. {
  5667. CriticalBlock block(crit);
  5668. loadResults();
  5669. ForEachItemIn(idx, results)
  5670. {
  5671. IConstWUResult &cur = results.item(idx);
  5672. if (cur.getResultSequence() == seq)
  5673. {
  5674. cur.Link();
  5675. return &cur;
  5676. }
  5677. }
  5678. return NULL;
  5679. }
  5680. IConstWUResultIterator& CLocalWorkUnit::getVariables() const
  5681. {
  5682. CriticalBlock block(crit);
  5683. loadVariables();
  5684. return *new CArrayIteratorOf<IConstWUResult,IConstWUResultIterator> (variables, 0, (IConstWorkUnit *) this);
  5685. }
  5686. IConstWUResult* CLocalWorkUnit::getGlobalByName(const char *qname) const
  5687. {
  5688. CriticalBlock block(crit);
  5689. if (strcmp(p->queryName(), GLOBAL_WORKUNIT)==0)
  5690. return getVariableByName(qname);
  5691. Owned <IWorkUnit> global = factory->ensureNamedWorkUnit(GLOBAL_WORKUNIT);
  5692. return global->getVariableByName(qname);
  5693. }
  5694. IWUResult* CLocalWorkUnit::updateGlobalByName(const char *qname)
  5695. {
  5696. CriticalBlock block(crit);
  5697. if (strcmp(p->queryName(), GLOBAL_WORKUNIT)==0)
  5698. return updateVariableByName(qname);
  5699. Owned <IWorkUnit> global = factory->ensureNamedWorkUnit(GLOBAL_WORKUNIT);
  5700. return global->updateVariableByName(qname);
  5701. }
  5702. IConstWUResult* CLocalWorkUnit::getVariableByName(const char *qname) const
  5703. {
  5704. CriticalBlock block(crit);
  5705. loadVariables();
  5706. ForEachItemIn(idx, variables)
  5707. {
  5708. SCMStringBuffer name;
  5709. IConstWUResult &cur = variables.item(idx);
  5710. cur.getResultName(name);
  5711. if (stricmp(name.str(), qname)==0)
  5712. {
  5713. cur.Link();
  5714. return &cur;
  5715. }
  5716. }
  5717. return NULL;
  5718. }
  5719. IConstWUResult* CLocalWorkUnit::getTemporaryByName(const char *qname) const
  5720. {
  5721. CriticalBlock block(crit);
  5722. loadTemporaries();
  5723. ForEachItemIn(idx, temporaries)
  5724. {
  5725. SCMStringBuffer name;
  5726. IConstWUResult &cur = temporaries.item(idx);
  5727. cur.getResultName(name);
  5728. if (stricmp(name.str(), qname)==0)
  5729. {
  5730. cur.Link();
  5731. return &cur;
  5732. }
  5733. }
  5734. return NULL;
  5735. }
  5736. IConstWUResultIterator& CLocalWorkUnit::getTemporaries() const
  5737. {
  5738. CriticalBlock block(crit);
  5739. loadTemporaries();
  5740. return *new CArrayIteratorOf<IConstWUResult,IConstWUResultIterator> (temporaries, 0, (IConstWorkUnit *) this);
  5741. }
  5742. IWUResult* CLocalWorkUnit::updateTemporaryByName(const char *qname)
  5743. {
  5744. CriticalBlock block(crit);
  5745. IConstWUResult *existing = getTemporaryByName(qname);
  5746. if (existing)
  5747. return (IWUResult *) existing;
  5748. if (!temporaries.length())
  5749. p->addPropTree("Temporaries", createPTree("Temporaries"));
  5750. IPropertyTree *vars = p->queryPropTree("Temporaries");
  5751. IPropertyTree *s = vars->addPropTree("Variable", createPTree("Variable"));
  5752. s->Link();
  5753. IWUResult* q = new CLocalWUResult(s);
  5754. q->Link();
  5755. temporaries.append(*q);
  5756. q->setResultName(qname);
  5757. return q;
  5758. }
  5759. IWUResult* CLocalWorkUnit::updateVariableByName(const char *qname)
  5760. {
  5761. CriticalBlock block(crit);
  5762. IConstWUResult *existing = getVariableByName(qname);
  5763. if (existing)
  5764. return (IWUResult *) existing;
  5765. if (!variables.length())
  5766. p->addPropTree("Variables", createPTree("Variables"));
  5767. IPropertyTree *vars = p->queryPropTree("Variables");
  5768. IPropertyTree *s = vars->addPropTree("Variable", createPTree("Variable"));
  5769. s->Link();
  5770. IWUResult* q = new CLocalWUResult(s);
  5771. q->Link();
  5772. variables.append(*q);
  5773. q->setResultName(qname);
  5774. return q;
  5775. }
  5776. void CLocalWorkUnit::deleteTempFiles(const char *graph, bool deleteOwned, bool deleteJobOwned)
  5777. {
  5778. CriticalBlock block(crit);
  5779. IPropertyTree *files = p->queryPropTree("Files");
  5780. if (!files) return;
  5781. Owned<IPropertyTreeIterator> iter = files->getElements("File");
  5782. ICopyArrayOf<IPropertyTree> toRemove;
  5783. ForEach (*iter)
  5784. {
  5785. IPropertyTree &file = iter->query();
  5786. WUFileKind fileKind = (WUFileKind) file.getPropInt("@kind", WUFileStandard);
  5787. if(file.getPropBool("@temporary")) fileKind = WUFileTemporary; // @temporary, legacy check
  5788. bool needDelete;
  5789. switch(fileKind)
  5790. {
  5791. case WUFileTemporary:
  5792. if(graph==NULL)
  5793. needDelete = true;
  5794. else
  5795. {
  5796. const char *graphOwner = file.queryProp("@graph");
  5797. needDelete = ((graphOwner==NULL) || (strcmp(graph, graphOwner)==0));
  5798. }
  5799. break;
  5800. case WUFileJobOwned:
  5801. needDelete = ((graph==NULL) && deleteJobOwned);
  5802. break;
  5803. case WUFileOwned:
  5804. needDelete = ((graph==NULL) && deleteOwned);
  5805. break;
  5806. default:
  5807. needDelete = false;
  5808. }
  5809. if(needDelete)
  5810. {
  5811. const char *name = file.queryProp("@name");
  5812. LOG(MCdebugProgress, unknownJob, "Removing workunit file %s from DFS", name);
  5813. queryDistributedFileDirectory().removeEntry(name, queryUserDescriptor());
  5814. toRemove.append(file);
  5815. }
  5816. }
  5817. ForEachItemIn(r, toRemove) files->removeTree(&toRemove.item(r));
  5818. }
  5819. static void _noteFileRead(IDistributedFile *file, IPropertyTree *filesRead)
  5820. {
  5821. IDistributedSuperFile *super = file->querySuperFile();
  5822. StringBuffer fname;
  5823. file->getLogicalName(fname);
  5824. StringBuffer path("File[@name=\"");
  5825. path.append(fname).append("\"]");
  5826. IPropertyTree *fileTree = filesRead->queryPropTree(path.str());
  5827. if (fileTree)
  5828. fileTree->setPropInt("@useCount", fileTree->getPropInt("@useCount")+1);
  5829. else
  5830. {
  5831. StringBuffer cluster;
  5832. file->getClusterName(0,cluster);
  5833. fileTree = createPTree();
  5834. fileTree->setProp("@name", fname.str());
  5835. fileTree->setProp("@cluster", cluster.str());
  5836. fileTree->setPropInt("@useCount", 1);
  5837. fileTree = filesRead->addPropTree("File", fileTree);
  5838. }
  5839. if (super)
  5840. {
  5841. Owned<IDistributedFileIterator> iter = super->getSubFileIterator(false);
  5842. ForEach (*iter)
  5843. {
  5844. IDistributedFile &file = iter->query();
  5845. StringBuffer fname;
  5846. file.getLogicalName(fname);
  5847. Owned<IPropertyTree> subfile = createPTree();
  5848. subfile->setProp("@name", fname.str());
  5849. fileTree->addPropTree("Subfile", subfile.getClear());
  5850. _noteFileRead(&file, filesRead);
  5851. }
  5852. }
  5853. }
  5854. void CLocalWorkUnit::noteFileRead(IDistributedFile *file)
  5855. {
  5856. CriticalBlock block(crit);
  5857. IPropertyTree *files = p->queryPropTree("FilesRead");
  5858. if (!files)
  5859. files = p->addPropTree("FilesRead", createPTree());
  5860. _noteFileRead(file, files);
  5861. }
  5862. static void addFile(IPropertyTree *files, const char *fileName, const char *cluster, unsigned usageCount, WUFileKind fileKind, const char *graphOwner)
  5863. {
  5864. StringBuffer path("File[@name=\"");
  5865. path.append(fileName).append("\"]");
  5866. if (cluster)
  5867. path.append("[@cluster=\"").append(cluster).append("\"]");
  5868. IPropertyTree *file = files->queryPropTree(path.str());
  5869. if (file) files->removeTree(file);
  5870. file = createPTree();
  5871. file->setProp("@name", fileName);
  5872. if (cluster)
  5873. file->setProp("@cluster", cluster);
  5874. if (graphOwner)
  5875. file->setProp("@graph", graphOwner);
  5876. file->setPropInt("@kind", (unsigned)fileKind);
  5877. if (WUFileTemporary == fileKind)
  5878. file->setPropInt("@usageCount", usageCount);
  5879. files->addPropTree("File", file);
  5880. }
  5881. void CLocalWorkUnit::addFile(const char *fileName, StringArray *clusters, unsigned usageCount, WUFileKind fileKind, const char *graphOwner)
  5882. {
  5883. CriticalBlock block(crit);
  5884. IPropertyTree *files = p->queryPropTree("Files");
  5885. if (!files)
  5886. files = p->addPropTree("Files", createPTree());
  5887. if (!clusters)
  5888. addFile(fileName, NULL, usageCount, fileKind, graphOwner);
  5889. else
  5890. {
  5891. ForEachItemIn(c, *clusters)
  5892. ::addFile(files, fileName, clusters->item(c), usageCount, fileKind, graphOwner);
  5893. }
  5894. }
  5895. void CLocalWorkUnit::releaseFile(const char *fileName)
  5896. {
  5897. StringBuffer path("File[@name=\"");
  5898. path.append(fileName).append("\"]");
  5899. CriticalBlock block(crit);
  5900. IPropertyTree *files = p->queryPropTree("Files");
  5901. if (!files) return;
  5902. Owned<IPropertyTreeIterator> fiter = files->getElements(path.str());
  5903. ForEach (*fiter)
  5904. {
  5905. IPropertyTree *file = &fiter->query();
  5906. unsigned usageCount = file->getPropInt("@usageCount");
  5907. if (usageCount > 1)
  5908. file->setPropInt("@usageCount", usageCount-1);
  5909. else
  5910. {
  5911. StringAttr name(file->queryProp("@name"));
  5912. files->removeTree(file);
  5913. if (!name.isEmpty()&&(1 == usageCount))
  5914. {
  5915. if (queryDistributedFileDirectory().removeEntry(fileName, queryUserDescriptor()))
  5916. LOG(MCdebugProgress, unknownJob, "Removed (released) file %s from DFS", name.get());
  5917. }
  5918. }
  5919. }
  5920. }
  5921. void CLocalWorkUnit::clearGraphProgress()
  5922. {
  5923. CConstGraphProgress::deleteWuidProgress(p->queryName());
  5924. }
  5925. void CLocalWorkUnit::resetBeforeGeneration()
  5926. {
  5927. CriticalBlock block(crit);
  5928. //Remove all associated files
  5929. Owned<IWUQuery> q = updateQuery();
  5930. q->removeAssociatedFiles();
  5931. //Remove any pre-existing workflow information
  5932. workflowIterator.clear();
  5933. p->removeProp("Workflow");
  5934. }
  5935. unsigned CLocalWorkUnit::queryFileUsage(const char *fileName) const
  5936. {
  5937. StringBuffer path("Files/File[@name=\"");
  5938. path.append(fileName).append("\"]/@usageCount");
  5939. CriticalBlock block(crit);
  5940. return p->getPropInt(path.str());
  5941. }
  5942. IPropertyTree *CLocalWorkUnit::getDiskUsageStats()
  5943. {
  5944. return p->getPropTree("DiskUsageStats");
  5945. }
  5946. void CLocalWorkUnit::addDiskUsageStats(__int64 _avgNodeUsage, unsigned _minNode, __int64 _minNodeUsage, unsigned _maxNode, __int64 _maxNodeUsage, __int64 _graphId)
  5947. {
  5948. IPropertyTree *stats = p->queryPropTree("DiskUsageStats");
  5949. offset_t maxNodeUsage;
  5950. if (stats)
  5951. maxNodeUsage = stats->getPropInt64("@maxNodeUsage");
  5952. else
  5953. {
  5954. stats = p->addPropTree("DiskUsageStats", createPTree());
  5955. maxNodeUsage = 0;
  5956. }
  5957. if ((offset_t)_maxNodeUsage > maxNodeUsage)
  5958. {
  5959. // record all details at time of max node usage.
  5960. stats->setPropInt("@minNode", _minNode);
  5961. stats->setPropInt("@maxNode", _maxNode);
  5962. stats->setPropInt64("@minNodeUsage", _minNodeUsage);
  5963. stats->setPropInt64("@maxNodeUsage", _maxNodeUsage);
  5964. stats->setPropInt64("@graphId", _graphId);
  5965. if (_avgNodeUsage)
  5966. {
  5967. unsigned _skewHi = (unsigned)((100 * (_maxNodeUsage-_avgNodeUsage))/_avgNodeUsage);
  5968. unsigned _skewLo = (unsigned)((100 * (_avgNodeUsage-_minNodeUsage))/_avgNodeUsage);
  5969. stats->setPropInt("@skewHi", _skewHi);
  5970. stats->setPropInt("@skewLo", _skewLo);
  5971. }
  5972. }
  5973. }
  5974. IPropertyTreeIterator & CLocalWorkUnit::getFileIterator() const
  5975. {
  5976. CriticalBlock block(crit);
  5977. return * p->getElements("Files/File");
  5978. }
  5979. IPropertyTreeIterator & CLocalWorkUnit::getFilesReadIterator() const
  5980. {
  5981. CriticalBlock block(crit);
  5982. return * p->getElements("FilesRead/File");
  5983. }
  5984. //=================================================================================================
  5985. IWUActivity * CLocalWorkUnit::updateActivity(__int64 id)
  5986. {
  5987. CriticalBlock block(crit);
  5988. IConstWUActivity *existing = getActivity(id);
  5989. if (existing)
  5990. return (IWUActivity *) existing;
  5991. if (!activities.length())
  5992. p->addPropTree("Activities", createPTree("Activities"));
  5993. IPropertyTree *pl = p->queryPropTree("Activities");
  5994. IPropertyTree *s = pl->addPropTree("Activity", createPTree("Activity"));
  5995. IWUActivity * q = new CLocalWUActivity(LINK(s), id);
  5996. activities.append(*LINK(q));
  5997. return q;
  5998. }
  5999. IConstWUActivity * CLocalWorkUnit::getActivity(__int64 id) const
  6000. {
  6001. CriticalBlock block(crit);
  6002. loadActivities();
  6003. ForEachItemIn(idx, activities)
  6004. {
  6005. IConstWUActivity &cur = activities.item(idx);
  6006. if (cur.getId() == id)
  6007. return &OLINK(cur);
  6008. }
  6009. return NULL;
  6010. }
  6011. void CLocalWorkUnit::loadActivities() const
  6012. {
  6013. CriticalBlock block(crit);
  6014. if (!activitiesCached)
  6015. {
  6016. assertex(activities.length() == 0);
  6017. Owned<IPropertyTreeIterator> r = p->getElements("Activities/Activity");
  6018. for (r->first(); r->isValid(); r->next())
  6019. {
  6020. IPropertyTree *rp = &r->query();
  6021. rp->Link();
  6022. activities.append(*new CLocalWUActivity(rp));
  6023. }
  6024. activitiesCached = true;
  6025. }
  6026. }
  6027. IConstWUActivityIterator& CLocalWorkUnit::getActivities() const
  6028. {
  6029. CriticalBlock block(crit);
  6030. loadActivities();
  6031. return *new CArrayIteratorOf<IConstWUActivity,IConstWUActivityIterator> (activities, 0, (IConstWorkUnit *) this);
  6032. }
  6033. //=================================================================================================
  6034. bool CLocalWorkUnit::switchThorQueue(const char *cluster, IQueueSwitcher *qs)
  6035. {
  6036. CriticalBlock block(crit);
  6037. if (qs->isAuto()&&!getAllowAutoQueueSwitch())
  6038. return false;
  6039. Owned<IConstWUClusterInfo> newci = getTargetClusterInfo(cluster);
  6040. if (!newci)
  6041. return false;
  6042. StringBuffer currentcluster;
  6043. if (!p->getProp("@clusterName",currentcluster))
  6044. return false;
  6045. Owned<IConstWUClusterInfo> curci = getTargetClusterInfo(currentcluster.str());
  6046. if (!curci)
  6047. return false;
  6048. SCMStringBuffer curqname;
  6049. curci->getThorQueue(curqname);
  6050. const char *wuid = p->queryName();
  6051. void *qi = qs->getQ(curqname.str(),wuid);
  6052. if (!qi)
  6053. return false;
  6054. setClusterName(cluster);
  6055. SCMStringBuffer newqname;
  6056. newci->getThorQueue(newqname);
  6057. qs->putQ(newqname.str(),wuid,qi);
  6058. return true;
  6059. }
  6060. //=================================================================================================
  6061. IPropertyTree *CLocalWorkUnit::getUnpackedTree(bool includeProgress) const
  6062. {
  6063. Owned<IPropertyTree> ret = createPTreeFromIPT(p);
  6064. Owned<IConstWUGraphIterator> graphIter = &getGraphs(GraphTypeAny);
  6065. ForEach(*graphIter)
  6066. {
  6067. IConstWUGraph &graph = graphIter->query();
  6068. Owned<IPropertyTree> graphTree = graph.getXGMMLTree(includeProgress);
  6069. SCMStringBuffer gName;
  6070. graph.getName(gName);
  6071. StringBuffer xpath("Graphs/Graph[@name=\"");
  6072. xpath.append(gName.s).append("\"]/xgmml");
  6073. IPropertyTree *xgmml = ret->queryPropTree(xpath.str());
  6074. if (xgmml) // don't know of any reason it shouldn't exist
  6075. {
  6076. xgmml->removeProp("graphBin");
  6077. xgmml->setPropTree("graph", graphTree.getClear());
  6078. }
  6079. }
  6080. return ret.getClear();
  6081. }
  6082. void CLocalWorkUnit::loadGraphs() const
  6083. {
  6084. CriticalBlock block(crit);
  6085. if (!cachedGraphs.get())
  6086. {
  6087. MemoryBuffer buf;
  6088. IPropertyTree *t = p->queryPropTree("PackedGraphs");
  6089. if (t&&t->getPropBin(NULL,buf)) {
  6090. cachedGraphs.setown(createPTree(buf));
  6091. }
  6092. else
  6093. cachedGraphs.set(p->queryPropTree("Graphs"));
  6094. if (cachedGraphs.get())
  6095. {
  6096. Owned<IPropertyTreeIterator> iter = cachedGraphs->getElements("Graph");
  6097. ForEach(*iter)
  6098. {
  6099. IPropertyTree &graph = iter->query();
  6100. graphs.append(*new CLocalWUGraph(*this, LINK(&graph)));
  6101. }
  6102. }
  6103. }
  6104. }
  6105. mapEnums graphTypes[] = {
  6106. { GraphTypeAny, "unknown" },
  6107. { GraphTypeProgress, "progress" },
  6108. { GraphTypeEcl, "ECL" },
  6109. { GraphTypeActivities, "activities" },
  6110. { GraphTypeSubProgress, "subgraph" },
  6111. { GraphTypeSize, NULL },
  6112. };
  6113. CLocalWUGraph::CLocalWUGraph(const CLocalWorkUnit &_owner, IPropertyTree *props) : p(props), owner(_owner)
  6114. {
  6115. SCMStringBuffer str;
  6116. owner.getWuid(str);
  6117. wuid.set(str.s.str());
  6118. wuidVersion = owner.getWuidVersion();
  6119. }
  6120. IStringVal& CLocalWUGraph::getName(IStringVal &str) const
  6121. {
  6122. str.set(p->queryProp("@name"));
  6123. return str;
  6124. }
  6125. IStringVal& CLocalWUGraph::getLabel(IStringVal &str) const
  6126. {
  6127. if (wuidVersion >= 2)
  6128. {
  6129. str.set(p->queryProp("@label"));
  6130. return str;
  6131. }
  6132. else
  6133. {
  6134. Owned<IPropertyTree> xgmml = getXGMMLTree(false);
  6135. str.set(xgmml->queryProp("@label"));
  6136. return str;
  6137. }
  6138. }
  6139. IStringVal& CLocalWUGraph::getXGMML(IStringVal &str, bool mergeProgress) const
  6140. {
  6141. Owned<IPropertyTree> xgmml = getXGMMLTree(mergeProgress);
  6142. if (xgmml)
  6143. {
  6144. StringBuffer x;
  6145. toXML(xgmml, x);
  6146. str.set(x.str());
  6147. }
  6148. return str;
  6149. }
  6150. unsigned CLocalWorkUnit::getGraphCount() const
  6151. {
  6152. CriticalBlock block(crit);
  6153. if (p->hasProp("Graphs"))
  6154. {
  6155. return p->queryPropTree("Graphs")->numChildren();
  6156. }
  6157. return 0;
  6158. }
  6159. unsigned CLocalWorkUnit::getSourceFileCount() const
  6160. {
  6161. CriticalBlock block(crit);
  6162. if (p->hasProp("FilesRead"))
  6163. {
  6164. return p->queryPropTree("FilesRead")->numChildren();
  6165. }
  6166. return 0;
  6167. }
  6168. unsigned CLocalWorkUnit::getResultCount() const
  6169. {
  6170. CriticalBlock block(crit);
  6171. if (p->hasProp("Results"))
  6172. {
  6173. return p->queryPropTree("Results")->numChildren();
  6174. }
  6175. return 0;
  6176. }
  6177. unsigned CLocalWorkUnit::getVariableCount() const
  6178. {
  6179. CriticalBlock block(crit);
  6180. if (p->hasProp("Variables"))
  6181. {
  6182. return p->queryPropTree("Variables")->numChildren();
  6183. }
  6184. return 0;
  6185. }
  6186. unsigned CLocalWorkUnit::getTimerCount() const
  6187. {
  6188. CriticalBlock block(crit);
  6189. if (p->hasProp("Statistics"))
  6190. {
  6191. Owned<IPropertyTreeIterator> iter = p->getElements("Statistics/Statistic[@unit=\"ns\"]");
  6192. unsigned cnt =0;
  6193. if (iter)
  6194. {
  6195. ForEach(*iter)
  6196. cnt++;
  6197. }
  6198. return cnt;
  6199. }
  6200. if (p->hasProp("Timings"))
  6201. return p->queryPropTree("Timings")->numChildren();
  6202. return 0;
  6203. }
  6204. unsigned CLocalWorkUnit::getApplicationValueCount() const
  6205. {
  6206. CriticalBlock block(crit);
  6207. if (p->hasProp("Application"))
  6208. {
  6209. return p->queryPropTree("Application")->numChildren();
  6210. }
  6211. return 0;
  6212. }
  6213. IStringVal &CLocalWorkUnit::getXmlParams(IStringVal &str) const
  6214. {
  6215. CriticalBlock block(crit);
  6216. IPropertyTree *paramTree = p->queryPropTree("Parameters");
  6217. if (paramTree)
  6218. {
  6219. StringBuffer temp;
  6220. toXML(paramTree, temp);
  6221. str.set(temp.str());
  6222. }
  6223. return str;
  6224. }
  6225. const IPropertyTree *CLocalWorkUnit::getXmlParams() const
  6226. {
  6227. CriticalBlock block(crit);
  6228. return p->getPropTree("Parameters");
  6229. }
  6230. void CLocalWorkUnit::setXmlParams(const char *params)
  6231. {
  6232. CriticalBlock block(crit);
  6233. p->setPropTree("Parameters", createPTreeFromXMLString(params));
  6234. }
  6235. void CLocalWorkUnit::setXmlParams(IPropertyTree *tree)
  6236. {
  6237. CriticalBlock block(crit);
  6238. p->setPropTree("Parameters", tree);
  6239. }
  6240. unsigned __int64 CLocalWorkUnit::getHash() const
  6241. {
  6242. CriticalBlock block(crit);
  6243. return p->getPropInt64("@hash");
  6244. }
  6245. void CLocalWorkUnit::setHash(unsigned __int64 hash)
  6246. {
  6247. CriticalBlock block(crit);
  6248. p->setPropInt64("@hash", hash);
  6249. }
  6250. IConstWUGraphIterator& CLocalWorkUnit::getGraphs(WUGraphType type) const
  6251. {
  6252. CriticalBlock block(crit);
  6253. loadGraphs();
  6254. IConstWUGraphIterator *giter = new CArrayIteratorOf<IConstWUGraph,IConstWUGraphIterator> (graphs, 0, (IConstWorkUnit *) this);
  6255. if (type!=GraphTypeAny) {
  6256. class CConstWUGraphIterator: public CInterface, implements IConstWUGraphIterator
  6257. {
  6258. WUGraphType type;
  6259. Owned<IConstWUGraphIterator> base;
  6260. bool match()
  6261. {
  6262. return base->query().getType()==type;
  6263. }
  6264. public:
  6265. IMPLEMENT_IINTERFACE;
  6266. CConstWUGraphIterator(IConstWUGraphIterator *_base,WUGraphType _type)
  6267. : base(_base)
  6268. {
  6269. type = _type;
  6270. }
  6271. bool first()
  6272. {
  6273. if (!base->first())
  6274. return false;
  6275. if (match())
  6276. return true;
  6277. return next();
  6278. }
  6279. bool next()
  6280. {
  6281. while (base->next())
  6282. if (match())
  6283. return true;
  6284. return false;
  6285. }
  6286. virtual bool isValid()
  6287. {
  6288. return base->isValid();
  6289. }
  6290. IConstWUGraph & query()
  6291. {
  6292. return base->query();
  6293. }
  6294. };
  6295. giter = new CConstWUGraphIterator(giter,type);
  6296. }
  6297. return *giter;
  6298. }
  6299. IConstWUGraph* CLocalWorkUnit::getGraph(const char *qname) const
  6300. {
  6301. CriticalBlock block(crit);
  6302. loadGraphs();
  6303. ForEachItemIn(idx, graphs)
  6304. {
  6305. SCMStringBuffer name;
  6306. IConstWUGraph &cur = graphs.item(idx);
  6307. cur.getName(name);
  6308. if (stricmp(name.str(), qname)==0)
  6309. {
  6310. cur.Link();
  6311. return &cur;
  6312. }
  6313. }
  6314. return NULL;
  6315. }
  6316. IWUGraph* CLocalWorkUnit::createGraph()
  6317. {
  6318. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  6319. CriticalBlock block(crit);
  6320. ensureGraphsUnpacked();
  6321. loadGraphs();
  6322. if (!graphs.length())
  6323. p->addPropTree("Graphs", createPTree("Graphs"));
  6324. IPropertyTree *r = p->queryPropTree("Graphs");
  6325. IPropertyTree *s = r->addPropTree("Graph", createPTree());
  6326. s->Link();
  6327. IWUGraph* q = new CLocalWUGraph(*this, s);
  6328. q->Link();
  6329. graphs.append(*q);
  6330. return q;
  6331. }
  6332. IWUGraph * CLocalWorkUnit::updateGraph(const char * name)
  6333. {
  6334. CriticalBlock block(crit);
  6335. ensureGraphsUnpacked();
  6336. IConstWUGraph *existing = getGraph(name);
  6337. if (existing)
  6338. return (IWUGraph *) existing;
  6339. IWUGraph * q = createGraph();
  6340. q->setName(name);
  6341. return q;
  6342. }
  6343. IConstWUGraphProgress *CLocalWorkUnit::getGraphProgress(const char *name) const
  6344. {
  6345. CriticalBlock block(crit);
  6346. return new CConstGraphProgress(p->queryName(), name);
  6347. }
  6348. IStringVal& CLocalWUGraph::getDOT(IStringVal &str) const
  6349. {
  6350. UNIMPLEMENTED;
  6351. }
  6352. void CLocalWUGraph::setName(const char *str)
  6353. {
  6354. p->setProp("@name", str);
  6355. progress.clear();
  6356. progress.setown(new CConstGraphProgress(wuid, str));
  6357. }
  6358. void CLocalWUGraph::setLabel(const char *str)
  6359. {
  6360. p->setProp("@label", str);
  6361. }
  6362. void CLocalWUGraph::setXGMML(const char *str)
  6363. {
  6364. setXGMMLTree(createPTreeFromXMLString(str));
  6365. }
  6366. void CLocalWUGraph::setXGMMLTree(IPropertyTree *_graph, bool compress)
  6367. {
  6368. assertex(strcmp(_graph->queryName(), "graph")==0);
  6369. IPropertyTree *xgmml = p->setPropTree("xgmml", createPTree());
  6370. if (compress)
  6371. {
  6372. MemoryBuffer mb;
  6373. _graph->serialize(mb);
  6374. xgmml->setPropBin("graphBin", mb.length(), mb.toByteArray());
  6375. graph.setown(_graph);
  6376. }
  6377. else
  6378. xgmml->setPropTree("graph", _graph);
  6379. }
  6380. void CLocalWUGraph::mergeProgress(IPropertyTree &rootNode, IPropertyTree &progressTree, const unsigned &progressV) const
  6381. {
  6382. IPropertyTree *graphNode = rootNode.queryPropTree("att/graph");
  6383. if (!graphNode) return;
  6384. unsigned nodeId = rootNode.getPropInt("@id");
  6385. StringBuffer progressNodePath("node[@id=\"");
  6386. progressNodePath.append(nodeId).append("\"]");
  6387. IPropertyTree *progressNode = progressTree.queryPropTree(progressNodePath.str());
  6388. if (progressNode)
  6389. {
  6390. Owned<IPropertyTreeIterator> edges = progressNode->getElements("edge");
  6391. ForEach (*edges)
  6392. {
  6393. IPropertyTree &edge = edges->query();
  6394. StringBuffer edgePath("edge[@id=\"");
  6395. edgePath.append(edge.queryProp("@id")).append("\"]");
  6396. IPropertyTree *graphEdge = graphNode->queryPropTree(edgePath.str());
  6397. if (graphEdge)
  6398. {
  6399. if (progressV < 1)
  6400. mergePTree(graphEdge, &edge);
  6401. else
  6402. { // must translate to XGMML format
  6403. Owned<IAttributeIterator> aIter = edge.getAttributes();
  6404. ForEach (*aIter)
  6405. {
  6406. const char *aName = aIter->queryName()+1;
  6407. if (0 != stricmp("id", aName)) // "id" reserved.
  6408. {
  6409. IPropertyTree *att = graphEdge->addPropTree("att", createPTree());
  6410. att->setProp("@name", aName);
  6411. att->setProp("@value", aIter->queryValue());
  6412. }
  6413. }
  6414. // 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)
  6415. Owned<IPropertyTreeIterator> iter = edge.getElements("*");
  6416. ForEach (*iter)
  6417. {
  6418. IPropertyTree &t = iter->query();
  6419. IPropertyTree *att = graphEdge->addPropTree("att", createPTree());
  6420. att->setProp("@name", t.queryName());
  6421. att->setProp("@value", t.queryProp(NULL));
  6422. }
  6423. }
  6424. }
  6425. }
  6426. Owned<IPropertyTreeIterator> nodes = progressNode->getElements("node");
  6427. ForEach (*nodes)
  6428. {
  6429. IPropertyTree &node = nodes->query();
  6430. StringBuffer nodePath("node[@id=\"");
  6431. nodePath.append(node.queryProp("@id")).append("\"]");
  6432. IPropertyTree *_node = graphNode->queryPropTree(nodePath.str());
  6433. if (_node)
  6434. {
  6435. if (progressV < 1)
  6436. mergePTree(_node, &node);
  6437. else
  6438. { // must translate to XGMML format
  6439. Owned<IAttributeIterator> aIter = node.getAttributes();
  6440. ForEach (*aIter)
  6441. {
  6442. const char *aName = aIter->queryName()+1;
  6443. if (0 != stricmp("id", aName)) // "id" reserved.
  6444. {
  6445. IPropertyTree *att = _node->addPropTree("att", createPTree());
  6446. att->setProp("@name", aName);
  6447. att->setProp("@value", aIter->queryValue());
  6448. }
  6449. }
  6450. }
  6451. }
  6452. }
  6453. }
  6454. Owned<IPropertyTreeIterator> iter = graphNode->getElements("node");
  6455. ForEach (*iter)
  6456. mergeProgress(iter->query(), progressTree, progressV);
  6457. }
  6458. IPropertyTree * CLocalWUGraph::getXGMMLTreeRaw() const
  6459. {
  6460. return p->getPropTree("xgmml");
  6461. }
  6462. IPropertyTree * CLocalWUGraph::getXGMMLTree(bool doMergeProgress) const
  6463. {
  6464. if (!graph)
  6465. {
  6466. // NB: although graphBin introduced in wuidVersion==2,
  6467. // daliadmin can retrospectively compress existing graphs, so need to check for all versions
  6468. MemoryBuffer mb;
  6469. if (p->getPropBin("xgmml/graphBin", mb))
  6470. graph.setown(createPTree(mb));
  6471. else
  6472. graph.setown(p->getBranch("xgmml/graph"));
  6473. if (!graph)
  6474. return NULL;
  6475. }
  6476. if (!doMergeProgress)
  6477. return graph.getLink();
  6478. else
  6479. {
  6480. Owned<IPropertyTree> copy = createPTreeFromIPT(graph);
  6481. Owned<IConstWUGraphProgress> _progress;
  6482. if (progress) _progress.set(progress);
  6483. else
  6484. _progress.setown(new CConstGraphProgress(wuid, p->queryProp("@name")));
  6485. unsigned progressV = _progress->queryFormatVersion();
  6486. IPropertyTree *progressTree = _progress->queryProgressTree();
  6487. Owned<IPropertyTreeIterator> nodeIterator = copy->getElements("node");
  6488. ForEach (*nodeIterator)
  6489. mergeProgress(nodeIterator->query(), *progressTree, progressV);
  6490. return copy.getClear();
  6491. }
  6492. }
  6493. bool CLocalWUGraph::isValid() const
  6494. {
  6495. // JCSMORE - I can't really see why this is necessary, a graph cannot be empty.
  6496. return p->hasProp("xgmml/graph/node") || p->hasProp("xgmml/graphBin");
  6497. }
  6498. WUGraphType CLocalWUGraph::getType() const
  6499. {
  6500. return (WUGraphType) getEnum(p, "@type", graphTypes);
  6501. }
  6502. IStringVal & CLocalWUGraph::getTypeName(IStringVal &str) const
  6503. {
  6504. str.set(p->queryProp("@type"));
  6505. if (!str.length())
  6506. str.set("unknown");
  6507. return str;
  6508. }
  6509. void CLocalWUGraph::setType(WUGraphType _type)
  6510. {
  6511. setEnum(p, "@type", _type, graphTypes);
  6512. }
  6513. //=================================================================================================
  6514. mapEnums queryFileTypes[] = {
  6515. { FileTypeCpp, "cpp" },
  6516. { FileTypeDll, "dll" },
  6517. { FileTypeResText, "res" },
  6518. { FileTypeHintXml, "hint" },
  6519. { FileTypeXml, "xml" },
  6520. { FileTypeSize, NULL },
  6521. };
  6522. CLocalWUAssociated::CLocalWUAssociated(IPropertyTree *props) : p(props)
  6523. {
  6524. }
  6525. WUFileType CLocalWUAssociated::getType() const
  6526. {
  6527. return (WUFileType)getEnum(p, "@type", queryFileTypes);
  6528. }
  6529. IStringVal & CLocalWUAssociated::getDescription(IStringVal & str) const
  6530. {
  6531. str.set(p->queryProp("@desc"));
  6532. return str;
  6533. }
  6534. IStringVal & CLocalWUAssociated::getIp(IStringVal & str) const
  6535. {
  6536. str.set(p->queryProp("@ip"));
  6537. return str;
  6538. }
  6539. IStringVal & CLocalWUAssociated::getName(IStringVal & str) const
  6540. {
  6541. str.set(p->queryProp("@filename"));
  6542. return str;
  6543. }
  6544. IStringVal & CLocalWUAssociated::getNameTail(IStringVal & str) const
  6545. {
  6546. str.set(pathTail(p->queryProp("@filename")));
  6547. return str;
  6548. }
  6549. unsigned CLocalWUAssociated::getCrc() const
  6550. {
  6551. return p->getPropInt("@crc", 0);
  6552. }
  6553. //=================================================================================================
  6554. CLocalWUQuery::CLocalWUQuery(IPropertyTree *props) : p(props)
  6555. {
  6556. associatedCached = false;
  6557. }
  6558. mapEnums queryTypes[] = {
  6559. { QueryTypeUnknown, "unknown" },
  6560. { QueryTypeEcl, "ECL" },
  6561. { QueryTypeSql, "SQL" },
  6562. { QueryTypeXml, "XML" },
  6563. { QueryTypeAttribute, "Attribute" },
  6564. { QueryTypeSize, NULL },
  6565. };
  6566. WUQueryType CLocalWUQuery::getQueryType() const
  6567. {
  6568. return (WUQueryType) getEnum(p, "@type", queryTypes);
  6569. }
  6570. void CLocalWUQuery::setQueryType(WUQueryType qt)
  6571. {
  6572. setEnum(p, "@type", qt, queryTypes);
  6573. }
  6574. IStringVal& CLocalWUQuery::getQueryText(IStringVal &str) const
  6575. {
  6576. str.set(p->queryProp("Text"));
  6577. return str;
  6578. }
  6579. IStringVal& CLocalWUQuery::getQueryShortText(IStringVal &str) const
  6580. {
  6581. const char * text = p->queryProp("Text");
  6582. if (isArchiveQuery(text))
  6583. {
  6584. Owned<IPropertyTree> xml = createPTreeFromXMLString(text, ipt_caseInsensitive);
  6585. const char * path = xml->queryProp("Query/@attributePath");
  6586. if (path)
  6587. {
  6588. IPropertyTree * resolved = resolveDefinitionInArchive(xml, path);
  6589. if (resolved)
  6590. str.set(resolved->queryProp(NULL));
  6591. }
  6592. else
  6593. str.set(xml->queryProp("Query"));
  6594. }
  6595. else
  6596. str.set(text);
  6597. return str;
  6598. }
  6599. IStringVal& CLocalWUQuery::getQueryName(IStringVal &str) const
  6600. {
  6601. str.set(p->queryProp("@name"));
  6602. return str;
  6603. }
  6604. IStringVal & CLocalWUQuery::getQueryMainDefinition(IStringVal & str) const
  6605. {
  6606. str.set(p->queryProp("@main"));
  6607. return str;
  6608. }
  6609. IStringVal& CLocalWUQuery::getQueryDllName(IStringVal &str) const
  6610. {
  6611. Owned<IConstWUAssociatedFile> entry = getAssociatedFile(FileTypeDll, 0);
  6612. if (entry)
  6613. entry->getNameTail(str);
  6614. return str;
  6615. }
  6616. IStringVal& CLocalWUQuery::getQueryCppName(IStringVal &str) const
  6617. {
  6618. Owned<IConstWUAssociatedFile> entry = getAssociatedFile(FileTypeCpp, 0);
  6619. if (entry)
  6620. entry->getName(str);
  6621. return str;
  6622. }
  6623. IStringVal& CLocalWUQuery::getQueryResTxtName(IStringVal &str) const
  6624. {
  6625. Owned<IConstWUAssociatedFile> entry = getAssociatedFile(FileTypeResText, 0);
  6626. if (entry)
  6627. entry->getName(str);
  6628. return str;
  6629. }
  6630. unsigned CLocalWUQuery::getQueryDllCrc() const
  6631. {
  6632. Owned<IConstWUAssociatedFile> entry = getAssociatedFile(FileTypeDll, 0);
  6633. if (entry)
  6634. return entry->getCrc();
  6635. return 0;
  6636. }
  6637. void CLocalWUQuery::setQueryText(const char *text)
  6638. {
  6639. p->setProp("Text", text);
  6640. }
  6641. void CLocalWUQuery::setQueryName(const char *qname)
  6642. {
  6643. p->setProp("@name", qname);
  6644. }
  6645. void CLocalWUQuery::setQueryMainDefinition(const char * str)
  6646. {
  6647. p->setProp("@main", str);
  6648. }
  6649. void CLocalWUQuery::addAssociatedFile(WUFileType type, const char * name, const char * ip, const char * desc, unsigned crc)
  6650. {
  6651. CriticalBlock block(crit);
  6652. loadAssociated();
  6653. if (!associated.length())
  6654. p->addPropTree("Associated", createPTree("Associated"));
  6655. IPropertyTree *pl = p->queryPropTree("Associated");
  6656. IPropertyTree *s = pl->addPropTree("File", createPTree("File"));
  6657. setEnum(s, "@type", type, queryFileTypes);
  6658. s->setProp("@filename", name);
  6659. s->setProp("@ip", ip);
  6660. s->setProp("@desc", desc);
  6661. if (crc)
  6662. s->setPropInt("@crc", crc);
  6663. IConstWUAssociatedFile * q = new CLocalWUAssociated(LINK(s));
  6664. associated.append(*q);
  6665. }
  6666. void CLocalWUQuery::removeAssociatedFiles()
  6667. {
  6668. associatedCached = false;
  6669. associated.kill();
  6670. p->removeProp("Associated");
  6671. }
  6672. IConstWUAssociatedFile * CLocalWUQuery::getAssociatedFile(WUFileType type, unsigned index) const
  6673. {
  6674. CriticalBlock block(crit);
  6675. loadAssociated();
  6676. ForEachItemIn(idx, associated)
  6677. {
  6678. CLocalWUAssociated &cur = static_cast<CLocalWUAssociated &>(associated.item(idx));
  6679. if (cur.getType() == type)
  6680. {
  6681. if (index-- == 0)
  6682. return &OLINK(cur);
  6683. }
  6684. }
  6685. return NULL;
  6686. }
  6687. void CLocalWUQuery::addSpecialCaseAssociated(WUFileType type, const char * propname, unsigned crc) const
  6688. {
  6689. const char * name = p->queryProp(propname);
  6690. if (name)
  6691. {
  6692. IPropertyTree *s = createPTree("File");
  6693. setEnum(s, "@type", type, queryFileTypes);
  6694. s->setProp("@filename", name);
  6695. if (crc)
  6696. s->setPropInt("@crc", crc);
  6697. associated.append(*new CLocalWUAssociated(s));
  6698. }
  6699. }
  6700. void CLocalWUQuery::loadAssociated() const
  6701. {
  6702. CriticalBlock block(crit);
  6703. if (!associatedCached)
  6704. {
  6705. assertex(associated.length() == 0);
  6706. addSpecialCaseAssociated(FileTypeDll, "DllName", p->getPropInt("DllCrc", 0));
  6707. addSpecialCaseAssociated(FileTypeCpp, "CppName", 0);
  6708. addSpecialCaseAssociated(FileTypeResText, "ResTxtName", 0);
  6709. Owned<IPropertyTreeIterator> r = p->getElements("Associated/File");
  6710. for (r->first(); r->isValid(); r->next())
  6711. {
  6712. IPropertyTree *rp = &r->query();
  6713. rp->Link();
  6714. associated.append(*new CLocalWUAssociated(rp));
  6715. }
  6716. associatedCached = true;
  6717. }
  6718. }
  6719. IConstWUAssociatedFileIterator& CLocalWUQuery::getAssociatedFiles() const
  6720. {
  6721. CriticalBlock block(crit);
  6722. loadAssociated();
  6723. return *new CArrayIteratorOf<IConstWUAssociatedFile,IConstWUAssociatedFileIterator> (associated, 0, (IConstWUQuery *) this);
  6724. }
  6725. //========================================================================================
  6726. CLocalWUWebServicesInfo::CLocalWUWebServicesInfo(IPropertyTree *props) : p(props)
  6727. {
  6728. }
  6729. IStringVal& CLocalWUWebServicesInfo::getModuleName(IStringVal &str) const
  6730. {
  6731. str.set(p->queryProp("@module"));
  6732. return str;
  6733. }
  6734. IStringVal& CLocalWUWebServicesInfo::getAttributeName(IStringVal &str) const
  6735. {
  6736. str.set(p->queryProp("@attribute"));
  6737. return str;
  6738. }
  6739. IStringVal& CLocalWUWebServicesInfo::getDefaultName(IStringVal &str) const
  6740. {
  6741. str.set(p->queryProp("@defaultName"));
  6742. return str;
  6743. }
  6744. unsigned CLocalWUWebServicesInfo::getWebServicesCRC() const
  6745. {
  6746. return (unsigned) p->getPropInt("@crc");
  6747. }
  6748. IStringVal& CLocalWUWebServicesInfo::getInfo(const char *name, IStringVal &str) const
  6749. {
  6750. if (!name)
  6751. {
  6752. StringBuffer ws_info;
  6753. ws_info.appendf("<%s ", p->queryName());
  6754. Owned<IAttributeIterator> attrs = p->getAttributes();
  6755. for(attrs->first(); attrs->isValid(); attrs->next())
  6756. {
  6757. const char *name = attrs->queryName()+1;
  6758. const char *value = attrs->queryValue();
  6759. ws_info.appendf("%s='%s' ", name, value);
  6760. }
  6761. ws_info.append("> \n");
  6762. Owned<IPropertyTreeIterator> info = p->getElements("*");
  6763. ForEach(*info)
  6764. {
  6765. IPropertyTree &item = info->query();
  6766. const char *name = item.queryName();
  6767. if (name)
  6768. {
  6769. MemoryBuffer mb;
  6770. bool isbin = p->isBinary(name);
  6771. if (isbin)
  6772. {
  6773. p->getPropBin(name,mb);
  6774. if (mb.length())
  6775. {
  6776. unsigned len = 0;
  6777. mb.read(len);
  6778. StringBuffer encodedString;
  6779. StringBuffer val(len, (const char *) mb.readDirect(len));
  6780. encodeXML(val, encodedString);
  6781. ws_info.appendf("<%s>%s</%s>", name, encodedString.str(), name);
  6782. }
  6783. }
  6784. else
  6785. {
  6786. StringBuffer tmp;
  6787. toXML(&item, tmp);
  6788. ws_info.append(tmp.str());
  6789. }
  6790. }
  6791. }
  6792. ws_info.appendf("</%s>", p->queryName());
  6793. str.setLen(ws_info.str(), ws_info.length());
  6794. }
  6795. else
  6796. {
  6797. MemoryBuffer mb;
  6798. p->getPropBin(name,mb);
  6799. if (mb.length())
  6800. {
  6801. unsigned len;
  6802. mb.read(len);
  6803. str.setLen((const char *) mb.readDirect(len), len);
  6804. }
  6805. }
  6806. return str;
  6807. }
  6808. void CLocalWUWebServicesInfo::setModuleName(const char *mname)
  6809. {
  6810. p->setProp("@module", mname);
  6811. }
  6812. void CLocalWUWebServicesInfo::setAttributeName(const char *aname)
  6813. {
  6814. p->setProp("@attribute", aname);
  6815. }
  6816. void CLocalWUWebServicesInfo::setDefaultName(const char *dname)
  6817. {
  6818. p->setProp("@defaultName", dname);
  6819. }
  6820. void CLocalWUWebServicesInfo::setWebServicesCRC(unsigned crc)
  6821. {
  6822. p->setPropInt("@crc", crc);
  6823. }
  6824. void CLocalWUWebServicesInfo::setInfo(const char *name, const char *info)
  6825. {
  6826. MemoryBuffer m;
  6827. unsigned len = (size32_t)strlen(info);
  6828. serializeLPString(len, info, m);
  6829. p->setPropBin(name, m.length(), m.toByteArray());
  6830. }
  6831. //========================================================================================
  6832. CLocalWURoxieQueryInfo::CLocalWURoxieQueryInfo(IPropertyTree *props) : p(props)
  6833. {
  6834. }
  6835. IStringVal& CLocalWURoxieQueryInfo::getQueryInfo(IStringVal &str) const
  6836. {
  6837. IPropertyTree *queryTree = p->queryPropTree("query");
  6838. if (queryTree)
  6839. {
  6840. StringBuffer temp;
  6841. toXML(queryTree, temp);
  6842. str.set(temp.str());
  6843. }
  6844. return str;
  6845. }
  6846. IStringVal& CLocalWURoxieQueryInfo::getDefaultPackageInfo(IStringVal &str) const
  6847. {
  6848. MemoryBuffer mb;
  6849. p->getPropBin("RoxiePackages",mb);
  6850. if (mb.length())
  6851. {
  6852. unsigned len;
  6853. mb.read(len);
  6854. str.setLen((const char *) mb.readDirect(len), len);
  6855. }
  6856. return str;
  6857. }
  6858. IStringVal& CLocalWURoxieQueryInfo::getRoxieClusterName(IStringVal &str) const
  6859. {
  6860. const char *val = p->queryProp("@roxieClusterName");
  6861. if (val)
  6862. str.set(val);
  6863. return str;
  6864. }
  6865. IStringVal& CLocalWURoxieQueryInfo::getWuid(IStringVal &str) const
  6866. {
  6867. const char *val = p->queryProp("@wuid");
  6868. if (val)
  6869. str.set(val);
  6870. return str;
  6871. }
  6872. void CLocalWURoxieQueryInfo::setQueryInfo(const char *info)
  6873. {
  6874. IPropertyTree *queryTree = p->queryPropTree("query");
  6875. if (queryTree)
  6876. p->removeTree(queryTree);
  6877. IPropertyTree * tempTree = p->addPropTree("query", createPTreeFromXMLString(info));
  6878. if (!p->hasProp("@roxieClusterName"))
  6879. {
  6880. const char *roxieClusterName = tempTree->queryProp("@roxieName");
  6881. if (roxieClusterName && *roxieClusterName)
  6882. p->addProp("@roxieClusterName", roxieClusterName);
  6883. }
  6884. if (!p->hasProp("@wuid"))
  6885. {
  6886. const char *wuid = tempTree->queryProp("Query/@wuid");
  6887. if (wuid && *wuid)
  6888. p->addProp("@wuid", wuid);
  6889. }
  6890. }
  6891. void CLocalWURoxieQueryInfo::setDefaultPackageInfo(const char *info, int len)
  6892. {
  6893. MemoryBuffer m;
  6894. serializeLPString(len, info, m);
  6895. p->setPropBin("RoxiePackages", m.length(), m.toByteArray());
  6896. }
  6897. void CLocalWURoxieQueryInfo::setRoxieClusterName(const char *info)
  6898. {
  6899. p->setProp("@roxieClusterName", info);
  6900. }
  6901. void CLocalWURoxieQueryInfo::setWuid(const char *info)
  6902. {
  6903. p->setProp("@wuid", info);
  6904. }
  6905. //========================================================================================
  6906. CLocalWUResult::CLocalWUResult(IPropertyTree *props) : p(props)
  6907. {
  6908. }
  6909. mapEnums resultStatuses[] = {
  6910. { ResultStatusUndefined, "undefined" },
  6911. { ResultStatusCalculated, "calculated" },
  6912. { ResultStatusSupplied, "supplied" },
  6913. { ResultStatusFailed, "failed" },
  6914. { ResultStatusPartial, "partial" },
  6915. { ResultStatusSize, NULL }
  6916. };
  6917. WUResultStatus CLocalWUResult::getResultStatus() const
  6918. {
  6919. return (WUResultStatus ) getEnum(p, "@status", resultStatuses);
  6920. }
  6921. IStringVal& CLocalWUResult::getResultName(IStringVal &str) const
  6922. {
  6923. str.set(p->queryProp("@name"));
  6924. return str;
  6925. }
  6926. int CLocalWUResult::getResultSequence() const
  6927. {
  6928. return p->getPropInt("@sequence", -1);
  6929. }
  6930. bool CLocalWUResult::isResultScalar() const
  6931. {
  6932. return p->getPropInt("@isScalar", 1) != 0;
  6933. }
  6934. bool findSize(int size, IntArray &sizes)
  6935. {
  6936. ForEachItemIn(idx, sizes)
  6937. {
  6938. if (sizes.item(idx)==size)
  6939. return true;
  6940. }
  6941. return false;
  6942. }
  6943. void CLocalWUResult::getSchema(TypeInfoArray &types, StringAttrArray &names, IStringVal * eclText) const
  6944. {
  6945. MemoryBuffer schema;
  6946. p->getPropBin("SchemaRaw", schema);
  6947. if (schema.length())
  6948. {
  6949. for (;;)
  6950. {
  6951. StringAttr name;
  6952. schema.read(name);
  6953. if (*schema.readDirect(0)==type_void)
  6954. break;
  6955. names.append(*new StringAttrItem(name));
  6956. types.append(*deserializeType(schema)); // MORE - nested records!
  6957. }
  6958. schema.skip(1);
  6959. if (schema.length() != schema.getPos())
  6960. {
  6961. unsigned eclLen;
  6962. schema.read(eclLen);
  6963. const char * schemaData = (const char *)schema.readDirect(eclLen);
  6964. if (eclText)
  6965. {
  6966. eclText->setLen(schemaData, eclLen);
  6967. if ((eclLen == 0) && names.ordinality())
  6968. {
  6969. const char * firstName = names.item(0).text;
  6970. StringBuffer temp;
  6971. temp.append("RECORD ");
  6972. types.item(0).getECLType(temp);
  6973. temp.append(" value{NAMED('").append(firstName).append("')}").append("; END;");
  6974. eclText->set(temp.str());
  6975. }
  6976. }
  6977. }
  6978. }
  6979. }
  6980. void readRow(StringBuffer &out, MemoryBuffer &in, TypeInfoArray &types, StringAttrArray &names)
  6981. {
  6982. ForEachItemIn(idx, types)
  6983. {
  6984. StringAttrItem &name = names.item(idx);
  6985. ITypeInfo &type = types.item(idx);
  6986. unsigned size = type.getSize();
  6987. switch(type.getTypeCode())
  6988. {
  6989. case type_data:
  6990. if (size==UNKNOWN_LENGTH)
  6991. {
  6992. if (in.remaining() < sizeof(int))
  6993. throw MakeStringException(WUERR_CorruptResult, "corrupt workunit information");
  6994. in.read(size);
  6995. }
  6996. outputXmlData(size, in.readDirect(size), name.text, out);
  6997. break;
  6998. case type_string:
  6999. if (size==UNKNOWN_LENGTH)
  7000. {
  7001. if (in.remaining() < sizeof(int))
  7002. throw MakeStringException(WUERR_CorruptResult, "corrupt workunit information");
  7003. in.read(size);
  7004. }
  7005. outputXmlString(size, (const char *) in.readDirect(size), name.text, out);
  7006. break;
  7007. case type_varstring:
  7008. {
  7009. if (size == UNKNOWN_LENGTH)
  7010. size = (size32_t)strlen((const char *) in.readDirect(0))+1;
  7011. const char * text = (const char *) in.readDirect(size);
  7012. outputXmlString((size32_t)strlen(text), text, name.text, out);
  7013. break;
  7014. }
  7015. case type_unicode:
  7016. {
  7017. unsigned len = type.getStringLen();
  7018. if (size==UNKNOWN_LENGTH)
  7019. in.read(len);
  7020. outputXmlUnicode(len, (UChar const *) in.readDirect(len*2), name.text, out);
  7021. }
  7022. break;
  7023. case type_utf8:
  7024. {
  7025. unsigned len = type.getStringLen();
  7026. if (size==UNKNOWN_LENGTH)
  7027. {
  7028. in.read(len);
  7029. size = rtlUtf8Size(len, in.readDirect(0));
  7030. }
  7031. outputXmlUtf8(len, (const char *) in.readDirect(size), name.text, out);
  7032. }
  7033. break;
  7034. case type_qstring:
  7035. {
  7036. unsigned len = type.getStringLen();
  7037. if (size==UNKNOWN_LENGTH)
  7038. in.read(len);
  7039. unsigned outlen;
  7040. char *outstr;
  7041. rtlQStrToStrX(outlen, outstr, len, (const char *) in.readDirect(rtlQStrSize(len)));
  7042. outputXmlString(outlen, outstr, name.text, out);
  7043. free(outstr);
  7044. break;
  7045. }
  7046. case type_int:
  7047. case type_swapint:
  7048. if (type.isSigned())
  7049. {
  7050. const unsigned char *raw = (const unsigned char *) in.readDirect(size);
  7051. unsigned __int64 cval8 = 0;
  7052. //MORE: I think this is wrong - swapped doesn't mean little/big/
  7053. if (type.isSwappedEndian())
  7054. {
  7055. unsigned idx = 0;
  7056. if (raw[idx] & 0x80)
  7057. cval8 = (__int64)-1;
  7058. while (size--)
  7059. cval8 = (cval8 << 8) | raw[idx++];
  7060. }
  7061. else
  7062. {
  7063. if (raw[size-1] & 0x80)
  7064. cval8 = (__int64)-1;
  7065. while (size--)
  7066. cval8 = (cval8 << 8) | raw[size];
  7067. }
  7068. outputXmlInt((__int64) cval8, name.text, out);
  7069. }
  7070. else
  7071. {
  7072. const unsigned char *raw = (const unsigned char *) in.readDirect(size);
  7073. unsigned __int64 cval8 = 0;
  7074. if (type.isSwappedEndian())
  7075. {
  7076. unsigned idx = 0;
  7077. while (size--)
  7078. cval8 = (cval8 << 8) | raw[idx++];
  7079. }
  7080. else
  7081. {
  7082. while (size--)
  7083. cval8 = (cval8 << 8) | raw[size];
  7084. }
  7085. outputXmlUInt(cval8, name.text, out);
  7086. }
  7087. break;
  7088. case type_boolean:
  7089. bool cvalb;
  7090. in.read(cvalb);
  7091. outputXmlBool(cvalb, name.text, out);
  7092. break;
  7093. case type_decimal:
  7094. if (type.isSigned())
  7095. outputXmlDecimal(in.readDirect(size), size, type.getPrecision(), name.text, out);
  7096. else
  7097. outputXmlUDecimal(in.readDirect(size), size, type.getPrecision(), name.text, out);
  7098. break;
  7099. case type_real:
  7100. double cvald;
  7101. switch(size)
  7102. {
  7103. case 4:
  7104. float cvalf;
  7105. in.read(cvalf);
  7106. cvald = cvalf;
  7107. break;
  7108. case 8:
  7109. in.read(cvald);
  7110. break;
  7111. }
  7112. outputXmlReal(cvald, name.text, out);
  7113. break;
  7114. default:
  7115. assertex(!"unexpected type in raw record");
  7116. break;
  7117. }
  7118. }
  7119. }
  7120. IStringVal& CLocalWUResult::getResultXml(IStringVal &str) const
  7121. {
  7122. TypeInfoArray types;
  7123. StringAttrArray names;
  7124. getSchema(types, names);
  7125. StringBuffer xml;
  7126. const char * name = p->queryProp("@name");
  7127. if (name)
  7128. xml.appendf("<Dataset name=\'%s\'>\n", name);
  7129. else
  7130. xml.append("<Dataset>\n");
  7131. if (p->hasProp("Value"))
  7132. {
  7133. MemoryBuffer raw;
  7134. p->getPropBin("Value", raw);
  7135. unsigned __int64 numrows = getResultRowCount();
  7136. while (numrows--)
  7137. {
  7138. xml.append(" <Row>");
  7139. readRow(xml, raw, types, names);
  7140. xml.append("</Row>\n");
  7141. }
  7142. }
  7143. else if (p->hasProp("xmlValue"))
  7144. {
  7145. xml.append(" <Row>");
  7146. appendXMLTag(xml, name, p->queryProp("xmlValue"));
  7147. xml.append("</Row>\n");
  7148. }
  7149. xml.append("</Dataset>\n");
  7150. str.set(xml.str());
  7151. return str;
  7152. }
  7153. IProperties *CLocalWUResult::queryXmlns()
  7154. {
  7155. CriticalBlock block(crit);
  7156. if (xmlns)
  7157. return xmlns;
  7158. xmlns.setown(createProperties());
  7159. Owned<IAttributeIterator> it = p->getAttributes();
  7160. unsigned prefixLen = strlen("@xmlns");
  7161. ForEach(*it)
  7162. {
  7163. const char *name = it->queryName();
  7164. if (!strncmp("@xmlns", name, prefixLen))
  7165. {
  7166. if (name[prefixLen]==':') //normal case
  7167. xmlns->setProp(name+prefixLen+1, it->queryValue());
  7168. else if (!name[prefixLen]) //special case, unprefixed namespace
  7169. xmlns->setProp("xmlns", it->queryValue());
  7170. }
  7171. }
  7172. return xmlns;
  7173. }
  7174. unsigned CLocalWUResult::getResultFetchSize() const
  7175. {
  7176. return p->getPropInt("fetchSize", 100);
  7177. }
  7178. __int64 CLocalWUResult::getResultTotalRowCount() const
  7179. {
  7180. return p->getPropInt64("totalRowCount", -1);
  7181. }
  7182. __int64 CLocalWUResult::getResultRowCount() const
  7183. {
  7184. return p->getPropInt64("rowCount", 0);
  7185. }
  7186. void CLocalWUResult::getResultDataset(IStringVal & ecl, IStringVal & defs) const
  7187. {
  7188. ecl.set(p->queryProp("datasetEcl"));
  7189. defs.set(p->queryProp("datasetEclDefs"));
  7190. }
  7191. IStringVal& CLocalWUResult::getResultLogicalName(IStringVal & val) const
  7192. {
  7193. val.set(p->queryProp("logicalName"));
  7194. return val;
  7195. }
  7196. IStringVal& CLocalWUResult::getResultKeyField(IStringVal & ecl) const
  7197. {
  7198. ecl.set(p->queryProp("keyField"));
  7199. return ecl;
  7200. }
  7201. unsigned CLocalWUResult::getResultRequestedRows() const
  7202. {
  7203. return p->getPropInt("requestedRows", 1);
  7204. }
  7205. IStringVal& CLocalWUResult::getResultEclSchema(IStringVal & str) const
  7206. {
  7207. TypeInfoArray types;
  7208. StringAttrArray names;
  7209. getSchema(types, names, &str);
  7210. return str;
  7211. }
  7212. IStringVal& CLocalWUResult::getResultRecordSizeEntry(IStringVal & str) const
  7213. {
  7214. str.set(p->queryProp("@recordSizeEntry"));
  7215. return str;
  7216. }
  7217. IStringVal& CLocalWUResult::getResultTransformerEntry(IStringVal & str) const
  7218. {
  7219. str.set(p->queryProp("@transformerEntry"));
  7220. return str;
  7221. }
  7222. __int64 CLocalWUResult::getResultRowLimit() const
  7223. {
  7224. return p->getPropInt64("@rowLimit");
  7225. }
  7226. IStringVal& CLocalWUResult::getResultFilename(IStringVal & str) const
  7227. {
  7228. str.set(p->queryProp("@tempFilename"));
  7229. return str;
  7230. }
  7231. void CLocalWUResult::setResultStatus(WUResultStatus status)
  7232. {
  7233. setEnum(p, "@status", status, resultStatuses);
  7234. if (status==ResultStatusUndefined)
  7235. p->removeProp("Value");
  7236. }
  7237. void CLocalWUResult::setResultName(const char *s)
  7238. {
  7239. p->setProp("@name", s);
  7240. }
  7241. void CLocalWUResult::setResultSequence(unsigned seq)
  7242. {
  7243. p->setPropInt("@sequence", seq);
  7244. }
  7245. void CLocalWUResult::setResultSchemaRaw(unsigned size, const void *schema)
  7246. {
  7247. p->setPropBin("SchemaRaw", size, schema);
  7248. }
  7249. void CLocalWUResult::setXmlns(const char *prefix, const char *uri)
  7250. {
  7251. StringBuffer xpath("@xmlns");
  7252. if (prefix && *prefix)
  7253. xpath.append(':').append(prefix);
  7254. p->setProp(xpath, uri);
  7255. }
  7256. void CLocalWUResult::setResultScalar(bool isScalar)
  7257. {
  7258. p->setPropInt("@isScalar", (int) isScalar);
  7259. if (isScalar)
  7260. setResultTotalRowCount(1);
  7261. }
  7262. void CLocalWUResult::setResultRaw(unsigned len, const void *data, WUResultFormat format)
  7263. {
  7264. p->setPropBin("Value", len, data);
  7265. setResultStatus(ResultStatusSupplied);
  7266. setResultFormat(format);
  7267. }
  7268. void CLocalWUResult::setResultFormat(WUResultFormat format)
  7269. {
  7270. switch (format)
  7271. {
  7272. case ResultFormatXml:
  7273. p->setProp("@format","xml");
  7274. break;
  7275. case ResultFormatXmlSet:
  7276. p->setProp("@format","xmlSet");
  7277. break;
  7278. case ResultFormatCsv:
  7279. p->setProp("@format","csv");
  7280. break;
  7281. default:
  7282. p->removeProp("@format");
  7283. break;
  7284. }
  7285. }
  7286. void CLocalWUResult::setResultXML(const char *val)
  7287. {
  7288. p->setProp("xmlValue", val);
  7289. }
  7290. void CLocalWUResult::addResultRaw(unsigned len, const void *data, WUResultFormat format)
  7291. {
  7292. p->appendPropBin("Value", len, data);
  7293. setResultStatus(ResultStatusPartial);
  7294. const char *existingFormat = p->queryProp("@format");
  7295. const char *formatStr = NULL;
  7296. switch (format)
  7297. {
  7298. case ResultFormatXml:
  7299. formatStr = "xml";
  7300. break;
  7301. case ResultFormatXmlSet:
  7302. formatStr = "xmlSet";
  7303. break;
  7304. case ResultFormatCsv:
  7305. formatStr = "csv";
  7306. break;
  7307. default:
  7308. p->removeProp("@format");
  7309. break;
  7310. }
  7311. if (format)
  7312. {
  7313. if (existingFormat)
  7314. {
  7315. if (0 != stricmp(formatStr, existingFormat))
  7316. throw MakeStringException(WUERR_ResultFormatMismatch, "addResult format %s, does not match existing format %s", formatStr, existingFormat);
  7317. }
  7318. else
  7319. p->setProp("@format", formatStr);
  7320. }
  7321. }
  7322. void CLocalWUResult::setResultFetchSize(unsigned rows)
  7323. {
  7324. p->setPropInt("fetchSize", rows);
  7325. }
  7326. void CLocalWUResult::setResultTotalRowCount(__int64 rows)
  7327. {
  7328. p->setPropInt64("totalRowCount", rows);
  7329. }
  7330. void CLocalWUResult::setResultRowCount(__int64 rows)
  7331. {
  7332. p->setPropInt64("rowCount", rows);
  7333. }
  7334. void CLocalWUResult::setResultDataset(const char *ecl, const char *defs)
  7335. {
  7336. p->setProp("datasetEcl", ecl);
  7337. p->setProp("datasetEclDefs", defs);
  7338. }
  7339. void CLocalWUResult::setResultLogicalName(const char *logicalName)
  7340. {
  7341. p->setProp("logicalName", logicalName);
  7342. }
  7343. void CLocalWUResult::setResultKeyField(const char *ecl)
  7344. {
  7345. p->setProp("keyField", ecl);
  7346. }
  7347. void CLocalWUResult::setResultRequestedRows(unsigned rows)
  7348. {
  7349. p->setPropInt("requestedRows", rows);
  7350. }
  7351. void CLocalWUResult::setResultRecordSizeEntry(const char * entry)
  7352. {
  7353. p->setProp("@recordSizeEntry", entry);
  7354. }
  7355. void CLocalWUResult::setResultTransformerEntry(const char * entry)
  7356. {
  7357. p->setProp("@transformerEntry", entry);
  7358. }
  7359. void CLocalWUResult::setResultRowLimit(__int64 value)
  7360. {
  7361. p->setPropInt64("@rowLimit", value);
  7362. }
  7363. void CLocalWUResult::setResultFilename(const char * name)
  7364. {
  7365. p->setProp("@tempFilename", name);
  7366. }
  7367. // MORE - it's an undetected error if we call getResult... of a type that does not match schema
  7368. __int64 CLocalWUResult::getResultInt() const
  7369. {
  7370. __int64 result = 0;
  7371. MemoryBuffer s;
  7372. p->getPropBin("Value", s);
  7373. if (s.length())
  7374. s.read(result);
  7375. else
  7376. result = p->getPropInt64("xmlValue");
  7377. return result;
  7378. }
  7379. bool CLocalWUResult::getResultBool() const
  7380. {
  7381. bool result = false;
  7382. MemoryBuffer s;
  7383. p->getPropBin("Value", s);
  7384. if (s.length())
  7385. s.read(result);
  7386. else
  7387. result = p->getPropBool("xmlValue");
  7388. return result;
  7389. }
  7390. double CLocalWUResult::getResultReal() const
  7391. {
  7392. double result = 0;
  7393. MemoryBuffer s;
  7394. p->getPropBin("Value", s);
  7395. if (s.length())
  7396. s.read(result);
  7397. else
  7398. {
  7399. const char *xmlVal = p->queryProp("xmlValue");
  7400. if (xmlVal)
  7401. result = atof(xmlVal);
  7402. }
  7403. return result;
  7404. }
  7405. void CLocalWUResult::getResultDecimal(void * val, unsigned len, unsigned precision, bool isSigned) const
  7406. {
  7407. MemoryBuffer s;
  7408. p->getPropBin("Value", s);
  7409. if (s.length())
  7410. {
  7411. assertex(s.length() == len);
  7412. s.read(len, val);
  7413. }
  7414. else
  7415. {
  7416. const char *xmlVal = p->queryProp("xmlValue");
  7417. if (xmlVal)
  7418. {
  7419. Decimal d;
  7420. d.setString(strlen(xmlVal), xmlVal);
  7421. if (isSigned)
  7422. d.getDecimal(len, precision, val);
  7423. else
  7424. d.getUDecimal(len, precision, val);
  7425. }
  7426. else
  7427. memset(val, 0, len);
  7428. }
  7429. }
  7430. IStringVal& CLocalWUResult::getResultString(IStringVal & str) const
  7431. {
  7432. MemoryBuffer s;
  7433. p->getPropBin("Value", s);
  7434. if (s.length())
  7435. {
  7436. unsigned len;
  7437. s.read(len);
  7438. str.setLen((const char *) s.readDirect(len), len);
  7439. }
  7440. else
  7441. {
  7442. p->getPropBin("xmlValue", s);
  7443. if (p->isBinary("xmlValue"))
  7444. str.setLen(s.toByteArray(), s.length());
  7445. else
  7446. {
  7447. char *ascii = rtlUtf8ToVStr(rtlUtf8Length(s.length(), s.toByteArray()), s.toByteArray());
  7448. str.set(ascii);
  7449. rtlFree(ascii);
  7450. }
  7451. }
  7452. return str;
  7453. }
  7454. WUResultFormat CLocalWUResult::getResultFormat() const
  7455. {
  7456. const char * format = p->queryProp("@format");
  7457. if (!format)
  7458. return ResultFormatRaw;
  7459. else if (strcmp(format, "xml") == 0)
  7460. return ResultFormatXml;
  7461. else if (strcmp(format, "xmlSet") == 0)
  7462. return ResultFormatXmlSet;
  7463. else if (strcmp(format, "csv") == 0)
  7464. return ResultFormatCsv;
  7465. else
  7466. throw MakeStringException(WUERR_InvalidResultFormat, "Unrecognised result format %s", format);
  7467. }
  7468. IDataVal& CLocalWUResult::getResultRaw(IDataVal & data, IXmlToRawTransformer * xmlTransformer, ICsvToRawTransformer * csvTransformer) const
  7469. {
  7470. MemoryBuffer s;
  7471. p->getPropBin("Value", s);
  7472. unsigned len = s.length();
  7473. if (len)
  7474. {
  7475. WUResultFormat format = getResultFormat();
  7476. if (format == ResultFormatXml || format == ResultFormatXmlSet)
  7477. {
  7478. if (!xmlTransformer)
  7479. throw MakeStringException(WUERR_MissingFormatTranslator, "No transformer supplied to translate XML format result");
  7480. xmlTransformer->transform(data, len, s.readDirect(len), format == ResultFormatXml);
  7481. }
  7482. else if (format == ResultFormatCsv)
  7483. {
  7484. if (!csvTransformer)
  7485. throw MakeStringException(WUERR_MissingFormatTranslator, "No transformer supplied to translate Csv format result");
  7486. csvTransformer->transform(data, len, s.readDirect(len), true);
  7487. }
  7488. else
  7489. data.setLen(s.readDirect(len), len);
  7490. }
  7491. else
  7492. data.clear();
  7493. return data;
  7494. }
  7495. unsigned CLocalWUResult::getResultHash() const
  7496. {
  7497. MemoryBuffer s;
  7498. p->getPropBin("Value", s);
  7499. unsigned len = s.length();
  7500. const byte * data = (const byte *)s.toByteArray();
  7501. return ~hashc(data, len, ~0);
  7502. }
  7503. IDataVal& CLocalWUResult::getResultUnicode(IDataVal & data) const
  7504. {
  7505. MemoryBuffer s;
  7506. p->getPropBin("Value", s);
  7507. if (s.length())
  7508. {
  7509. unsigned len;
  7510. s.read(len);
  7511. data.setLen(s.readDirect(len*2), len*2);
  7512. }
  7513. else
  7514. {
  7515. StringBuffer utf8;
  7516. if (p->getProp("xmlValue", utf8))
  7517. {
  7518. unsigned outlen;
  7519. UChar *out;
  7520. rtlUtf8ToUnicodeX(outlen, out, utf8.length(), utf8.str());
  7521. data.setLen(out, outlen*2);
  7522. rtlFree(out);
  7523. }
  7524. else
  7525. data.clear();
  7526. }
  7527. return data;
  7528. }
  7529. __int64 CLocalWUResult::getResultRawSize(IXmlToRawTransformer * xmlTransformer, ICsvToRawTransformer * csvTransformer) const
  7530. {
  7531. WUResultFormat format = getResultFormat();
  7532. if (format == ResultFormatRaw)
  7533. {
  7534. //MORE: This should not load the whole property...
  7535. MemoryBuffer s;
  7536. p->getPropBin("Value", s);
  7537. return s.length();
  7538. }
  7539. else
  7540. {
  7541. MemoryBuffer temp;
  7542. MemoryBuffer2IDataVal adaptor(temp);
  7543. getResultRaw(adaptor, xmlTransformer, csvTransformer);
  7544. return temp.length();
  7545. }
  7546. }
  7547. IDataVal& CLocalWUResult::getResultRaw(IDataVal & data, __int64 from, __int64 length, IXmlToRawTransformer * xmlTransformer, ICsvToRawTransformer * csvTransformer) const
  7548. {
  7549. WUResultFormat format = getResultFormat();
  7550. if (format != ResultFormatRaw)
  7551. {
  7552. MemoryBuffer temp;
  7553. MemoryBuffer2IDataVal adaptor(temp);
  7554. getResultRaw(adaptor, xmlTransformer, csvTransformer);
  7555. unsigned len = temp.length();
  7556. if (from > len) from = len;
  7557. if (from + length > len) length = len - from;
  7558. data.setLen(temp.readDirect(len) + from, (size32_t)length);
  7559. return data;
  7560. }
  7561. else
  7562. {
  7563. //MORE: This should not load the whole property, and should be different from the code above...
  7564. MemoryBuffer s;
  7565. p->getPropBin("Value", s);
  7566. unsigned len = s.length();
  7567. if (from > len) from = len;
  7568. if (from + length > len) length = len - from;
  7569. data.setLen(s.readDirect(len) + from, (size32_t)length);
  7570. return data;
  7571. }
  7572. }
  7573. bool CLocalWUResult::getResultIsAll() const
  7574. {
  7575. return p->getPropBool("@isAll", false);
  7576. }
  7577. // MORE - it's an undetected error if we call setResult... of a type that does not match schema
  7578. void CLocalWUResult::setResultInt(__int64 val)
  7579. {
  7580. // Note: we always serialize scalar integer results as int8, and schema must reflect this
  7581. MemoryBuffer m;
  7582. serializeInt8(val, m);
  7583. p->setPropBin("Value", m.length(), m.toByteArray());
  7584. setResultRowCount(1);
  7585. setResultTotalRowCount(1);
  7586. }
  7587. void CLocalWUResult::setResultUInt(unsigned __int64 val)
  7588. {
  7589. setResultInt((__int64) val);
  7590. }
  7591. void CLocalWUResult::setResultReal(double val)
  7592. {
  7593. // Note: we always serialize scalar real results as real8, and schema must reflect this
  7594. MemoryBuffer m;
  7595. serializeReal8(val, m);
  7596. p->setPropBin("Value", m.length(), m.toByteArray());
  7597. setResultRowCount(1);
  7598. setResultTotalRowCount(1);
  7599. }
  7600. void CLocalWUResult::setResultBool(bool val)
  7601. {
  7602. MemoryBuffer m;
  7603. serializeBool(val, m);
  7604. p->setPropBin("Value", m.length(), m.toByteArray());
  7605. setResultRowCount(1);
  7606. setResultTotalRowCount(1);
  7607. }
  7608. void CLocalWUResult::setResultString(const char *val, unsigned len)
  7609. {
  7610. // Note: we always serialize scalar strings with length prefix, and schema must reflect this
  7611. MemoryBuffer m;
  7612. serializeLPString(len, val, m);
  7613. p->setPropBin("Value", m.length(), m.toByteArray());
  7614. setResultRowCount(1);
  7615. setResultTotalRowCount(1);
  7616. }
  7617. void CLocalWUResult::setResultUnicode(const void *val, unsigned len)
  7618. {
  7619. // Note: we always serialize scalar strings with length prefix, and schema must reflect this
  7620. MemoryBuffer m;
  7621. m.append(len).append(len*2, val);
  7622. p->setPropBin("Value", m.length(), m.toByteArray());
  7623. setResultRowCount(1);
  7624. setResultTotalRowCount(1);
  7625. }
  7626. void CLocalWUResult::setResultData(const void *val, unsigned len)
  7627. {
  7628. // Note: we always serialize scalar data with length prefix, and schema must reflect this
  7629. MemoryBuffer m;
  7630. serializeLPString(len, (const char *)val, m);
  7631. p->setPropBin("Value", m.length(), m.toByteArray());
  7632. setResultRowCount(1);
  7633. setResultTotalRowCount(1);
  7634. }
  7635. void CLocalWUResult::setResultDecimal(const void *val, unsigned len)
  7636. {
  7637. // Note: serialized as data but with length known from schema
  7638. MemoryBuffer m;
  7639. serializeFixedData(len, val, m);
  7640. p->setPropBin("Value", m.length(), m.toByteArray());
  7641. setResultRowCount(1);
  7642. setResultTotalRowCount(1);
  7643. }
  7644. void CLocalWUResult::setResultRow(unsigned len, const void * data)
  7645. {
  7646. p->setPropBin("Value", len, data);
  7647. setResultRowCount(1);
  7648. setResultTotalRowCount(1);
  7649. setResultFormat(ResultFormatRaw);
  7650. }
  7651. void CLocalWUResult::setResultIsAll(bool value)
  7652. {
  7653. p->setPropBool("@isAll", value);
  7654. }
  7655. //==========================================================================================
  7656. CLocalWUPlugin::CLocalWUPlugin(IPropertyTree *props) : p(props)
  7657. {
  7658. }
  7659. IStringVal& CLocalWUPlugin::getPluginName(IStringVal &str) const
  7660. {
  7661. str.set(p->queryProp("@dllname"));
  7662. return str;
  7663. }
  7664. IStringVal& CLocalWUPlugin::getPluginVersion(IStringVal &str) const
  7665. {
  7666. str.set(p->queryProp("@version"));
  7667. return str;
  7668. }
  7669. bool CLocalWUPlugin::getPluginThor() const
  7670. {
  7671. return p->getPropInt("@thor") != 0;
  7672. }
  7673. bool CLocalWUPlugin::getPluginHole() const
  7674. {
  7675. return p->getPropInt("@hole") != 0;
  7676. }
  7677. void CLocalWUPlugin::setPluginName(const char *str)
  7678. {
  7679. p->setProp("@dllname", str);
  7680. }
  7681. void CLocalWUPlugin::setPluginVersion(const char *str)
  7682. {
  7683. p->setProp("@version", str);
  7684. }
  7685. void CLocalWUPlugin::setPluginThor(bool on)
  7686. {
  7687. p->setPropInt("@thor", (int) on);
  7688. }
  7689. void CLocalWUPlugin::setPluginHole(bool on)
  7690. {
  7691. p->setPropInt("@hole", (int) on);
  7692. }
  7693. //==========================================================================================
  7694. class WULibraryActivityIterator : public CInterface, implements IConstWULibraryActivityIterator
  7695. {
  7696. public:
  7697. WULibraryActivityIterator(IPropertyTree * tree) { iter.setown(tree->getElements("activity")); }
  7698. IMPLEMENT_IINTERFACE;
  7699. bool first() { return iter->first(); }
  7700. bool isValid() { return iter->isValid(); }
  7701. bool next() { return iter->next(); }
  7702. unsigned query() const { return iter->query().getPropInt("@id"); }
  7703. private:
  7704. Owned<IPropertyTreeIterator> iter;
  7705. };
  7706. CLocalWULibrary::CLocalWULibrary(IPropertyTree *props) : p(props)
  7707. {
  7708. }
  7709. IStringVal& CLocalWULibrary::getName(IStringVal &str) const
  7710. {
  7711. str.set(p->queryProp("@name"));
  7712. return str;
  7713. }
  7714. IConstWULibraryActivityIterator * CLocalWULibrary::getActivities() const
  7715. {
  7716. return new WULibraryActivityIterator(p);
  7717. }
  7718. void CLocalWULibrary::setName(const char *str)
  7719. {
  7720. p->setProp("@name", str);
  7721. }
  7722. void CLocalWULibrary::addActivity(unsigned id)
  7723. {
  7724. StringBuffer s;
  7725. s.append("activity[@id=\"").append(id).append("\"]");
  7726. if (!p->hasProp(s.str()))
  7727. p->addPropTree("activity", createPTree())->setPropInt("@id", id);
  7728. }
  7729. //==========================================================================================
  7730. CLocalWUActivity::CLocalWUActivity(IPropertyTree *props, __int64 id) : p(props)
  7731. {
  7732. if (id)
  7733. p->setPropInt64("@id", id);
  7734. }
  7735. __int64 CLocalWUActivity::getId() const
  7736. {
  7737. return p->getPropInt64("@id");
  7738. }
  7739. unsigned CLocalWUActivity::getKind() const
  7740. {
  7741. return (unsigned)p->getPropInt("@kind");
  7742. }
  7743. IStringVal & CLocalWUActivity::getHelper(IStringVal & str) const
  7744. {
  7745. str.set(p->queryProp("@helper"));
  7746. return str;
  7747. }
  7748. void CLocalWUActivity::setKind(unsigned kind)
  7749. {
  7750. p->setPropInt64("@kind", kind);
  7751. }
  7752. void CLocalWUActivity::setHelper(const char * str)
  7753. {
  7754. p->setProp("@helper", str);
  7755. }
  7756. //==========================================================================================
  7757. CLocalWUException::CLocalWUException(IPropertyTree *props) : p(props)
  7758. {
  7759. }
  7760. IStringVal& CLocalWUException::getExceptionSource(IStringVal &str) const
  7761. {
  7762. str.set(p->queryProp("@source"));
  7763. return str;
  7764. }
  7765. IStringVal& CLocalWUException::getExceptionMessage(IStringVal &str) const
  7766. {
  7767. str.set(p->queryProp(NULL));
  7768. return str;
  7769. }
  7770. unsigned CLocalWUException::getExceptionCode() const
  7771. {
  7772. return p->getPropInt("@code", 0);
  7773. }
  7774. WUExceptionSeverity CLocalWUException::getSeverity() const
  7775. {
  7776. return (WUExceptionSeverity)p->getPropInt("@severity", ExceptionSeverityError);
  7777. }
  7778. IStringVal & CLocalWUException::getTimeStamp(IStringVal & dt) const
  7779. {
  7780. dt.set(p->queryProp("@time"));
  7781. return dt;
  7782. }
  7783. IStringVal & CLocalWUException::getExceptionFileName(IStringVal & str) const
  7784. {
  7785. str.set(p->queryProp("@filename"));
  7786. return str;
  7787. }
  7788. unsigned CLocalWUException::getExceptionLineNo() const
  7789. {
  7790. return p->getPropInt("@row", 0);
  7791. }
  7792. unsigned CLocalWUException::getExceptionColumn() const
  7793. {
  7794. return p->getPropInt("@col", 0);
  7795. }
  7796. void CLocalWUException::setExceptionSource(const char *str)
  7797. {
  7798. p->setProp("@source", str);
  7799. }
  7800. void CLocalWUException::setExceptionMessage(const char *str)
  7801. {
  7802. p->setProp(NULL, str);
  7803. }
  7804. void CLocalWUException::setExceptionCode(unsigned code)
  7805. {
  7806. p->setPropInt("@code", code);
  7807. }
  7808. void CLocalWUException::setSeverity(WUExceptionSeverity level)
  7809. {
  7810. p->setPropInt("@severity", level);
  7811. }
  7812. void CLocalWUException::setTimeStamp(const char *str)
  7813. {
  7814. p->setProp("@time", str);
  7815. }
  7816. void CLocalWUException::setExceptionFileName(const char *str)
  7817. {
  7818. p->setProp("@filename", str);
  7819. }
  7820. void CLocalWUException::setExceptionLineNo(unsigned r)
  7821. {
  7822. p->setPropInt("@row", r);
  7823. }
  7824. void CLocalWUException::setExceptionColumn(unsigned c)
  7825. {
  7826. p->setPropInt("@col", c);
  7827. }
  7828. //==========================================================================================
  7829. CLocalWUTimeStamp::CLocalWUTimeStamp(IPropertyTree *props) : p(props)
  7830. {
  7831. }
  7832. IStringVal & CLocalWUTimeStamp::getApplication(IStringVal & str) const
  7833. {
  7834. str.set(p->queryProp("@application"));
  7835. return str;
  7836. }
  7837. IStringVal & CLocalWUTimeStamp::getEvent(IStringVal & str) const
  7838. {
  7839. IPropertyTree* evt=p->queryPropTree("*[1]");
  7840. if(evt)
  7841. str.set(evt->queryName());
  7842. return str;
  7843. }
  7844. IStringVal & CLocalWUTimeStamp::getDate(IStringVal & str) const
  7845. {
  7846. str.set(p->queryProp("*[1]"));
  7847. return str;
  7848. }
  7849. //==========================================================================================
  7850. CLocalWUAppValue::CLocalWUAppValue(IPropertyTree *props,unsigned child): p(props)
  7851. {
  7852. prop.append("*[").append(child).append("]");
  7853. }
  7854. IStringVal & CLocalWUAppValue::getApplication(IStringVal & str) const
  7855. {
  7856. str.set(p->queryName());
  7857. return str;
  7858. }
  7859. IStringVal & CLocalWUAppValue::getName(IStringVal & str) const
  7860. {
  7861. IPropertyTree* val=p->queryPropTree(prop.str());
  7862. if(val)
  7863. str.set(val->queryName());
  7864. return str;
  7865. }
  7866. IStringVal & CLocalWUAppValue::getValue(IStringVal & str) const
  7867. {
  7868. str.set(p->queryProp(prop.str()));
  7869. return str;
  7870. }
  7871. //==========================================================================================
  7872. CLocalWUStatistic::CLocalWUStatistic(IPropertyTree *props) : p(props)
  7873. {
  7874. }
  7875. IStringVal & CLocalWUStatistic::getFullName(IStringVal & str) const
  7876. {
  7877. str.set(p->queryProp("@name"));
  7878. return str;
  7879. }
  7880. IStringVal & CLocalWUStatistic::getCreator(IStringVal & str) const
  7881. {
  7882. const char * name = p->queryProp("@name");
  7883. const char * sep1 = strchr(name, ';');
  7884. assertex(sep1);
  7885. str.setLen(name, sep1-name);
  7886. return str;
  7887. }
  7888. IStringVal & CLocalWUStatistic::getDescription(IStringVal & str) const
  7889. {
  7890. str.set(p->queryProp("@desc"));
  7891. return str;
  7892. }
  7893. IStringVal & CLocalWUStatistic::getName(IStringVal & str) const
  7894. {
  7895. const char * name = p->queryProp("@name");
  7896. const char * sep1 = strchr(name, ';');
  7897. assertex(sep1);
  7898. const char * scope = sep1+1;
  7899. const char * sep2 = strchr(scope, ';');
  7900. assertex(sep2);
  7901. str.set(sep2+1);
  7902. return str;
  7903. }
  7904. IStringVal & CLocalWUStatistic::getScope(IStringVal & str) const
  7905. {
  7906. const char * name = p->queryProp("@name");
  7907. const char * sep1 = strchr(name, ';');
  7908. assertex(sep1);
  7909. const char * scope = sep1+1;
  7910. const char * sep2 = strchr(scope, ';');
  7911. assertex(sep2);
  7912. str.setLen(scope, sep2-scope);
  7913. return str;
  7914. }
  7915. StatisticMeasure CLocalWUStatistic::getKind() const
  7916. {
  7917. return (StatisticMeasure)getEnum(p, "@unit", queryStatMeasure);
  7918. }
  7919. unsigned __int64 CLocalWUStatistic::getValue() const
  7920. {
  7921. return p->getPropInt64("@value", 0);
  7922. }
  7923. unsigned __int64 CLocalWUStatistic::getCount() const
  7924. {
  7925. return p->getPropInt64("@count", 0);
  7926. }
  7927. unsigned __int64 CLocalWUStatistic::getMax() const
  7928. {
  7929. return p->getPropInt64("@max", 0);
  7930. }
  7931. //==========================================================================================
  7932. extern WORKUNIT_API ILocalWorkUnit * createLocalWorkUnit()
  7933. {
  7934. Owned<CLocalWorkUnit> cw = new CLocalWorkUnit("W_LOCAL", NULL, (ISecManager*)NULL, NULL);
  7935. ILocalWorkUnit* ret = QUERYINTERFACE(&cw->lockRemote(false), ILocalWorkUnit);
  7936. return ret;
  7937. }
  7938. extern WORKUNIT_API StringBuffer &exportWorkUnitToXML(const IConstWorkUnit *wu, StringBuffer &str, bool unpack, bool includeProgress)
  7939. {
  7940. const CLocalWorkUnit *w = QUERYINTERFACE(wu, const CLocalWorkUnit);
  7941. if (!w)
  7942. {
  7943. const CLockedWorkUnit *wl = QUERYINTERFACE(wu, const CLockedWorkUnit);
  7944. if (wl)
  7945. w = wl->c;
  7946. }
  7947. if (w)
  7948. {
  7949. Linked<IPropertyTree> p;
  7950. if (unpack||includeProgress)
  7951. p.setown(w->getUnpackedTree(includeProgress));
  7952. else
  7953. p.set(w->p);
  7954. toXML(p, str, 0, XML_Format|XML_SortTags);
  7955. }
  7956. else
  7957. str.append("Unrecognized workunit format");
  7958. return str;
  7959. }
  7960. extern WORKUNIT_API IStringVal& exportWorkUnitToXML(const IConstWorkUnit *wu, IStringVal &str, bool unpack, bool includeProgress)
  7961. {
  7962. StringBuffer x;
  7963. str.set(exportWorkUnitToXML(wu,x,unpack, includeProgress).str());
  7964. return str;
  7965. }
  7966. extern WORKUNIT_API void exportWorkUnitToXMLFile(const IConstWorkUnit *wu, const char * filename, unsigned extraXmlFlags, bool unpack, bool includeProgress)
  7967. {
  7968. const CLocalWorkUnit *w = QUERYINTERFACE(wu, const CLocalWorkUnit);
  7969. if (!w)
  7970. {
  7971. const CLockedWorkUnit *wl = QUERYINTERFACE(wu, const CLockedWorkUnit);
  7972. if (wl)
  7973. w = wl->c;
  7974. }
  7975. if (w)
  7976. {
  7977. Linked<IPropertyTree> p;
  7978. if (unpack||includeProgress)
  7979. p.setown(w->getUnpackedTree(includeProgress));
  7980. else
  7981. p.set(w->p);
  7982. saveXML(filename, p, 0, XML_Format|XML_SortTags|extraXmlFlags);
  7983. }
  7984. }
  7985. extern WORKUNIT_API void submitWorkUnit(const char *wuid, const char *username, const char *password)
  7986. {
  7987. MemoryBuffer buffer;
  7988. Owned<INamedQueueConnection> conn = createNamedQueueConnection(0); // MORE - security token?
  7989. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  7990. Owned<IWorkUnit> workunit = factory->updateWorkUnit(wuid);
  7991. assertex(workunit);
  7992. SCMStringBuffer token;
  7993. createToken(wuid, username, password, token);
  7994. workunit->setSecurityToken(token.str());
  7995. SCMStringBuffer clusterName;
  7996. workunit->getClusterName(clusterName);
  7997. if (!clusterName.length())
  7998. throw MakeStringException(WUERR_InvalidCluster, "No target cluster specified");
  7999. workunit->commit();
  8000. workunit.clear();
  8001. Owned<IConstWUClusterInfo> clusterInfo = getTargetClusterInfo(clusterName.str());
  8002. if (!clusterInfo)
  8003. throw MakeStringException(WUERR_InvalidCluster, "Unknown cluster %s", clusterName.str());
  8004. SCMStringBuffer serverQueue;
  8005. clusterInfo->getServerQueue(serverQueue);
  8006. assertex(serverQueue.length());
  8007. Owned<IJobQueue> queue = createJobQueue(serverQueue.str());
  8008. if (!queue.get())
  8009. throw MakeStringException(WUERR_InvalidQueue, "Could not create workunit queue");
  8010. IJobQueueItem *item = createJobQueueItem(wuid);
  8011. queue->enqueue(item);
  8012. }
  8013. extern WORKUNIT_API void abortWorkUnit(const char *wuid)
  8014. {
  8015. StringBuffer xpath("/WorkUnitAborts/");
  8016. xpath.append(wuid);
  8017. Owned<IRemoteConnection> acon = querySDS().connect(xpath.str(), myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE, SDS_LOCK_TIMEOUT);
  8018. acon->queryRoot()->setPropInt(NULL, 1);
  8019. }
  8020. extern WORKUNIT_API void secSubmitWorkUnit(const char *wuid, ISecManager &secmgr, ISecUser &secuser)
  8021. {
  8022. if (checkWuSecAccess(wuid, secmgr, &secuser, SecAccess_Write, "Submit", true, true))
  8023. submitWorkUnit(wuid, secuser.getName(), secuser.credentials().getPassword());
  8024. }
  8025. extern WORKUNIT_API void secAbortWorkUnit(const char *wuid, ISecManager &secmgr, ISecUser &secuser)
  8026. {
  8027. if (checkWuSecAccess(wuid, secmgr, &secuser, SecAccess_Write, "Submit", true, true))
  8028. abortWorkUnit(wuid);
  8029. }
  8030. extern WORKUNIT_API void submitWorkUnit(const char *wuid, ISecManager *secmgr, ISecUser *secuser)
  8031. {
  8032. if (secmgr && secuser)
  8033. return secSubmitWorkUnit(wuid, *secmgr, *secuser);
  8034. if (secuser)
  8035. return submitWorkUnit(wuid, secuser->getName(), secuser->credentials().getPassword());
  8036. submitWorkUnit(wuid, "", "");
  8037. }
  8038. extern WORKUNIT_API void abortWorkUnit(const char *wuid, ISecManager *secmgr, ISecUser *secuser)
  8039. {
  8040. if (secmgr && secuser)
  8041. return secAbortWorkUnit(wuid, *secmgr, *secuser);
  8042. abortWorkUnit(wuid);
  8043. }
  8044. bool CLocalWorkUnit::hasWorkflow() const
  8045. {
  8046. return p->hasProp("Workflow");
  8047. }
  8048. unsigned CLocalWorkUnit::queryEventScheduledCount() const
  8049. {
  8050. CriticalBlock block(crit);
  8051. return p->getPropInt("Workflow/@eventScheduledCount", 0);
  8052. }
  8053. void CLocalWorkUnit::incEventScheduledCount()
  8054. {
  8055. CriticalBlock block(crit);
  8056. p->setPropInt("Workflow/@eventScheduledCount", p->getPropInt("Workflow/@eventScheduledCount", 0)+1);
  8057. }
  8058. IPropertyTree * CLocalWorkUnit::queryWorkflowTree() const
  8059. {
  8060. CriticalBlock block(crit);
  8061. return p->queryPropTree("Workflow");
  8062. }
  8063. IConstWorkflowItemIterator* CLocalWorkUnit::getWorkflowItems() const
  8064. {
  8065. // For this to be legally called, we must have the read-able interface. So we are already locked for (at least) read.
  8066. CriticalBlock block(crit);
  8067. if(!workflowIteratorCached)
  8068. {
  8069. assertex(!workflowIterator);
  8070. Owned<IPropertyTree> s = p->getPropTree("Workflow");
  8071. if(s)
  8072. workflowIterator.setown(createWorkflowItemIterator(s));
  8073. workflowIteratorCached = true;
  8074. }
  8075. return workflowIterator.getLink();
  8076. }
  8077. IWorkflowItemArray * CLocalWorkUnit::getWorkflowClone() const
  8078. {
  8079. unsigned count = 0;
  8080. Owned<IConstWorkflowItemIterator> iter = getWorkflowItems();
  8081. for(iter->first(); iter->isValid(); iter->next())
  8082. count++;
  8083. Owned<IWorkflowItemArray> array = createWorkflowItemArray(count);
  8084. for(iter->first(); iter->isValid(); iter->next())
  8085. array->addClone(iter->query());
  8086. return array.getLink();
  8087. }
  8088. IWorkflowItem * CLocalWorkUnit::addWorkflowItem(unsigned wfid, WFType type, WFMode mode, unsigned success, unsigned failure, unsigned recovery, unsigned retriesAllowed, unsigned contingencyFor)
  8089. {
  8090. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  8091. CriticalBlock block(crit);
  8092. workflowIterator.clear();
  8093. workflowIteratorCached = false;
  8094. IPropertyTree * s = p->queryPropTree("Workflow");
  8095. if(!s)
  8096. s = p->addPropTree("Workflow", createPTree("Workflow"));
  8097. return createWorkflowItem(s, wfid, type, mode, success, failure, recovery, retriesAllowed, contingencyFor);
  8098. }
  8099. IWorkflowItemIterator * CLocalWorkUnit::updateWorkflowItems()
  8100. {
  8101. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  8102. CriticalBlock block(crit);
  8103. if(!workflowIterator)
  8104. {
  8105. IPropertyTree * s = p->queryPropTree("Workflow");
  8106. if(!s)
  8107. s = p->addPropTree("Workflow", createPTree("Workflow"));
  8108. workflowIterator.setown(createWorkflowItemIterator(s));
  8109. workflowIteratorCached = true;
  8110. }
  8111. return workflowIterator.getLink();
  8112. }
  8113. void CLocalWorkUnit::syncRuntimeWorkflow(IWorkflowItemArray * array)
  8114. {
  8115. Owned<IWorkflowItemIterator> iter = updateWorkflowItems();
  8116. Owned<IWorkflowItem> item;
  8117. for(iter->first(); iter->isValid(); iter->next())
  8118. {
  8119. item.setown(iter->get());
  8120. item->syncRuntimeData(array->queryWfid(item->queryWfid()));
  8121. }
  8122. workflowIterator.clear();
  8123. workflowIteratorCached = false;
  8124. }
  8125. void CLocalWorkUnit::resetWorkflow()
  8126. {
  8127. if (hasWorkflow())
  8128. {
  8129. Owned<IWorkflowItemIterator> iter = updateWorkflowItems();
  8130. Owned<IWorkflowItem> wf;
  8131. for(iter->first(); iter->isValid(); iter->next())
  8132. {
  8133. wf.setown(iter->get());
  8134. wf->reset();
  8135. }
  8136. workflowIterator.clear();
  8137. workflowIteratorCached = false;
  8138. }
  8139. }
  8140. void CLocalWorkUnit::schedule()
  8141. {
  8142. CriticalBlock block(crit);
  8143. if(queryEventScheduledCount() == 0) return;
  8144. switch(getState())
  8145. {
  8146. case WUStateCompleted:
  8147. setState(WUStateWait);
  8148. break;
  8149. case WUStateFailed:
  8150. case WUStateArchived:
  8151. case WUStateAborting:
  8152. case WUStateAborted:
  8153. case WUStateScheduled:
  8154. throw MakeStringException(WUERR_CannotSchedule, "Cannot schedule workunit in this state");
  8155. }
  8156. StringBuffer rootPath;
  8157. SCMStringBuffer clusterName;
  8158. getClusterName(clusterName);
  8159. rootPath.append("/Schedule/").append(clusterName.str());
  8160. Owned<IRemoteConnection> conn = querySDS().connect(rootPath.str(), myProcessSession(), RTM_LOCK_WRITE | RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  8161. Owned<IPropertyTree> root = conn->getRoot();
  8162. if(!root->hasChildren())
  8163. {
  8164. StringBuffer addPath;
  8165. addPath.append("/Schedulers/").append(clusterName.str());
  8166. Owned<IRemoteConnection> addConn = querySDS().connect(addPath.str(), myProcessSession(), RTM_LOCK_WRITE | RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  8167. }
  8168. char const * wuid = p->queryName();
  8169. StringBuffer xpath("*/*/");
  8170. ncnameEscape(wuid, xpath);
  8171. bool more;
  8172. do more = root->removeProp(xpath.str()); while(more);
  8173. Owned<IConstWorkflowItemIterator> iter = getWorkflowItems();
  8174. Owned<IWorkflowEvent> event;
  8175. Owned<IPropertyTree> branch1, branch2;
  8176. for(iter->first(); iter->isValid(); iter->next())
  8177. {
  8178. event.setown(iter->query()->getScheduleEvent());
  8179. if(!event) continue;
  8180. ncnameEscape(event->queryName(), xpath.clear());
  8181. ensurePTree(root, xpath.str());
  8182. branch1.setown(root->getPropTree(xpath.str()));
  8183. ncnameEscape(event->queryText(), xpath.clear());
  8184. ensurePTree(branch1, xpath.str());
  8185. branch2.setown(branch1->getPropTree(xpath.str()));
  8186. ncnameEscape(wuid, xpath.clear());
  8187. ensurePTree(branch2, xpath.str());
  8188. }
  8189. }
  8190. void CLocalWorkUnit::deschedule()
  8191. {
  8192. if(queryEventScheduledCount() == 0) return;
  8193. if(getState() == WUStateWait)
  8194. setState(WUStateCompleted);
  8195. doDescheduleWorkkunit(p->queryName());
  8196. }
  8197. mapEnums localFileUploadTypes[] = {
  8198. { UploadTypeFileSpray, "FileSpray" },
  8199. { UploadTypeWUResult, "WUResult" },
  8200. { UploadTypeWUResultCsv, "WUResultCsv" },
  8201. { UploadTypeWUResultXml, "WUResultXml" },
  8202. { UploadTypeSize, NULL }
  8203. };
  8204. class CLocalFileUpload : public CInterface, implements IConstLocalFileUpload
  8205. {
  8206. public:
  8207. CLocalFileUpload(IPropertyTree * _tree) : tree(_tree) {}
  8208. CLocalFileUpload(unsigned id, LocalFileUploadType type, char const * source, char const * destination, char const * eventTag)
  8209. {
  8210. tree.setown(createPTree());
  8211. tree->setPropInt("@id", id);
  8212. setEnum(tree, "@type", type, localFileUploadTypes);
  8213. tree->setProp("@source", source);
  8214. tree->setProp("@destination", destination);
  8215. if (eventTag)
  8216. tree->setProp("@eventTag", eventTag);
  8217. }
  8218. IMPLEMENT_IINTERFACE;
  8219. IPropertyTree * getTree() { return tree.getLink(); }
  8220. virtual unsigned queryID() const { return tree->getPropInt("@id"); }
  8221. virtual LocalFileUploadType queryType() const { return (LocalFileUploadType)getEnum(tree, "@type", localFileUploadTypes); }
  8222. virtual IStringVal & getSource(IStringVal & ret) const { ret.set(tree->queryProp("@source")); return ret; }
  8223. virtual IStringVal & getDestination(IStringVal & ret) const { ret.set(tree->queryProp("@destination")); return ret; }
  8224. virtual IStringVal & getEventTag(IStringVal & ret) const { if(tree->hasProp("@eventTag")) ret.set(tree->queryProp("@eventTag")); else ret.clear(); return ret; }
  8225. private:
  8226. Owned<IPropertyTree> tree;
  8227. };
  8228. class CLocalFileUploadIterator : public CInterface, implements IConstLocalFileUploadIterator
  8229. {
  8230. public:
  8231. CLocalFileUploadIterator(IPropertyTree * _tree) : tree(_tree), iter(tree->getElements("LocalFileUpload")) {}
  8232. IMPLEMENT_IINTERFACE;
  8233. bool first() { return iter->first(); }
  8234. bool isValid() { return iter->isValid(); }
  8235. bool next() { return iter->next(); }
  8236. IConstLocalFileUpload * get() { return new CLocalFileUpload(&iter->get()); }
  8237. private:
  8238. Owned<IPropertyTree> tree;
  8239. Owned<IPropertyTreeIterator> iter;
  8240. };
  8241. IConstLocalFileUploadIterator * CLocalWorkUnit::getLocalFileUploads() const
  8242. {
  8243. // For this to be legally called, we must have the read-able interface. So we are already locked for (at least) read.
  8244. CriticalBlock block(crit);
  8245. Owned<IPropertyTree> s = p->getPropTree("LocalFileUploads");
  8246. if(s)
  8247. return new CLocalFileUploadIterator(s.getClear());
  8248. else
  8249. return NULL;
  8250. }
  8251. bool CLocalWorkUnit::requiresLocalFileUpload() const
  8252. {
  8253. SCMStringBuffer dest;
  8254. Owned<IConstWUResult> result;
  8255. Owned<IConstLocalFileUploadIterator> iter(getLocalFileUploads());
  8256. if(!iter)
  8257. return false;
  8258. for(iter->first(); iter->isValid(); iter->next())
  8259. {
  8260. Owned<IConstLocalFileUpload> upload(iter->get());
  8261. switch(upload->queryType())
  8262. {
  8263. case UploadTypeWUResult:
  8264. case UploadTypeWUResultCsv:
  8265. case UploadTypeWUResultXml:
  8266. upload->getDestination(dest);
  8267. result.setown(getResultByName(dest.str()));
  8268. if(!result)
  8269. return true;
  8270. break;
  8271. default:
  8272. throw MakeStringException(WUERR_InvalidUploadFormat, "Unsupported local file upload type %s", getEnumText(upload->queryType(), localFileUploadTypes));
  8273. }
  8274. }
  8275. return false;
  8276. }
  8277. unsigned CLocalWorkUnit::addLocalFileUpload(LocalFileUploadType type, char const * source, char const * destination, char const * eventTag)
  8278. {
  8279. // For this to be legally called, we must have the write-able interface. So we are already locked for write.
  8280. CriticalBlock block(crit);
  8281. IPropertyTree * s = p->queryPropTree("LocalFileUploads");
  8282. if(!s)
  8283. s = p->addPropTree("LocalFileUploads", createPTree());
  8284. unsigned id = s->numChildren();
  8285. Owned<CLocalFileUpload> upload = new CLocalFileUpload(id, type, source, destination, eventTag);
  8286. s->addPropTree("LocalFileUpload", upload->getTree());
  8287. return id;
  8288. }
  8289. #if 0
  8290. void testConstWorkflow(IConstWorkflowItem * cwf, bool * okay, bool * dep)
  8291. {
  8292. DBGLOG("Test workflow const iface %u", cwf->queryWfid());
  8293. unsigned deps = 0;
  8294. Owned<IWorkflowDependencyIterator> diter;
  8295. switch(cwf->queryWfid())
  8296. {
  8297. case 1:
  8298. assertex(!cwf->isScheduled());
  8299. assertex(cwf->queryType() == WFTypeNormal);
  8300. assertex(cwf->queryState() == WFStateNull);
  8301. diter.setown(cwf->getDependencies());
  8302. for(diter->first(); diter->isValid(); diter->next())
  8303. deps++;
  8304. assertex(deps==0);
  8305. okay[0] = true;
  8306. break;
  8307. case 2:
  8308. assertex(!cwf->isScheduled());
  8309. assertex(cwf->queryType() == WFTypeRecovery);
  8310. assertex(cwf->queryState() == WFStateSkip);
  8311. okay[1] = true;
  8312. break;
  8313. case 3:
  8314. assertex(cwf->queryContingencyFor() == 4);
  8315. okay[2] = true;
  8316. break;
  8317. case 4:
  8318. assertex(cwf->isScheduled());
  8319. assertex(cwf->queryType() == WFTypeNormal);
  8320. assertex(cwf->queryState() == WFStateReqd);
  8321. assertex(cwf->querySuccess() == 0);
  8322. assertex(cwf->queryFailure() == 3);
  8323. assertex(cwf->queryRecovery() == 2);
  8324. assertex(cwf->queryRetriesAllowed() == 10);
  8325. assertex(cwf->queryRetriesRemaining() == 10);
  8326. diter.setown(cwf->getDependencies());
  8327. for(diter->first(); diter->isValid(); diter->next())
  8328. {
  8329. dep[diter->query()-1] = true;
  8330. deps++;
  8331. }
  8332. assertex(deps==2);
  8333. assertex(dep[0]);
  8334. assertex(dep[1]);
  8335. okay[3] = true;
  8336. break;
  8337. case 5:
  8338. assertex(cwf->isScheduled());
  8339. assertex(!cwf->isScheduledNow());
  8340. assertex(cwf->querySchedulePriority() == 75);
  8341. assertex(cwf->queryScheduleCount() == 5);
  8342. assertex(cwf->queryScheduleCountRemaining() == 5);
  8343. okay[4] = true;
  8344. break;
  8345. case 6:
  8346. assertex(cwf->isScheduled());
  8347. assertex(!cwf->isScheduledNow());
  8348. assertex(cwf->querySchedulePriority() == 25);
  8349. assertex(!cwf->hasScheduleCount());
  8350. okay[5] = true;
  8351. break;
  8352. default:
  8353. assertex(!"unknown wfid in test");
  8354. }
  8355. }
  8356. void testRuntimeWorkflow(IRuntimeWorkflowItem * rwf, bool * okay)
  8357. {
  8358. DBGLOG("Test workflow runtime iface %u", rwf->queryWfid());
  8359. switch(rwf->queryWfid())
  8360. {
  8361. case 1:
  8362. case 2:
  8363. case 3:
  8364. okay[rwf->queryWfid()-1] = true;
  8365. break;
  8366. case 4:
  8367. {
  8368. unsigned tries = 0;
  8369. while(rwf->testAndDecRetries())
  8370. tries++;
  8371. assertex(tries == 10);
  8372. assertex(rwf->queryRetriesRemaining() == 0);
  8373. rwf->setState(WFStateFail);
  8374. assertex(rwf->queryState() == WFStateFail);
  8375. rwf->reset();
  8376. assertex(rwf->queryRetriesRemaining() == 10);
  8377. assertex(rwf->queryState() == WFStateReqd);
  8378. }
  8379. okay[3] = true;
  8380. break;
  8381. case 5:
  8382. {
  8383. assertex(rwf->queryScheduleCountRemaining() == 5);
  8384. unsigned count = 0;
  8385. do count++; while(rwf->decAndTestScheduleCountRemaining());
  8386. assertex(count == 5);
  8387. assertex(rwf->queryScheduleCountRemaining() == 0);
  8388. rwf->reset();
  8389. assertex(rwf->queryScheduleCountRemaining() == 5);
  8390. }
  8391. okay[4] = true;
  8392. break;
  8393. case 6:
  8394. {
  8395. assertex(!rwf->hasScheduleCount());
  8396. unsigned count;
  8397. for(count=0; count<20; count++)
  8398. assertex(rwf->decAndTestScheduleCountRemaining());
  8399. }
  8400. okay[5] = true;
  8401. break;
  8402. default:
  8403. assertex(!"unknown wfid in test");
  8404. }
  8405. }
  8406. void testWorkflow()
  8407. {
  8408. DBGLOG("workunit.cpp : testWorkflow");
  8409. CLocalWorkUnit wu("W-WF-TEST", 0, 0, 0);
  8410. Owned<IWorkflowItem> wf;
  8411. wf.setown(wu.addWorkflowItem(1, WFTypeNormal, 0, 0, 0, 0, 0));
  8412. wf.setown(wu.addWorkflowItem(2, WFTypeRecovery, 0, 0, 0, 0, 0));
  8413. wf.setown(wu.addWorkflowItem(3, WFTypeFailure, 0, 0, 0, 0, 4));
  8414. wf.setown(wu.addWorkflowItem(4, WFTypeNormal, 0, 3, 2, 10, 0));
  8415. wf->setScheduledNow();
  8416. wf->addDependency(1);
  8417. wf.setown(wu.addWorkflowItem(5, WFTypeNormal, 0, 0, 0, 0, 0));
  8418. wf->setScheduledOn("test", "foo*");
  8419. wf->setSchedulePriority(75);
  8420. wf->setScheduleCount(5);
  8421. wf.setown(wu.addWorkflowItem(6, WFTypeNormal, 0, 0, 0, 0, 0));
  8422. wf->setScheduledOn("test", "bar*");
  8423. wf->setSchedulePriority(25);
  8424. unsigned const n = 6;
  8425. bool okay[n];
  8426. bool dep[n];
  8427. unsigned i;
  8428. for(i=0; i<n; i++)
  8429. okay[i] = dep[i] = 0;
  8430. Owned<IConstWorkflowItemIterator> citer(wu.getWorkflowItems());
  8431. for(citer->first(); citer->isValid(); citer->next())
  8432. testConstWorkflow(citer->query(), okay, dep);
  8433. for(i=0; i<n; i++)
  8434. {
  8435. assertex(okay[i]);
  8436. okay[i] = false;
  8437. }
  8438. Owned<IWorkflowItemIterator> miter(wu.updateWorkflowItems());
  8439. for(miter->first(); miter->isValid(); miter->next())
  8440. {
  8441. Owned<IRuntimeWorkflowItem> rwf(miter->get());
  8442. testRuntimeWorkflow(rwf, okay);
  8443. }
  8444. for(i=0; i<n; i++)
  8445. {
  8446. assertex(okay[i]);
  8447. okay[i] = dep[i] = false;
  8448. }
  8449. Owned<IWorkflowItemArray> array(wu.getWorkflowClone());
  8450. unsigned wfid;
  8451. for(wfid = 1; array->isValid(wfid); wfid++)
  8452. testConstWorkflow(&array->queryWfid(wfid), okay, dep);
  8453. for(i=0; i<n; i++)
  8454. {
  8455. assertex(okay[i]);
  8456. okay[i] = false;
  8457. }
  8458. for(wfid = 1; array->isValid(wfid); wfid++)
  8459. testRuntimeWorkflow(&array->queryWfid(wfid), okay);
  8460. for(i=0; i<n; i++)
  8461. {
  8462. assertex(okay[i]);
  8463. okay[i] = false;
  8464. }
  8465. }
  8466. #endif
  8467. //------------------------------------------------------------------------------------------
  8468. class WorkUnitWaiter : public CInterface, implements ISDSSubscription, implements IAbortHandler
  8469. {
  8470. Semaphore changed;
  8471. SubscriptionId change;
  8472. public:
  8473. IMPLEMENT_IINTERFACE;
  8474. WorkUnitWaiter(const char *xpath)
  8475. {
  8476. change = querySDS().subscribe(xpath, *this, false);
  8477. aborted = false;
  8478. }
  8479. ~WorkUnitWaiter()
  8480. {
  8481. assertex(change==0);
  8482. }
  8483. void notify(SubscriptionId id, const char *xpath, SDSNotifyFlags flags, unsigned valueLen, const void *valueData)
  8484. {
  8485. changed.signal();
  8486. }
  8487. bool wait(unsigned timeout)
  8488. {
  8489. return changed.wait(timeout) && !aborted;
  8490. }
  8491. bool onAbort()
  8492. {
  8493. aborted = true;
  8494. changed.signal();
  8495. return false;
  8496. }
  8497. void unsubscribe()
  8498. {
  8499. querySDS().unsubscribe(change);
  8500. change = 0;
  8501. }
  8502. bool aborted;
  8503. };
  8504. static WUState _waitForWorkUnit(const char * wuid, unsigned timeout, bool compiled, bool returnOnWaitState)
  8505. {
  8506. StringBuffer wuRoot;
  8507. getXPath(wuRoot, wuid);
  8508. Owned<WorkUnitWaiter> waiter = new WorkUnitWaiter(wuRoot.str());
  8509. LocalIAbortHandler abortHandler(*waiter);
  8510. WUState ret = WUStateUnknown;
  8511. Owned<IRemoteConnection> conn = querySDS().connect(wuRoot.str(), myProcessSession(), 0, SDS_LOCK_TIMEOUT);
  8512. if (conn)
  8513. {
  8514. unsigned start = msTick();
  8515. loop
  8516. {
  8517. ret = (WUState) getEnum(conn->queryRoot(), "@state", states);
  8518. switch (ret)
  8519. {
  8520. case WUStateCompiled:
  8521. case WUStateUploadingFiles:
  8522. if (!compiled)
  8523. break;
  8524. // fall into
  8525. case WUStateCompleted:
  8526. case WUStateFailed:
  8527. case WUStateAborted:
  8528. waiter->unsubscribe();
  8529. return ret;
  8530. case WUStateWait:
  8531. if(returnOnWaitState)
  8532. {
  8533. waiter->unsubscribe();
  8534. return ret;
  8535. }
  8536. break;
  8537. case WUStateCompiling:
  8538. case WUStateRunning:
  8539. case WUStateDebugPaused:
  8540. case WUStateDebugRunning:
  8541. case WUStateBlocked:
  8542. case WUStateAborting:
  8543. if (queryDaliServerVersion().compare("2.1")>=0)
  8544. {
  8545. SessionId agent = conn->queryRoot()->getPropInt64("@agentSession", -1);
  8546. if((agent>0) && querySessionManager().sessionStopped(agent, 0))
  8547. {
  8548. waiter->unsubscribe();
  8549. conn->reload();
  8550. ret = (WUState) getEnum(conn->queryRoot(), "@state", states);
  8551. bool isEcl = false;
  8552. switch (ret)
  8553. {
  8554. case WUStateCompiling:
  8555. isEcl = true;
  8556. // drop into
  8557. case WUStateRunning:
  8558. case WUStateBlocked:
  8559. ret = WUStateFailed;
  8560. break;
  8561. case WUStateAborting:
  8562. ret = WUStateAborted;
  8563. break;
  8564. default:
  8565. return ret;
  8566. }
  8567. WARNLOG("_waitForWorkUnit terminated: %"I64F"d state = %d",(__int64)agent,(int)ret);
  8568. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  8569. Owned<IWorkUnit> wu = factory->updateWorkUnit(wuid);
  8570. wu->setState(ret);
  8571. Owned<IWUException> e = wu->createException();
  8572. e->setExceptionCode(isEcl ? 1001 : 1000);
  8573. e->setExceptionMessage(isEcl ? "EclServer terminated unexpectedly" : "Workunit terminated unexpectedly");
  8574. return ret;
  8575. }
  8576. }
  8577. break;
  8578. }
  8579. unsigned waited = msTick() - start;
  8580. if (timeout==-1)
  8581. {
  8582. waiter->wait(20000); // recheck state every 20 seconds even if no timeout, in case eclagent has crashed.
  8583. if (waiter->aborted)
  8584. {
  8585. ret = WUStateUnknown; // MORE - throw an exception?
  8586. break;
  8587. }
  8588. }
  8589. else if (waited > timeout || !waiter->wait(timeout-waited))
  8590. {
  8591. ret = WUStateUnknown; // MORE - throw an exception?
  8592. break;
  8593. }
  8594. conn->reload();
  8595. }
  8596. }
  8597. waiter->unsubscribe();
  8598. return ret;
  8599. }
  8600. extern WUState waitForWorkUnitToComplete(const char * wuid, int timeout, bool returnOnWaitState)
  8601. {
  8602. return _waitForWorkUnit(wuid, (unsigned)timeout, false, returnOnWaitState);
  8603. }
  8604. extern WORKUNIT_API WUState secWaitForWorkUnitToComplete(const char * wuid, ISecManager &secmgr, ISecUser &secuser, int timeout, bool returnOnWaitState)
  8605. {
  8606. if (checkWuSecAccess(wuid, secmgr, &secuser, SecAccess_Read, "Wait for Complete", false, true))
  8607. return waitForWorkUnitToComplete(wuid, timeout, returnOnWaitState);
  8608. return WUStateUnknown;
  8609. }
  8610. extern bool waitForWorkUnitToCompile(const char * wuid, int timeout)
  8611. {
  8612. switch(_waitForWorkUnit(wuid, (unsigned)timeout, true, true))
  8613. {
  8614. case WUStateCompiled:
  8615. case WUStateCompleted:
  8616. case WUStateWait:
  8617. case WUStateUploadingFiles:
  8618. return true;
  8619. default:
  8620. return false;
  8621. }
  8622. }
  8623. extern WORKUNIT_API bool secWaitForWorkUnitToCompile(const char * wuid, ISecManager &secmgr, ISecUser &secuser, int timeout)
  8624. {
  8625. if (checkWuSecAccess(wuid, secmgr, &secuser, SecAccess_Read, "Wait for Compile", false, true))
  8626. return waitForWorkUnitToCompile(wuid, timeout);
  8627. return false;
  8628. }
  8629. extern WORKUNIT_API bool secDebugWorkunit(const char * wuid, ISecManager &secmgr, ISecUser &secuser, const char *command, StringBuffer &response)
  8630. {
  8631. if (strnicmp(command, "<debug:", 7) == 0 && checkWuSecAccess(wuid, secmgr, &secuser, SecAccess_Read, "Debug", false, true))
  8632. {
  8633. Owned<IConstWorkUnit> wu = factory->secOpenWorkUnit(wuid, false, &secmgr, &secuser);
  8634. SCMStringBuffer ip;
  8635. unsigned port;
  8636. port = wu->getDebugAgentListenerPort();
  8637. wu->getDebugAgentListenerIP(ip);
  8638. SocketEndpoint debugEP(ip.str(), port);
  8639. Owned<ISocket> socket = ISocket::connect_timeout(debugEP, 1000);
  8640. unsigned len = (size32_t)strlen(command);
  8641. unsigned revlen = len;
  8642. _WINREV(revlen);
  8643. socket->write(&revlen, sizeof(revlen));
  8644. socket->write(command, len);
  8645. for (;;)
  8646. {
  8647. socket->read(&len, sizeof(len));
  8648. _WINREV(len);
  8649. if (len == 0)
  8650. break;
  8651. if (len & 0x80000000)
  8652. {
  8653. throwUnexpected();
  8654. }
  8655. char * mem = (char*) response.reserve(len);
  8656. socket->read(mem, len);
  8657. }
  8658. return true;
  8659. }
  8660. return false;
  8661. }
  8662. void updateSuppliedXmlParams(IWorkUnit * w)
  8663. {
  8664. Owned<const IPropertyTree> params = w->getXmlParams();
  8665. if (!params)
  8666. return;
  8667. Owned<IPropertyTreeIterator> elems = params->getElements("*");
  8668. ForEach(*elems)
  8669. {
  8670. IPropertyTree & curVal = elems->query();
  8671. const char *name = curVal.queryName();
  8672. Owned<IWUResult> r = updateWorkUnitResult(w, name, -1);
  8673. if (r)
  8674. {
  8675. StringBuffer s;
  8676. if (r->isResultScalar() && !curVal.hasChildren())
  8677. {
  8678. curVal.getProp(".", s);
  8679. r->setResultXML(s);
  8680. r->setResultStatus(ResultStatusSupplied);
  8681. }
  8682. else
  8683. {
  8684. toXML(&curVal, s);
  8685. bool isSet = (curVal.hasProp("Item") || curVal.hasProp("string"));
  8686. r->setResultRaw(s.length(), s.str(), isSet ? ResultFormatXmlSet : ResultFormatXml);
  8687. }
  8688. }
  8689. else
  8690. DBGLOG("WARNING: no matching variable in workunit for input parameter %s", name);
  8691. }
  8692. }
  8693. IWUResult * updateWorkUnitResult(IWorkUnit * w, const char *name, unsigned sequence)
  8694. {
  8695. switch ((int)sequence)
  8696. {
  8697. case ResultSequenceStored:
  8698. return w->updateVariableByName(name);
  8699. case ResultSequencePersist:
  8700. return w->updateGlobalByName(name);
  8701. case ResultSequenceInternal:
  8702. case ResultSequenceOnce:
  8703. return w->updateTemporaryByName(name);
  8704. default:
  8705. return w->updateResultBySequence(sequence);
  8706. }
  8707. }
  8708. IConstWUResult * getWorkUnitResult(IConstWorkUnit * w, const char *name, unsigned sequence)
  8709. {
  8710. switch ((int)sequence)
  8711. {
  8712. case ResultSequenceStored:
  8713. return w->getVariableByName(name);
  8714. case ResultSequencePersist:
  8715. return w->getGlobalByName(name);
  8716. case ResultSequenceInternal:
  8717. case ResultSequenceOnce:
  8718. return w->getTemporaryByName(name);
  8719. default:
  8720. if (name && name[0])
  8721. return w->getResultByName(name);//name takes precedence over sequence
  8722. else
  8723. return w->getResultBySequence(sequence);
  8724. }
  8725. }
  8726. extern WORKUNIT_API bool getWorkUnitCreateTime(const char *wuid,CDateTime &time)
  8727. {
  8728. if (wuid) {
  8729. char prefchar;
  8730. unsigned year,month,day,hour,min,sec;
  8731. if (sscanf(wuid, "%c%4u%2u%2u-%2u%2u%2u", &prefchar, &year, &month, &day, &hour, &min, &sec)==7) {
  8732. time.set(year, month, day, hour, min, sec, 0, true);
  8733. // time.setDate(year, month, day);
  8734. // time.setTime(hour, min, sec, 0, true); // for some reason time is local
  8735. return true;
  8736. }
  8737. }
  8738. return false;
  8739. }
  8740. extern WORKUNIT_API IStringVal& createToken(const char *wuid, const char *user, const char *password, IStringVal &str)
  8741. {
  8742. StringBuffer wu, token("X");
  8743. wu.append(wuid).append(';').append(user).append(';').append(password);
  8744. encrypt(token,wu.str());
  8745. str.set(token.str());
  8746. return str;
  8747. }
  8748. // This will be replaced by something more secure!
  8749. extern WORKUNIT_API void extractToken(const char *token, const char *wuid, IStringVal &user, IStringVal &password)
  8750. {
  8751. if (token && *token)
  8752. {
  8753. StringBuffer wu;
  8754. decrypt(wu, token+1);
  8755. const char *finger = strchr(wu.str(),';');
  8756. if (finger && strnicmp(wuid, wu.str(), finger-wu.str())==0)
  8757. {
  8758. const char *finger1 = strchr(++finger,';');
  8759. if(finger1)
  8760. {
  8761. user.setLen(finger, (size32_t)(finger1-finger));
  8762. password.setLen(++finger1, (size32_t)(wu.str() + wu.length() - finger1));
  8763. return;
  8764. }
  8765. }
  8766. throw MakeStringException(WUERR_InvalidSecurityToken, "Invalid call to extractToken");
  8767. }
  8768. }
  8769. extern WORKUNIT_API WUState getWorkUnitState(const char* state)
  8770. {
  8771. return (WUState) getEnum(state, states);
  8772. }
  8773. const LogMsgCategory MCschedconn = MCprogress(1000); // Category used to inform about schedule synchronization
  8774. class CWorkflowScheduleConnection : public CInterface, implements IWorkflowScheduleConnection
  8775. {
  8776. public:
  8777. CWorkflowScheduleConnection(char const * wuid)
  8778. {
  8779. basexpath.append("/WorkflowSchedule/").append(wuid);
  8780. flagxpath.append(basexpath.str()).append("/Active");
  8781. }
  8782. IMPLEMENT_IINTERFACE;
  8783. virtual void lock()
  8784. {
  8785. LOG(MCschedconn, "Locking base schedule connection");
  8786. baseconn.setown(querySDS().connect(basexpath.str(), myProcessSession(), RTM_CREATE_QUERY | RTM_LOCK_WRITE, INFINITE));
  8787. if(!baseconn)
  8788. throw MakeStringException(WUERR_ScheduleLockFailed, "Could not get base workflow schedule lock");
  8789. }
  8790. virtual void unlock()
  8791. {
  8792. LOG(MCschedconn, "Unlocking base schedule connection");
  8793. baseconn.clear();
  8794. }
  8795. virtual void setActive()
  8796. {
  8797. LOG(MCschedconn, "Setting active flag in schedule connection");
  8798. flagconn.setown(querySDS().connect(flagxpath.str(), myProcessSession(), RTM_CREATE | RTM_LOCK_WRITE | RTM_DELETE_ON_DISCONNECT, INFINITE));
  8799. if(!flagconn)
  8800. throw MakeStringException(WUERR_ScheduleLockFailed, "Could not get active workflow schedule lock");
  8801. }
  8802. virtual void resetActive()
  8803. {
  8804. LOG(MCschedconn, "Resetting active flag in schedule connection");
  8805. flagconn.clear();
  8806. }
  8807. virtual bool queryActive()
  8808. {
  8809. return baseconn->queryRoot()->hasProp("Active");
  8810. }
  8811. virtual bool pull(IWorkflowItemArray * workflow)
  8812. {
  8813. assertex(baseconn);
  8814. Owned<IPropertyTree> root = baseconn->getRoot();
  8815. Owned<IPropertyTree> eventQueue = root->getPropTree("EventQueue");
  8816. if(!eventQueue) return false;
  8817. if(!eventQueue->hasProp("Item")) return false;
  8818. {
  8819. Owned<IPropertyTreeIterator> eventItems = eventQueue->getElements("Item");
  8820. Owned<IPropertyTree> eventItem;
  8821. Owned<IRuntimeWorkflowItemIterator> wfItems = workflow->getSequenceIterator();
  8822. Owned<IRuntimeWorkflowItem> wfItem;
  8823. for(eventItems->first(); eventItems->isValid(); eventItems->next())
  8824. {
  8825. eventItem.setown(&eventItems->get());
  8826. const char * eventName = eventItem->queryProp("@name");
  8827. const char * eventText = eventItem->queryProp("@text");
  8828. for(wfItems->first(); wfItems->isValid(); wfItems->next())
  8829. {
  8830. wfItem.setown(wfItems->get());
  8831. if(wfItem->queryState() != WFStateWait)
  8832. continue;
  8833. Owned<IWorkflowEvent> targetEvent = wfItem->getScheduleEvent();
  8834. if(!targetEvent || !targetEvent->matches(eventName, eventText))
  8835. continue;
  8836. wfItem->setEvent(eventName, eventText);
  8837. wfItem->setState(WFStateReqd);
  8838. resetDependentsState(workflow, *wfItem);
  8839. }
  8840. }
  8841. }
  8842. bool more;
  8843. do
  8844. more = eventQueue->removeProp("Item");
  8845. while(more);
  8846. return true;
  8847. }
  8848. virtual void push(char const * name, char const * text)
  8849. {
  8850. assertex(baseconn);
  8851. Owned<IPropertyTree> root = baseconn->getRoot();
  8852. ensurePTree(root, "EventQueue");
  8853. Owned<IPropertyTree> eventQueue = root->getPropTree("EventQueue");
  8854. Owned<IPropertyTree> eventItem = createPTree();
  8855. eventItem->setProp("@name", name);
  8856. eventItem->setProp("@text", text);
  8857. eventQueue->addPropTree("Item", eventItem.getLink());
  8858. }
  8859. private:
  8860. void resetItemStateAndDependents(IWorkflowItemArray * workflow, unsigned wfid) const
  8861. {
  8862. if (wfid)
  8863. resetItemStateAndDependents(workflow, workflow->queryWfid(wfid));
  8864. }
  8865. void resetItemStateAndDependents(IWorkflowItemArray * workflow, IRuntimeWorkflowItem & item) const
  8866. {
  8867. switch(item.queryState())
  8868. {
  8869. case WFStateDone:
  8870. case WFStateFail:
  8871. {
  8872. item.setState(WFStateNull);
  8873. resetItemStateAndDependents(workflow, item.queryPersistWfid());
  8874. resetDependentsState(workflow, item);
  8875. break;
  8876. }
  8877. }
  8878. }
  8879. void resetDependentsState(IWorkflowItemArray * workflow, IRuntimeWorkflowItem & item) const
  8880. {
  8881. Owned<IWorkflowDependencyIterator> iter(item.getDependencies());
  8882. for(iter->first(); iter->isValid(); iter->next())
  8883. {
  8884. IRuntimeWorkflowItem & dep = workflow->queryWfid(iter->query());
  8885. resetItemStateAndDependents(workflow, dep);
  8886. }
  8887. }
  8888. private:
  8889. StringBuffer basexpath;
  8890. StringBuffer flagxpath;
  8891. Owned<IRemoteConnection> baseconn;
  8892. Owned<IRemoteConnection> flagconn;
  8893. };
  8894. extern WORKUNIT_API IWorkflowScheduleConnection * getWorkflowScheduleConnection(char const * wuid)
  8895. {
  8896. return new CWorkflowScheduleConnection(wuid);
  8897. }
  8898. extern WORKUNIT_API IExtendedWUInterface * queryExtendedWU(IWorkUnit * wu)
  8899. {
  8900. return QUERYINTERFACE(wu, IExtendedWUInterface);
  8901. }
  8902. extern WORKUNIT_API void addExceptionToWorkunit(IWorkUnit * wu, WUExceptionSeverity severity, const char * source, unsigned code, const char * text, const char * filename, unsigned lineno, unsigned column)
  8903. {
  8904. Owned<IWUException> we = wu->createException();
  8905. we->setSeverity(severity);
  8906. we->setExceptionMessage(text);
  8907. if (source)
  8908. we->setExceptionSource(source);
  8909. if (code)
  8910. we->setExceptionCode(code);
  8911. if (filename)
  8912. we->setExceptionFileName(filename);
  8913. if (lineno)
  8914. {
  8915. we->setExceptionLineNo(lineno);
  8916. if (column)
  8917. we->setExceptionColumn(lineno);
  8918. }
  8919. }
  8920. const char * skipLeadingXml(const char * text)
  8921. {
  8922. if (!text)
  8923. return NULL;
  8924. //skip utf8 BOM, probably excessive
  8925. if (memcmp(text, UTF8_BOM, 3) == 0)
  8926. text += 3;
  8927. loop
  8928. {
  8929. if (isspace(*text))
  8930. text++;
  8931. else if (text[0] == '<' && text[1] == '?')
  8932. {
  8933. text += 2;
  8934. loop
  8935. {
  8936. if (!*text) break;
  8937. if (text[0] == '?' && text[1] == '>')
  8938. {
  8939. text += 2;
  8940. break;
  8941. }
  8942. text++;
  8943. }
  8944. }
  8945. else if (text[0] == '<' && text[1] == '!' && text[2] == '-' && text[3] == '-')
  8946. {
  8947. text += 4;
  8948. loop
  8949. {
  8950. if (!*text) break;
  8951. if (text[0] == '-' && text[1] == '-' && text[2] == '>')
  8952. {
  8953. text += 3;
  8954. break;
  8955. }
  8956. text++;
  8957. }
  8958. }
  8959. else
  8960. break;
  8961. }
  8962. return text;
  8963. }
  8964. extern WORKUNIT_API bool isArchiveQuery(const char * text)
  8965. {
  8966. text = skipLeadingXml(text);
  8967. if (!text)
  8968. return false;
  8969. const char * archivePrefix = "<Archive";
  8970. return memicmp(text, archivePrefix, strlen(archivePrefix)) == 0;
  8971. }
  8972. extern WORKUNIT_API bool isQueryManifest(const char * text)
  8973. {
  8974. text = skipLeadingXml(text);
  8975. if (!text)
  8976. return false;
  8977. const char * manifestPrefix = "<Manifest";
  8978. return memicmp(text, manifestPrefix, strlen(manifestPrefix)) == 0;
  8979. }
  8980. //------------------------------------------------------------------------------
  8981. // Named Alias helper function
  8982. static IPropertyTree * resolveQueryByDll(IPropertyTree * queryRegistry, const char * dll)
  8983. {
  8984. StringBuffer xpath;
  8985. xpath.append("Query[@dll=\"").append(dll).append("\"]");
  8986. return queryRegistry->getPropTree(xpath);
  8987. }
  8988. static IPropertyTree * resolveQueryByWuid(IPropertyTree * queryRegistry, const char * wuid)
  8989. {
  8990. StringBuffer xpath;
  8991. xpath.append("Query[@wuid=\"").append(wuid).append("\"]");
  8992. return queryRegistry->getPropTree(xpath);
  8993. }
  8994. static void clearAliases(IPropertyTree * queryRegistry, const char * id)
  8995. {
  8996. StringBuffer lcId(id);
  8997. lcId.toLowerCase();
  8998. StringBuffer xpath;
  8999. xpath.append("Alias[@id=\"").append(lcId).append("\"]");
  9000. Owned<IPropertyTreeIterator> iter = queryRegistry->getElements(xpath);
  9001. ForEach(*iter)
  9002. {
  9003. queryRegistry->removeProp(xpath.str());
  9004. }
  9005. }
  9006. IPropertyTree * addNamedQuery(IPropertyTree * queryRegistry, const char * name, const char * wuid, const char * dll, bool library, const char *userid, const char *snapshot)
  9007. {
  9008. StringBuffer lcName(name);
  9009. lcName.toLowerCase();
  9010. StringBuffer xpath;
  9011. xpath.append("Query[@name=\"").append(lcName.str()).append("\"]");
  9012. Owned<IPropertyTreeIterator> iter = queryRegistry->getElements(xpath);
  9013. unsigned seq = 1;
  9014. ForEach(*iter)
  9015. {
  9016. IPropertyTree &item = iter->query();
  9017. const char *thisWuid = item.queryProp("@wuid");
  9018. if (strieq(wuid, thisWuid))
  9019. return &item;
  9020. unsigned thisSeq = item.getPropInt("@seq");
  9021. if (thisSeq >= seq)
  9022. seq = thisSeq + 1;
  9023. }
  9024. StringBuffer id;
  9025. id.append(lcName).append(".").append(seq);
  9026. IPropertyTree * newEntry = createPTree("Query", ipt_caseInsensitive);
  9027. newEntry->setProp("@name", lcName);
  9028. newEntry->setProp("@wuid", wuid);
  9029. newEntry->setProp("@dll", dll);
  9030. newEntry->setProp("@id", id);
  9031. newEntry->setPropInt("@seq", seq);
  9032. if (library)
  9033. newEntry->setPropBool("@isLibrary", true);
  9034. if (userid && *userid)
  9035. newEntry->setProp("@publishedBy", userid);
  9036. if (snapshot && *snapshot)
  9037. newEntry->setProp("@snapshot", snapshot);
  9038. return queryRegistry->addPropTree("Query", newEntry);
  9039. }
  9040. void removeNamedQuery(IPropertyTree * queryRegistry, const char * id)
  9041. {
  9042. StringBuffer lcId(id);
  9043. lcId.toLowerCase();
  9044. clearAliases(queryRegistry, lcId);
  9045. StringBuffer xpath;
  9046. xpath.append("Query[@id=\"").append(lcId).append("\"]");
  9047. queryRegistry->removeProp(xpath);
  9048. }
  9049. void removeDllFromNamedQueries(IPropertyTree * queryRegistry, const char * dll)
  9050. {
  9051. Owned<IPropertyTree> match = resolveQueryByDll(queryRegistry, dll);
  9052. if (!match)
  9053. return;
  9054. clearAliases(queryRegistry, match->queryProp("@id"));
  9055. queryRegistry->removeTree(match);
  9056. }
  9057. void removeWuidFromNamedQueries(IPropertyTree * queryRegistry, const char * wuid)
  9058. {
  9059. Owned<IPropertyTree> match = resolveQueryByWuid(queryRegistry, wuid);
  9060. if (!match)
  9061. return;
  9062. clearAliases(queryRegistry, match->queryProp("@id"));
  9063. queryRegistry->removeTree(match);
  9064. }
  9065. void removeAliasesFromNamedQuery(IPropertyTree * queryRegistry, const char * id)
  9066. {
  9067. clearAliases(queryRegistry, id);
  9068. }
  9069. void setQueryAlias(IPropertyTree * queryRegistry, const char * name, const char * value)
  9070. {
  9071. StringBuffer lcName(name);
  9072. lcName.toLowerCase();
  9073. StringBuffer xpath;
  9074. xpath.append("Alias[@name=\"").append(lcName).append("\"]");
  9075. IPropertyTree * match = queryRegistry->queryPropTree(xpath);
  9076. if (!match)
  9077. {
  9078. IPropertyTree * newEntry = createPTree("Alias");
  9079. newEntry->setProp("@name", lcName);
  9080. match = queryRegistry->addPropTree("Alias", newEntry);
  9081. }
  9082. match->setProp("@id", value);
  9083. }
  9084. extern WORKUNIT_API IPropertyTree * getQueryById(IPropertyTree * queryRegistry, const char *queryid)
  9085. {
  9086. if (!queryRegistry || !queryid)
  9087. return NULL;
  9088. StringBuffer xpath;
  9089. xpath.append("Query[@id=\"").append(queryid).append("\"]");
  9090. return queryRegistry->getPropTree(xpath);
  9091. }
  9092. extern WORKUNIT_API IPropertyTree * getQueryById(const char *queryset, const char *queryid, bool readonly)
  9093. {
  9094. Owned<IPropertyTree> queryRegistry = getQueryRegistry(queryset, readonly);
  9095. return getQueryById(queryRegistry, queryid);
  9096. }
  9097. extern WORKUNIT_API IPropertyTree * resolveQueryAlias(IPropertyTree * queryRegistry, const char * alias)
  9098. {
  9099. if (!queryRegistry || !alias)
  9100. return NULL;
  9101. StringBuffer xpath;
  9102. unsigned cnt = 0;
  9103. StringBuffer lc(alias);
  9104. const char * search = lc.toLowerCase().str();
  9105. loop
  9106. {
  9107. xpath.set("Alias[@name='").append(search).append("']/@id");
  9108. const char * queryId = queryRegistry->queryProp(xpath);
  9109. if (!queryId)
  9110. break;
  9111. //Check for too many alias indirections.
  9112. if (cnt++ > 10)
  9113. return NULL;
  9114. search = lc.set(queryId).toLowerCase().str();
  9115. }
  9116. return getQueryById(queryRegistry, search);
  9117. }
  9118. extern WORKUNIT_API IPropertyTree * resolveQueryAlias(const char *queryset, const char *alias, bool readonly)
  9119. {
  9120. Owned<IPropertyTree> queryRegistry = getQueryRegistry(queryset, readonly);
  9121. return resolveQueryAlias(queryRegistry, alias);
  9122. }
  9123. void setQuerySuspendedState(IPropertyTree * queryRegistry, const char *id, bool suspend, const char *userid)
  9124. {
  9125. StringBuffer lcId(id);
  9126. lcId.toLowerCase();
  9127. StringBuffer xpath;
  9128. xpath.append("Query[@id=\"").append(lcId).append("\"]");
  9129. IPropertyTree *tree = queryRegistry->queryPropTree(xpath);
  9130. if (tree)
  9131. {
  9132. if (tree->getPropBool("@suspended", false) == suspend)
  9133. return;
  9134. if (suspend)
  9135. {
  9136. tree->addPropBool("@suspended", true);
  9137. if (userid && *userid)
  9138. tree->addProp("@suspendedBy", userid);
  9139. }
  9140. else
  9141. {
  9142. tree->removeProp("@suspended");
  9143. tree->removeProp("@suspendedBy");
  9144. }
  9145. }
  9146. else
  9147. throw MakeStringException((suspend)? QUERRREG_SUSPEND : QUERRREG_UNSUSPEND, "Modifying query suspended state failed. Could not find query %s", id);
  9148. }
  9149. void setQueryCommentForNamedQuery(IPropertyTree * queryRegistry, const char *id, const char *queryComment)
  9150. {
  9151. if (queryComment)
  9152. {
  9153. StringBuffer lcId(id);
  9154. lcId.toLowerCase();
  9155. StringBuffer xpath;
  9156. xpath.append("Query[@id=\"").append(lcId).append("\"]");
  9157. IPropertyTree *tree = queryRegistry->queryPropTree(xpath);
  9158. if (tree)
  9159. tree->setProp("@queryComment", queryComment);
  9160. else
  9161. throw MakeStringException(QUERRREG_COMMENT, "Could not find query %s", id);
  9162. }
  9163. }
  9164. extern WORKUNIT_API IPropertyTree * getQueryRegistryRoot()
  9165. {
  9166. Owned<IRemoteConnection> globalLock = querySDS().connect("/QuerySets/", myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT);
  9167. if (!globalLock)
  9168. return NULL;
  9169. //Only lock the branch for the target we're interested in.
  9170. StringBuffer xpath;
  9171. xpath.append("/QuerySets");
  9172. Owned<IRemoteConnection> conn = querySDS().connect(xpath.str(), myProcessSession(), RTM_LOCK_READ, SDS_LOCK_TIMEOUT);
  9173. if (conn)
  9174. return conn->getRoot();
  9175. else
  9176. return NULL;
  9177. }
  9178. extern WORKUNIT_API IPropertyTree * getQueryRegistry(const char * wsEclId, bool readonly)
  9179. {
  9180. Owned<IRemoteConnection> globalLock = querySDS().connect("/QuerySets/", myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  9181. //Only lock the branch for the target we're interested in.
  9182. StringBuffer xpath;
  9183. xpath.append("/QuerySets/QuerySet[@id=\"").append(wsEclId).append("\"]");
  9184. Owned<IRemoteConnection> conn = querySDS().connect(xpath.str(), myProcessSession(), readonly ? RTM_LOCK_READ : RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  9185. if (!conn)
  9186. {
  9187. if (readonly)
  9188. return NULL;
  9189. Owned<IPropertyTree> querySet = createPTree();
  9190. querySet->setProp("@id", wsEclId);
  9191. globalLock->queryRoot()->addPropTree("QuerySet", querySet.getClear());
  9192. globalLock->commit();
  9193. conn.setown(querySDS().connect(xpath.str(), myProcessSession(), RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT));
  9194. if (!conn)
  9195. throwUnexpected();
  9196. }
  9197. return conn->getRoot();
  9198. }
  9199. IPropertyTree * addNamedPackageSet(IPropertyTree * packageRegistry, const char * name, IPropertyTree *packageInfo, bool overWrite)
  9200. {
  9201. StringBuffer xpath;
  9202. StringBuffer lcName(name);
  9203. lcName.toLowerCase();
  9204. // see if "name" already exists
  9205. xpath.append("Package[@id='").append(name).append("']");
  9206. IPropertyTree *pkgTree = packageRegistry->queryPropTree(xpath.str());
  9207. if (pkgTree)
  9208. {
  9209. if (overWrite)
  9210. packageRegistry->removeTree(pkgTree);
  9211. else
  9212. throw MakeStringException(WUERR_PackageAlreadyExists, "Package name %s already exists, either delete it or specify overwrite",name);
  9213. }
  9214. IPropertyTree *tree = packageRegistry->addPropTree("Package", packageInfo);
  9215. tree->setProp("@id", lcName);
  9216. return tree;
  9217. }
  9218. void removeNamedPackage(IPropertyTree * packageRegistry, const char * id)
  9219. {
  9220. StringBuffer lcId(id);
  9221. lcId.toLowerCase();
  9222. StringBuffer xpath;
  9223. xpath.append("Package[@id=\"").append(lcId).append("\"]");
  9224. packageRegistry->removeProp(xpath);
  9225. }
  9226. extern WORKUNIT_API IPropertyTree * getPackageSetRegistry(const char * wsEclId, bool readonly)
  9227. {
  9228. Owned<IRemoteConnection> globalLock = querySDS().connect("/PackageSets/", myProcessSession(), RTM_LOCK_WRITE|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  9229. //Only lock the branch for the target we're interested in.
  9230. StringBuffer xpath;
  9231. xpath.append("/PackageSets/PackageSet[@id=\"").append(wsEclId).append("\"]");
  9232. Owned<IRemoteConnection> conn = querySDS().connect(xpath.str(), myProcessSession(), readonly ? RTM_LOCK_READ : RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT);
  9233. if (!conn)
  9234. {
  9235. if (readonly)
  9236. return NULL;
  9237. Owned<IPropertyTree> querySet = createPTree();
  9238. querySet->setProp("@id", wsEclId);
  9239. globalLock->queryRoot()->addPropTree("PackageSet", querySet.getClear());
  9240. globalLock->commit();
  9241. conn.setown(querySDS().connect(xpath.str(), myProcessSession(), RTM_LOCK_WRITE, SDS_LOCK_TIMEOUT));
  9242. if (!conn)
  9243. throwUnexpected();
  9244. }
  9245. return conn->getRoot();
  9246. }
  9247. void addQueryToQuerySet(IWorkUnit *workunit, const char *querySetName, const char *queryName, IPropertyTree *packageInfo, WUQueryActivationOptions activateOption, StringBuffer &newQueryId, const char *userid)
  9248. {
  9249. StringBuffer cleanQueryName;
  9250. appendUtf8XmlName(cleanQueryName, strlen(queryName), queryName);
  9251. SCMStringBuffer dllName;
  9252. Owned<IConstWUQuery> q = workunit->getQuery();
  9253. q->getQueryDllName(dllName);
  9254. if (!dllName.length())
  9255. throw MakeStringException(WUERR_InvalidDll, "Cannot deploy query - no associated dll.");
  9256. SCMStringBuffer wuid;
  9257. workunit->getWuid(wuid);
  9258. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, false);
  9259. StringBuffer currentTargetClusterType;
  9260. queryRegistry->getProp("@targetclustertype", currentTargetClusterType);
  9261. SCMStringBuffer targetClusterType;
  9262. workunit->getDebugValue("targetclustertype", targetClusterType);
  9263. SCMStringBuffer snapshot;
  9264. workunit->getSnapshot(snapshot);
  9265. if (currentTargetClusterType.length() < 1)
  9266. {
  9267. queryRegistry->setProp("@targetclustertype", targetClusterType.str());
  9268. }
  9269. else
  9270. {
  9271. if (strcmp(currentTargetClusterType.str(), "roxie") == 0 && strcmp(currentTargetClusterType.str(), targetClusterType.str())!=0)
  9272. {
  9273. throw MakeStringException(WUERR_MismatchClusterType, "TargetClusterTypes of workunit and queryset do not match.");
  9274. }
  9275. }
  9276. IPropertyTree *newEntry = addNamedQuery(queryRegistry, cleanQueryName, wuid.str(), dllName.str(), isLibrary(workunit), userid, snapshot.str());
  9277. newQueryId.append(newEntry->queryProp("@id"));
  9278. workunit->setIsQueryService(true); //will check querysets before delete
  9279. workunit->commit();
  9280. if (activateOption == ACTIVATE_SUSPEND_PREVIOUS|| activateOption == ACTIVATE_DELETE_PREVIOUS)
  9281. {
  9282. Owned<IPropertyTree> prevQuery = resolveQueryAlias(queryRegistry, cleanQueryName);
  9283. setQueryAlias(queryRegistry, cleanQueryName, newQueryId);
  9284. if (prevQuery)
  9285. {
  9286. if (activateOption == ACTIVATE_SUSPEND_PREVIOUS)
  9287. setQuerySuspendedState(queryRegistry, prevQuery->queryProp("@id"), true, userid);
  9288. else
  9289. removeNamedQuery(queryRegistry, prevQuery->queryProp("@id"));
  9290. }
  9291. }
  9292. else if (activateOption == MAKE_ACTIVATE || activateOption == MAKE_ACTIVATE_LOAD_DATA_ONLY)
  9293. setQueryAlias(queryRegistry, cleanQueryName, newQueryId.str());
  9294. }
  9295. bool removeQuerySetAlias(const char *querySetName, const char *alias)
  9296. {
  9297. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, true);
  9298. StringBuffer xpath;
  9299. xpath.appendf("Alias[@name='%s']", alias);
  9300. IPropertyTree *t = queryRegistry->queryPropTree(xpath);
  9301. return queryRegistry->removeTree(t);
  9302. }
  9303. void addQuerySetAlias(const char *querySetName, const char *alias, const char *id)
  9304. {
  9305. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, false);
  9306. setQueryAlias(queryRegistry, alias, id);
  9307. }
  9308. void setSuspendQuerySetQuery(const char *querySetName, const char *id, bool suspend, const char *userid)
  9309. {
  9310. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, true);
  9311. setQuerySuspendedState(queryRegistry, id, suspend, userid);
  9312. }
  9313. void deleteQuerySetQuery(const char *querySetName, const char *id)
  9314. {
  9315. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, true);
  9316. removeNamedQuery(queryRegistry, id);
  9317. }
  9318. void removeQuerySetAliasesFromNamedQuery(const char *querySetName, const char * id)
  9319. {
  9320. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, true);
  9321. clearAliases(queryRegistry, id);
  9322. }
  9323. void setQueryCommentForNamedQuery(const char *querySetName, const char *id, const char *queryComment)
  9324. {
  9325. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, true);
  9326. setQueryCommentForNamedQuery(queryRegistry, id, queryComment);
  9327. }
  9328. const char *queryIdFromQuerySetWuid(const char *querySetName, const char *wuid, IStringVal &id)
  9329. {
  9330. Owned<IPropertyTree> queryRegistry = getQueryRegistry(querySetName, false);
  9331. StringBuffer xpath;
  9332. xpath.appendf("Query[@wuid='%s']", wuid);
  9333. IPropertyTree *q = queryRegistry->queryPropTree(xpath.str());
  9334. if (q)
  9335. {
  9336. id.set(q->queryProp("@id"));
  9337. }
  9338. return id.str();
  9339. }
  9340. extern WORKUNIT_API void gatherLibraryNames(StringArray &names, StringArray &unresolved, IWorkUnitFactory &workunitFactory, IConstWorkUnit &cw, IPropertyTree *queryset)
  9341. {
  9342. IConstWULibraryIterator &wulibraries = cw.getLibraries();
  9343. ForEach(wulibraries)
  9344. {
  9345. SCMStringBuffer libname;
  9346. IConstWULibrary &wulibrary = wulibraries.query();
  9347. wulibrary.getName(libname);
  9348. if (names.contains(libname.str()) || unresolved.contains(libname.str()))
  9349. continue;
  9350. Owned<IPropertyTree> query = resolveQueryAlias(queryset, libname.str());
  9351. if (query && query->getPropBool("@isLibrary"))
  9352. {
  9353. const char *wuid = query->queryProp("@wuid");
  9354. Owned<IConstWorkUnit> libcw = workunitFactory.openWorkUnit(wuid, false);
  9355. if (libcw)
  9356. {
  9357. names.appendUniq(libname.str());
  9358. gatherLibraryNames(names, unresolved, workunitFactory, *libcw, queryset);
  9359. continue;
  9360. }
  9361. }
  9362. unresolved.appendUniq(libname.str());
  9363. }
  9364. }
  9365. bool looksLikeAWuid(const char * wuid)
  9366. {
  9367. if (!wuid)
  9368. return false;
  9369. if (wuid[0] != 'W')
  9370. return false;
  9371. if (!isdigit(wuid[1]) || !isdigit(wuid[2]) || !isdigit(wuid[3]) || !isdigit(wuid[4]))
  9372. return false;
  9373. if (!isdigit(wuid[5]) || !isdigit(wuid[6]) || !isdigit(wuid[7]) || !isdigit(wuid[8]))
  9374. return false;
  9375. return (wuid[9]=='-');
  9376. }
  9377. IPropertyTree * resolveDefinitionInArchive(IPropertyTree * archive, const char * path)
  9378. {
  9379. IPropertyTree * module = archive;
  9380. const char * dot = strrchr(path, '.');
  9381. StringBuffer xpath;
  9382. if (dot)
  9383. {
  9384. xpath.clear().append("Module[@key='").appendLower(dot-path, path).append("']");
  9385. module = archive->queryPropTree(xpath);
  9386. path = dot+1;
  9387. }
  9388. else
  9389. module = archive->queryPropTree("Module[@key='']");
  9390. if (!module)
  9391. return NULL;
  9392. xpath.clear().append("Attribute[@key='").appendLower(strlen(path), path).append("']");
  9393. return module->queryPropTree(xpath);
  9394. }
  9395. extern WORKUNIT_API void associateLocalFile(IWUQuery * query, WUFileType type, const char * name, const char * description, unsigned crc)
  9396. {
  9397. StringBuffer hostname;
  9398. queryHostIP().getIpText(hostname);
  9399. StringBuffer fullPathname;
  9400. makeAbsolutePath(name, fullPathname);
  9401. query->addAssociatedFile(type, fullPathname, hostname, description, crc);
  9402. }
  9403. extern WORKUNIT_API void descheduleWorkunit(char const * wuid)
  9404. {
  9405. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  9406. Owned<IWorkUnit> workunit = factory->updateWorkUnit(wuid);
  9407. if(workunit)
  9408. workunit->deschedule();
  9409. else
  9410. doDescheduleWorkkunit(wuid);
  9411. }
  9412. extern WORKUNIT_API void updateWorkunitTimeStat(IWorkUnit * wu, const char * component, const char * wuScope, const char * stat, const char * description, unsigned __int64 value, unsigned __int64 count, unsigned __int64 maxValue)
  9413. {
  9414. if (!wuScope)
  9415. wuScope = "workunit";
  9416. if (!stat)
  9417. stat = "time";
  9418. //The following line duplicates the statistics as timing information - preserved temporarily to show refactoring.
  9419. //wu->setTimerInfo(description, (unsigned)(value/1000000), (unsigned)count, (unsigned)maxValue);
  9420. wu->setStatistic(component, wuScope, stat, description, SMEASURE_TIME_NS, value, count, maxValue, false);
  9421. }
  9422. extern WORKUNIT_API void updateWorkunitTiming(IWorkUnit * wu, const char * component, const char * mangledScope, const char * description, unsigned __int64 value, unsigned __int64 count, unsigned __int64 maxValue)
  9423. {
  9424. StringAttr scopeText;
  9425. StringAttr componentText;
  9426. const char * wuScope = mangledScope;
  9427. const char * stat = "time";
  9428. //If the scope contains a semicolon then it is taken to mean (wuScope;stat or comonent;wuScope;stat)
  9429. const char * sep1 = strchr(mangledScope, ';');
  9430. if (sep1)
  9431. {
  9432. const char * sep2 = strchr(sep1+1, ';');
  9433. if (sep2)
  9434. {
  9435. componentText.set(mangledScope, sep1 - mangledScope);
  9436. scopeText.set(sep1+1, sep2-(sep1+1));
  9437. component = componentText;
  9438. wuScope = scopeText;
  9439. stat = sep2+1;
  9440. }
  9441. else
  9442. {
  9443. scopeText.set(mangledScope, sep1-mangledScope);
  9444. wuScope = scopeText.get();
  9445. stat = sep1+1;
  9446. }
  9447. }
  9448. updateWorkunitTimeStat(wu, component, wuScope, stat, description, value, count, maxValue);
  9449. }
  9450. extern WORKUNIT_API void updateWorkunitTimings(IWorkUnit * wu, ITimeReporter *timer, const char * component)
  9451. {
  9452. StringBuffer description;
  9453. StringBuffer scope;
  9454. for (unsigned i = 0; i < timer->numSections(); i++)
  9455. {
  9456. timer->getDescription(i, description.clear());
  9457. timer->getScope(i, scope.clear());
  9458. updateWorkunitTiming(wu, component, scope, description, timer->getTime(i), timer->getCount(i), timer->getMaxTime(i));
  9459. }
  9460. }