12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744 |
- /*##############################################################################
- HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ############################################################################## */
- // todo look at IRemoteFileServer stop
- #include "platform.h"
- #include "limits.h"
- #include "jlib.hpp"
- #include "jio.hpp"
- #include "jmutex.hpp"
- #include "jfile.hpp"
- #include "jmisc.hpp"
- #include "jthread.hpp"
- #include "jqueue.tpp"
- #include "securesocket.hpp"
- #include "portlist.h"
- #include "jsocket.hpp"
- #include "jencrypt.hpp"
- #include "jlzw.hpp"
- #include "jset.hpp"
- #include "jhtree.hpp"
- #include "dadfs.hpp"
- #include "remoteerr.hpp"
- #include <atomic>
- #include <string>
- #include <unordered_map>
- #include "rtldynfield.hpp"
- #include "rtlds_imp.hpp"
- #include "rtlread_imp.hpp"
- #include "rtlrecord.hpp"
- #include "eclhelper_dyn.hpp"
- #include "rtlcommon.hpp"
- #include "rtlformat.hpp"
- #include "jflz.hpp"
- #include "digisign.hpp"
- #include "dafdesc.hpp"
- #include "thorcommon.hpp"
- #include "csvsplitter.hpp"
- #include "thorxmlread.hpp"
- #include "dafscommon.hpp"
- #include "rmtfile.hpp"
- #include "rmtclient_impl.hpp"
- #include "dafsserver.hpp"
- using namespace cryptohelper;
- #define SOCKET_CACHE_MAX 500
- #define TREECOPYTIMEOUT (60*60*1000) // 1Hr (I guess could take longer for big file but at least will stagger)
- #define TREECOPYPOLLTIME (60*1000*5) // for tracing that delayed
- #define TREECOPYPRUNETIME (24*60*60*1000) // 1 day
- static const unsigned __int64 defaultFileStreamChooseNLimit = I64C(0x7fffffffffffffff); // constant should be move to common place (see eclhelper.hpp)
- static const unsigned __int64 defaultFileStreamSkipN = 0;
- static const unsigned defaultDaFSReplyLimitKB = 1024; // 1MB
- enum OutputFormat:byte { outFmt_Binary, outFmt_Xml, outFmt_Json };
- ///////////////////////////
- static unsigned maxConnectTime = 0;
- static unsigned maxReceiveTime = 0;
- //Security and default port attributes
- static class _securitySettings
- {
- public:
- DAFSConnectCfg connectMethod;
- unsigned short daFileSrvPort;
- unsigned short daFileSrvSSLPort;
- const char * certificate;
- const char * privateKey;
- const char * passPhrase;
- _securitySettings()
- {
- queryDafsSecSettings(&connectMethod, &daFileSrvPort, &daFileSrvSSLPort, &certificate, &privateKey, &passPhrase);
- }
- } securitySettings;
- static CriticalSection secureContextCrit;
- static Owned<ISecureSocketContext> secureContextServer;
- static Owned<ISecureSocketContext> secureContextClient;
- #ifdef _USE_OPENSSL
- static ISecureSocket *createSecureSocket(ISocket *sock, SecureSocketType type)
- {
- {
- CriticalBlock b(secureContextCrit);
- if (type == ServerSocket)
- {
- if (!secureContextServer)
- secureContextServer.setown(createSecureSocketContextEx(securitySettings.certificate, securitySettings.privateKey, securitySettings.passPhrase, type));
- }
- else if (!secureContextClient)
- secureContextClient.setown(createSecureSocketContext(type));
- }
- int loglevel = SSLogNormal;
- #ifdef _DEBUG
- loglevel = SSLogMax;
- #endif
- if (type == ServerSocket)
- return secureContextServer->createSecureSocket(sock, loglevel);
- else
- return secureContextClient->createSecureSocket(sock, loglevel);
- }
- #else
- static ISecureSocket *createSecureSocket(ISocket *sock, SecureSocketType type)
- {
- throwUnexpected();
- }
- #endif
- struct sRFTM
- {
- CTimeMon *timemon;
- sRFTM(unsigned limit) { timemon = limit ? new CTimeMon(limit) : NULL; }
- ~sRFTM() { delete timemon; }
- };
- const char *remoteServerVersionString() { return DAFILESRV_VERSIONSTRING; }
- #define CLIENT_TIMEOUT (1000*60*60*12) // long timeout in case zombies
- #define CLIENT_INACTIVEWARNING_TIMEOUT (1000*60*60*12) // time between logging inactive clients
- #define SERVER_TIMEOUT (1000*60*5) // timeout when waiting for dafilesrv to reply after command
- // (increased when waiting for large block)
- #define RFCText(cmd) #cmd
- const char *RFCStrings[] =
- {
- RFCText(RFCopenIO),
- RFCText(RFCcloseIO),
- RFCText(RFCread),
- RFCText(RFCwrite),
- RFCText(RFCsize),
- RFCText(RFCexists),
- RFCText(RFCremove),
- RFCText(RFCrename),
- RFCText(RFCgetver),
- RFCText(RFCisfile),
- RFCText(RFCisdirectory),
- RFCText(RFCisreadonly),
- RFCText(RFCsetreadonly),
- RFCText(RFCgettime),
- RFCText(RFCsettime),
- RFCText(RFCcreatedir),
- RFCText(RFCgetdir),
- RFCText(RFCstop),
- RFCText(RFCexec),
- RFCText(RFCdummy1),
- RFCText(RFCredeploy),
- RFCText(RFCgetcrc),
- RFCText(RFCmove),
- RFCText(RFCsetsize),
- RFCText(RFCextractblobelements),
- RFCText(RFCcopy),
- RFCText(RFCappend),
- RFCText(RFCmonitordir),
- RFCText(RFCsettrace),
- RFCText(RFCgetinfo),
- RFCText(RFCfirewall),
- RFCText(RFCunlock),
- RFCText(RFCunlockreply),
- RFCText(RFCinvalid),
- RFCText(RFCcopysection),
- RFCText(RFCtreecopy),
- RFCText(RFCtreecopytmp),
- RFCText(RFCsetthrottle), // legacy version
- RFCText(RFCsetthrottle2),
- RFCText(RFCsetfileperms),
- RFCText(RFCreadfilteredindex),
- RFCText(RFCreadfilteredcount),
- RFCText(RFCreadfilteredblob),
- RFCText(RFCStreamRead),
- RFCText(RFCStreamReadTestSocket),
- RFCText(RFCStreamGeneral),
- RFCText(RFCStreamReadJSON),
- RFCText(RFCmaxnormal),
- };
- static const char *getRFCText(RemoteFileCommandType cmd)
- {
- if (cmd==RFCStreamReadJSON)
- return "RFCStreamReadJSON";
- else
- {
- unsigned elems = sizeof(RFCStrings) / sizeof(RFCStrings[0]);
- if (cmd >= elems)
- return "RFCunknown";
- return RFCStrings[cmd];
- }
- }
- static const char *getRFSERRText(unsigned err)
- {
- switch (err)
- {
- case RFSERR_InvalidCommand:
- return "RFSERR_InvalidCommand";
- case RFSERR_NullFileIOHandle:
- return "RFSERR_NullFileIOHandle";
- case RFSERR_InvalidFileIOHandle:
- return "RFSERR_InvalidFileIOHandle";
- case RFSERR_TimeoutFileIOHandle:
- return "RFSERR_TimeoutFileIOHandle";
- case RFSERR_OpenFailed:
- return "RFSERR_OpenFailed";
- case RFSERR_ReadFailed:
- return "RFSERR_ReadFailed";
- case RFSERR_WriteFailed:
- return "RFSERR_WriteFailed";
- case RFSERR_RenameFailed:
- return "RFSERR_RenameFailed";
- case RFSERR_ExistsFailed:
- return "RFSERR_ExistsFailed";
- case RFSERR_RemoveFailed:
- return "RFSERR_RemoveFailed";
- case RFSERR_CloseFailed:
- return "RFSERR_CloseFailed";
- case RFSERR_IsFileFailed:
- return "RFSERR_IsFileFailed";
- case RFSERR_IsDirectoryFailed:
- return "RFSERR_IsDirectoryFailed";
- case RFSERR_IsReadOnlyFailed:
- return "RFSERR_IsReadOnlyFailed";
- case RFSERR_SetReadOnlyFailed:
- return "RFSERR_SetReadOnlyFailed";
- case RFSERR_GetTimeFailed:
- return "RFSERR_GetTimeFailed";
- case RFSERR_SetTimeFailed:
- return "RFSERR_SetTimeFailed";
- case RFSERR_CreateDirFailed:
- return "RFSERR_CreateDirFailed";
- case RFSERR_GetDirFailed:
- return "RFSERR_GetDirFailed";
- case RFSERR_GetCrcFailed:
- return "RFSERR_GetCrcFailed";
- case RFSERR_MoveFailed:
- return "RFSERR_MoveFailed";
- case RFSERR_ExtractBlobElementsFailed:
- return "RFSERR_ExtractBlobElementsFailed";
- case RFSERR_CopyFailed:
- return "RFSERR_CopyFailed";
- case RFSERR_AppendFailed:
- return "RFSERR_AppendFailed";
- case RFSERR_AuthenticateFailed:
- return "RFSERR_AuthenticateFailed";
- case RFSERR_CopySectionFailed:
- return "RFSERR_CopySectionFailed";
- case RFSERR_TreeCopyFailed:
- return "RFSERR_TreeCopyFailed";
- case RAERR_InvalidUsernamePassword:
- return "RAERR_InvalidUsernamePassword";
- case RFSERR_MasterSeemsToHaveDied:
- return "RFSERR_MasterSeemsToHaveDied";
- case RFSERR_TimeoutWaitSlave:
- return "RFSERR_TimeoutWaitSlave";
- case RFSERR_TimeoutWaitConnect:
- return "RFSERR_TimeoutWaitConnect";
- case RFSERR_TimeoutWaitMaster:
- return "RFSERR_TimeoutWaitMaster";
- case RFSERR_NoConnectSlave:
- return "RFSERR_NoConnectSlave";
- case RFSERR_NoConnectSlaveXY:
- return "RFSERR_NoConnectSlaveXY";
- case RFSERR_VersionMismatch:
- return "RFSERR_VersionMismatch";
- case RFSERR_SetThrottleFailed:
- return "RFSERR_SetThrottleFailed";
- case RFSERR_MaxQueueRequests:
- return "RFSERR_MaxQueueRequests";
- case RFSERR_KeyIndexFailed:
- return "RFSERR_MaxQueueRequests";
- case RFSERR_StreamReadFailed:
- return "RFSERR_StreamReadFailed";
- case RFSERR_InternalError:
- return "Internal Error";
- }
- return "RFSERR_Unknown";
- }
- #define ThrottleText(throttleClass) #throttleClass
- const char *ThrottleStrings[] =
- {
- ThrottleText(ThrottleStd),
- ThrottleText(ThrottleSlow),
- };
- // very high upper limits that configure can't exceed
- #define THROTTLE_MAX_LIMIT 1000000
- #define THROTTLE_MAX_DELAYMS 3600000
- #define THROTTLE_MAX_CPUTHRESHOLD 100
- #define THROTTLE_MAX_QUEUELIMIT 10000000
- static const char *getThrottleClassText(ThrottleClass throttleClass) { return ThrottleStrings[throttleClass]; }
- //---------------------------------------------------------------------------
- // TreeCopy
- #define TREECOPY_CACHE_SIZE 50
- struct CTreeCopyItem: public CInterface
- {
- StringAttr net;
- StringAttr mask;
- offset_t sz; // original size
- CDateTime dt; // original date
- RemoteFilenameArray loc; // locations for file - 0 is original
- Owned<IBitSet> busy;
- unsigned lastused;
- CTreeCopyItem(RemoteFilename &orig, const char *_net, const char *_mask, offset_t _sz, CDateTime &_dt)
- : net(_net), mask(_mask)
- {
- loc.append(orig);
- dt.set(_dt);
- sz = _sz;
- busy.setown(createThreadSafeBitSet());
- lastused = msTick();
- }
- bool equals(const RemoteFilename &orig, const char *_net, const char *_mask, offset_t _sz, CDateTime &_dt)
- {
- if (!orig.equals(loc.item(0)))
- return false;
- if (strcmp(_net,net)!=0)
- return false;
- if (strcmp(_mask,mask)!=0)
- return false;
- if (sz!=_sz)
- return false;
- return (dt.equals(_dt,false));
- }
- };
- static CIArrayOf<CTreeCopyItem> treeCopyArray;
- static CriticalSection treeCopyCrit;
- static unsigned treeCopyWaiting=0;
- static Semaphore treeCopySem;
- /////////////////////////
- //====================================================================================================
- class CAsyncCommandManager
- {
- class CAsyncJob: public CInterface
- {
- class cThread: public Thread
- {
- CAsyncJob *parent;
- public:
- cThread(CAsyncJob *_parent)
- : Thread("CAsyncJob")
- {
- parent = _parent;
- }
- int run()
- {
- int ret = -1;
- try {
- ret = parent->run();
- parent->setDone();
- }
- catch (IException *e)
- {
- parent->setException(e);
- }
- parent->signal();
- return ret;
- }
- } *thread;
- StringAttr uuid;
- CAsyncCommandManager &parent;
- public:
- CAsyncJob(CAsyncCommandManager &_parent, const char *_uuid)
- : uuid(_uuid), parent(_parent)
- {
- thread = new cThread(this);
- hash = hashc((const byte *)uuid.get(),uuid.length(),~0U);
- }
- ~CAsyncJob()
- {
- thread->join();
- thread->Release();
- }
- static void destroy(CAsyncJob *j)
- {
- j->Release();
- }
- void signal()
- {
- parent.signal();
- }
- void start()
- {
- parent.wait();
- thread->start();
- }
- void join()
- {
- thread->join();
- }
- static unsigned getHash(const char *key)
- {
- return hashc((const byte *)key,strlen(key),~0U);
- }
- static CAsyncJob* create(const char *key) { assertex(!"CAsyncJob::create not implemented"); return NULL; }
- unsigned hash;
- bool eq(const char *key)
- {
- return stricmp(key,uuid.get())==0;
- }
- virtual int run()=0;
- virtual void setException(IException *e)=0;
- virtual void setDone()=0;
- };
- class CAsyncCopySection: public CAsyncJob
- {
- Owned<IFile> src;
- RemoteFilename dst;
- offset_t toOfs;
- offset_t fromOfs;
- offset_t size;
- CFPmode mode; // not yet supported
- CriticalSection sect;
- offset_t done;
- offset_t total;
- Semaphore finished;
- AsyncCommandStatus status;
- Owned<IException> exc;
- public:
- CAsyncCopySection(CAsyncCommandManager &parent, const char *_uuid, const char *fromFile, const char *toFile, offset_t _toOfs, offset_t _fromOfs, offset_t _size)
- : CAsyncJob(parent, _uuid)
- {
- status = ACScontinue;
- src.setown(createIFile(fromFile));
- dst.setRemotePath(toFile);
- toOfs = _toOfs;
- fromOfs = _fromOfs;
- size = _size;
- mode = CFPcontinue;
- done = 0;
- total = (offset_t)-1;
- }
- AsyncCommandStatus poll(offset_t &_done, offset_t &_total,unsigned timeout)
- {
- if (timeout&&finished.wait(timeout))
- finished.signal(); // may need to call again
- CriticalBlock block(sect);
- if (exc)
- throw exc.getClear();
- _done = done;
- _total = total;
- return status;
- }
- int run()
- {
- class cProgress: implements ICopyFileProgress
- {
- CriticalSection §
- CFPmode &mode;
- offset_t &done;
- offset_t &total;
- public:
- cProgress(CriticalSection &_sect,offset_t &_done,offset_t &_total,CFPmode &_mode)
- : sect(_sect), mode(_mode), done(_done), total(_total)
- {
- }
- CFPmode onProgress(offset_t sizeDone, offset_t totalSize)
- {
- CriticalBlock block(sect);
- done = sizeDone;
- total = totalSize;
- return mode;
- }
- } progress(sect,total,done,mode);
- src->copySection(dst,toOfs, fromOfs, size, &progress); // exceptions will be handled by base class
- return 0;
- }
- void setException(IException *e)
- {
- EXCLOG(e,"CAsyncCommandManager::CAsyncJob");
- CriticalBlock block(sect);
- if (exc.get())
- e->Release();
- else
- exc.setown(e);
- status = ACSerror;
- }
- void setDone()
- {
- CriticalBlock block(sect);
- finished.signal();
- status = ACSdone;
- }
- };
- CMinHashTable<CAsyncJob> jobtable;
- CriticalSection sect;
- Semaphore threadsem;
- unsigned limit;
- public:
- CAsyncCommandManager(unsigned _limit) : limit(_limit)
- {
- if (limit) // 0 == unbound
- threadsem.signal(limit); // max number of async jobs
- }
- void join()
- {
- CriticalBlock block(sect);
- unsigned i;
- CAsyncJob *j=jobtable.first(i);
- while (j) {
- j->join();
- j=jobtable.next(i);
- }
- }
- void signal()
- {
- if (limit)
- threadsem.signal();
- }
- void wait()
- {
- if (limit)
- threadsem.wait();
- }
- AsyncCommandStatus copySection(const char *uuid, const char *fromFile, const char *toFile, offset_t toOfs, offset_t fromOfs, offset_t size, offset_t &done, offset_t &total, unsigned timeout)
- {
- // return 0 if continuing, 1 if done
- CAsyncCopySection * job;
- Linked<CAsyncJob> cjob;
- {
- CriticalBlock block(sect);
- cjob.set(jobtable.find(uuid,false));
- if (cjob) {
- job = QUERYINTERFACE(cjob.get(),CAsyncCopySection);
- if (!job) {
- throw MakeStringException(-1,"Async job ID mismatch");
- }
- }
- else {
- job = new CAsyncCopySection(*this, uuid, fromFile, toFile, toOfs, fromOfs, size);
- cjob.setown(job);
- jobtable.add(cjob.getLink());
- cjob->start();
- }
- }
- AsyncCommandStatus ret = ACSerror;
- Owned<IException> rete;
- try {
- ret = job->poll(done,total,timeout);
- }
- catch (IException * e) {
- rete.setown(e);
- }
- if ((ret!=ACScontinue)||rete.get()) {
- job->join();
- CriticalBlock block(sect);
- jobtable.remove(job);
- if (rete.get())
- throw rete.getClear();
- }
- return ret;
- }
- };
- //====================================================================================================
- inline void appendErr(MemoryBuffer &reply, unsigned e)
- {
- reply.append(e).append(getRFSERRText(e));
- }
- #define MAPCOMMAND(c,p) case c: { this->p(msg, reply) ; break; }
- #define MAPCOMMANDCLIENT(c,p,client) case c: { this->p(msg, reply, client); break; }
- #define MAPCOMMANDCLIENTTESTSOCKET(c,p,client) case c: { testSocketFlag = true; this->p(msg, reply, client); break; }
- #define MAPCOMMANDCLIENTTHROTTLE(c,p,client,throttler) case c: { this->p(msg, reply, client, throttler); break; }
- #define MAPCOMMANDSTATS(c,p,stats) case c: { this->p(msg, reply, stats); break; }
- #define MAPCOMMANDCLIENTSTATS(c,p,client,stats) case c: { this->p(msg, reply, client, stats); break; }
- static unsigned ClientCount = 0;
- static unsigned MaxClientCount = 0;
- static CriticalSection ClientCountSect;
- #define DEFAULT_THROTTLOG_LOG_INTERVAL_SECS 60 // log total throttled delay period
- class CClientStats : public CInterface
- {
- public:
- CClientStats(const char *_client) : client(_client), count(0), bRead(0), bWritten(0) { }
- const char *queryFindString() const { return client; }
- inline void addRead(unsigned __int64 len)
- {
- bRead += len;
- }
- inline void addWrite(unsigned __int64 len)
- {
- bWritten += len;
- }
- void getStatus(StringBuffer & info) const
- {
- info.appendf("Client %s - %" I64F "d requests handled, bytes read = %" I64F "d, bytes written = % " I64F "d",
- client.get(), count, bRead.load(), bWritten.load()).newline();
- }
- StringAttr client;
- unsigned __int64 count;
- std::atomic<unsigned __int64> bRead;
- std::atomic<unsigned __int64> bWritten;
- };
- class CClientStatsTable : public OwningStringSuperHashTableOf<CClientStats>
- {
- typedef OwningStringSuperHashTableOf<CClientStats> PARENT;
- CriticalSection crit;
- unsigned cmdStats[RFCmax];
- static int compareElement(void* const *ll, void* const *rr)
- {
- const CClientStats *l = (const CClientStats *) *ll;
- const CClientStats *r = (const CClientStats *) *rr;
- if (l->count == r->count)
- return 0;
- else if (l->count<r->count)
- return 1;
- else
- return -1;
- }
- public:
- CClientStatsTable()
- {
- memset(&cmdStats[0], 0, sizeof(cmdStats));
- }
- ~CClientStatsTable()
- {
- _releaseAll();
- }
- CClientStats *getClientReference(RemoteFileCommandType cmd, const char *client)
- {
- CriticalBlock b(crit);
- CClientStats *stats = PARENT::find(client);
- if (!stats)
- {
- stats = new CClientStats(client);
- PARENT::replace(*stats);
- }
- if (cmd<RFCmax) // i.e. ignore duff command (which will be traced), but still record client connected
- cmdStats[cmd]++;
- ++stats->count;
- return LINK(stats);
- }
- StringBuffer &getInfo(StringBuffer &info, unsigned level=1)
- {
- CriticalBlock b(crit);
- unsigned __int64 totalCmds = 0;
- for (unsigned c=0; c<RFCmax; c++)
- totalCmds += cmdStats[c];
- unsigned totalClients = PARENT::ordinality();
- info.appendf("Commands processed = %" I64F "u, unique clients = %u", totalCmds, totalClients);
- if (totalCmds)
- {
- info.append("Command stats:").newline();
- for (unsigned c=0; c<RFCmax; c++)
- {
- unsigned __int64 count = cmdStats[c];
- if (count)
- info.append(getRFCText(c)).append(": ").append(count).newline();
- }
- }
- if (totalClients)
- {
- SuperHashIteratorOf<CClientStats> iter(*this);
- PointerArrayOf<CClientStats> elements;
- ForEach(iter)
- {
- CClientStats &elem = iter.query();
- elements.append(&elem);
- }
- elements.sort(&compareElement);
- if (level < 10)
- {
- // list up to 10 clients ordered by # of commands processed
- unsigned max=elements.ordinality();
- if (max>10)
- max = 10; // cap
- info.append("Top 10 clients:").newline();
- for (unsigned e=0; e<max; e++)
- {
- const CClientStats &element = *elements.item(e);
- element.getStatus(info);
- }
- }
- else // list all
- {
- info.append("All clients:").newline();
- ForEachItemIn(e, elements)
- {
- const CClientStats &element = *elements.item(e);
- element.getStatus(info);
- }
- }
- }
- return info;
- }
- void reset()
- {
- CriticalBlock b(crit);
- memset(&cmdStats[0], 0, sizeof(cmdStats));
- kill();
- }
- };
- interface IRemoteReadActivity;
- interface IRemoteWriteActivity;
- interface IRemoteActivity : extends IInterface
- {
- virtual unsigned __int64 queryProcessed() const = 0;
- virtual IOutputMetaData *queryOutputMeta() const = 0;
- virtual StringBuffer &getInfoStr(StringBuffer &out) const = 0;
- virtual void serializeCursor(MemoryBuffer &tgt) const = 0;
- virtual void restoreCursor(MemoryBuffer &src) = 0;
- virtual bool isGrouped() const = 0;
- virtual void flushStatistics(CClientStats &stats) = 0;
- virtual IRemoteReadActivity *queryIsReadActivity() { return nullptr; }
- virtual IRemoteWriteActivity *queryIsWriteActivity() { return nullptr; }
- };
- interface IRemoteReadActivity : extends IRemoteActivity
- {
- virtual const void *nextRow(MemoryBufferBuilder &outBuilder, size32_t &sz) = 0;
- virtual bool requiresPostProject() const = 0;
- };
- interface IRemoteWriteActivity : extends IRemoteActivity
- {
- virtual void write(size32_t sz, const void *row) = 0;
- };
- class CRemoteRequest : public CSimpleInterfaceOf<IInterface>
- {
- int cursorHandle;
- OutputFormat format;
- unsigned __int64 replyLimit = defaultDaFSReplyLimitKB * 1024;
- Linked<IRemoteActivity> activity;
- Linked<ICompressor> compressor;
- Linked<IExpander> expander;
- MemoryBuffer expandMb;
- Owned<IXmlWriterExt> responseWriter; // for xml or json response
- bool handleFull(MemoryBuffer &inMb, size32_t inPos, MemoryBuffer &compressMb, ICompressor *compressor, size32_t replyLimit, size32_t &totalSz)
- {
- size32_t sz = inMb.length()-inPos;
- if (sz < replyLimit)
- return false;
- if (!compressor)
- return true;
- // consumes data from inMb into compressor
- totalSz += sz;
- const void *data = inMb.bytes()+inPos;
- assertex(compressor->write(data, sz) == sz);
- inMb.setLength(inPos);
- return compressMb.capacity() > replyLimit;
- }
- void processRead(IPropertyTree *requestTree, MemoryBuffer &responseMb)
- {
- IRemoteReadActivity *readActivity = activity->queryIsReadActivity();
- assertex(readActivity);
- MemoryBuffer compressMb;
- IOutputMetaData *outMeta = readActivity->queryOutputMeta();
- bool eoi=false;
- bool grouped = readActivity->isGrouped();
- MemoryBuffer resultBuffer;
- MemoryBufferBuilder outBuilder(resultBuffer, outMeta->getMinRecordSize());
- if (outFmt_Binary == format)
- {
- if (compressor)
- {
- compressMb.setEndian(__BIG_ENDIAN);
- compressMb.append(responseMb);
- }
- DelayedMarker<size32_t> dataLenMarker(compressor ? compressMb : responseMb); // uncompressed data size
- if (compressor)
- {
- size32_t initialSz = replyLimit >= 0x10000 ? 0x10000 : replyLimit;
- compressor->open(compressMb, initialSz);
- }
- outBuilder.setBuffer(responseMb); // write direct to responseMb buffer for efficiency
- unsigned __int64 numProcessedStart = readActivity->queryProcessed();
- size32_t totalDataSz = 0;
- size32_t dataStartPos = responseMb.length();
- if (grouped)
- {
- bool pastFirstRow = numProcessedStart>0;
- do
- {
- size32_t eogPos = 0;
- if (pastFirstRow)
- {
- /* this is for last row output, which might have been returned in the previous request
- * The eog marker may change as a result of the next row (see writeDirect() call below);
- */
- eogPos = responseMb.length();
- responseMb.append(false);
- }
- size32_t rowSz;
- const void *row = readActivity->nextRow(outBuilder, rowSz);
- if (!row)
- {
- if (!pastFirstRow)
- {
- eoi = true;
- break;
- }
- else
- {
- bool eog = true;
- responseMb.writeDirect(eogPos, sizeof(eog), &eog);
- row = readActivity->nextRow(outBuilder, rowSz);
- if (!row)
- {
- eoi = true;
- break;
- }
- }
- }
- pastFirstRow = true;
- }
- while (!handleFull(responseMb, dataStartPos, compressMb, compressor, replyLimit, totalDataSz));
- }
- else
- {
- do
- {
- size32_t rowSz;
- const void *row = readActivity->nextRow(outBuilder, rowSz);
- if (!row)
- {
- eoi = true;
- break;
- }
- }
- while (!handleFull(responseMb, dataStartPos, compressMb, compressor, replyLimit, totalDataSz));
- }
- // Consume any trailing data remaining
- if (compressor)
- {
- size32_t sz = responseMb.length()-dataStartPos;
- if (sz)
- {
- // consumes data built up in responseMb buffer into compressor
- totalDataSz += sz;
- const void *data = responseMb.bytes()+dataStartPos;
- assertex(compressor->write(data, sz) == sz);
- responseMb.setLength(dataStartPos);
- }
- }
- // finalize responseMb
- dataLenMarker.write(compressor ? totalDataSz : responseMb.length()-dataStartPos);
- DelayedSizeMarker cursorLenMarker(responseMb); // cursor length
- if (!eoi)
- readActivity->serializeCursor(responseMb);
- cursorLenMarker.write();
- if (compressor)
- {
- // consume cursor into compressor
- size32_t sz = responseMb.length()-dataStartPos;
- const void *data = responseMb.bytes()+dataStartPos;
- assertex(compressor->write(data, sz) == sz);
- compressor->close();
- // now ready to swap compressed output into responseMb
- responseMb.swapWith(compressMb);
- }
- }
- else
- {
- responseWriter->outputBeginArray("Row");
- if (grouped)
- {
- bool pastFirstRow = readActivity->queryProcessed()>0;
- bool first = true;
- do
- {
- size32_t rowSz;
- const void *row = readActivity->nextRow(outBuilder, rowSz);
- if (!row)
- {
- if (!pastFirstRow)
- {
- eoi = true;
- break;
- }
- else
- {
- row = readActivity->nextRow(outBuilder, rowSz);
- if (!row)
- {
- eoi = true;
- break;
- }
- if (first) // possible if eog was 1st row on next packet
- responseWriter->outputBeginNested("Row", false);
- responseWriter->outputBool(true, "dfs:Eog"); // field name cannot clash with an ecl field name
- }
- }
- if (pastFirstRow)
- responseWriter->outputEndNested("Row"); // close last row
- responseWriter->outputBeginNested("Row", false);
- outMeta->toXML((const byte *)row, *responseWriter);
- resultBuffer.clear();
- pastFirstRow = true;
- first = false;
- }
- while (responseWriter->length() < replyLimit);
- if (pastFirstRow)
- responseWriter->outputEndNested("Row"); // close last row
- }
- else
- {
- do
- {
- size32_t rowSz;
- const void *row = readActivity->nextRow(outBuilder, rowSz);
- if (!row)
- {
- eoi = true;
- break;
- }
- responseWriter->outputBeginNested("Row", false);
- outMeta->toXML((const byte *)row, *responseWriter);
- responseWriter->outputEndNested("Row");
- resultBuffer.clear();
- }
- while (responseWriter->length() < replyLimit);
- }
- responseWriter->outputEndArray("Row");
- if (!eoi)
- {
- MemoryBuffer cursorMb;
- cursorMb.setEndian(__BIG_ENDIAN);
- readActivity->serializeCursor(cursorMb);
- StringBuffer cursorBinStr;
- JBASE64_Encode(cursorMb.toByteArray(), cursorMb.length(), cursorBinStr, true);
- responseWriter->outputString(cursorBinStr.length(), cursorBinStr.str(), "cursorBin");
- }
- }
- }
- void processWrite(IPropertyTree *requestTree, MemoryBuffer &rowDataMb, MemoryBuffer &responseMb)
- {
- IRemoteWriteActivity *writeActivity = activity->queryIsWriteActivity();
- assertex(writeActivity);
- /* row data is in serialized disk format already, and do not need to look at individual rows
- * so simply dump to disk
- */
- size32_t rowDataSz;
- rowDataMb.read(rowDataSz);
- const void *rowData;
- if (expander)
- {
- rowDataSz = expander->init(rowDataMb.readDirect(rowDataSz));
- expandMb.clear().reserve(rowDataSz);
- expander->expand(expandMb.bufferBase());
- rowData = expandMb.bufferBase();
- }
- else
- rowData = rowDataMb.readDirect(rowDataSz);
- writeActivity->write(rowDataSz, rowData);
- }
- public:
- CRemoteRequest(int _cursorHandle, OutputFormat _format, ICompressor *_compressor, IExpander *_expander, IRemoteActivity *_activity)
- : cursorHandle(_cursorHandle), format(_format), compressor(_compressor), expander(_expander), activity(_activity)
- {
- if (outFmt_Binary != format)
- {
- responseWriter.setown(createIXmlWriterExt(0, 0, nullptr, outFmt_Xml == format ? WTStandard : WTJSONObject));
- responseWriter->outputBeginNested("Response", true);
- if (outFmt_Xml == format)
- responseWriter->outputCString("urn:hpcc:dfs", "@xmlns:dfs");
- responseWriter->outputUInt(cursorHandle, sizeof(cursorHandle), "handle");
- }
- }
- OutputFormat queryFormat() const { return format; }
- unsigned __int64 queryReplyLimit() const { return replyLimit; }
- IRemoteActivity *queryActivity() const { return activity; }
- ICompressor *queryCompressor() const { return compressor; }
- void process(IPropertyTree *requestTree, MemoryBuffer &restMb, MemoryBuffer &responseMb, CClientStats &stats)
- {
- if (requestTree->hasProp("replyLimit"))
- replyLimit = requestTree->getPropInt64("replyLimit", defaultDaFSReplyLimitKB) * 1024;
- if (outFmt_Binary == format)
- responseMb.append(cursorHandle);
- else // outFmt_Xml || outFmt_Json
- responseWriter->outputUInt(cursorHandle, sizeof(cursorHandle), "handle");
- if (requestTree->hasProp("cursorBin")) // use handle if one provided
- {
- MemoryBuffer cursorMb;
- cursorMb.setEndian(__BIG_ENDIAN);
- JBASE64_Decode(requestTree->queryProp("cursorBin"), cursorMb);
- activity->restoreCursor(cursorMb);
- }
- if (activity->queryIsReadActivity())
- processRead(requestTree, responseMb);
- else if (activity->queryIsWriteActivity())
- processWrite(requestTree, restMb, responseMb);
- activity->flushStatistics(stats);
- if (outFmt_Binary != format)
- {
- responseWriter->outputEndNested("Response");
- responseWriter->finalize();
- PROGLOG("Response: %s", responseWriter->str());
- responseMb.append(responseWriter->length(), responseWriter->str());
- }
- }
- };
- enum OpenFileFlag { of_null=0x0, of_key=0x01 };
- struct OpenFileInfo
- {
- OpenFileInfo() { }
- OpenFileInfo(int _handle, IFileIO *_fileIO, StringAttrItem *_filename) : handle(_handle), fileIO(_fileIO), filename(_filename) { }
- OpenFileInfo(int _handle, CRemoteRequest *_remoteRequest, StringAttrItem *_filename)
- : handle(_handle), remoteRequest(_remoteRequest), filename(_filename) { }
- Linked<IFileIO> fileIO;
- Linked<CRemoteRequest> remoteRequest;
- Linked<StringAttrItem> filename; // for debug
- int handle = 0;
- unsigned flags = 0;
- };
- static IOutputMetaData *getTypeInfoOutputMetaData(IPropertyTree &actNode, const char *typePropName, bool grouped)
- {
- IPropertyTree *json = actNode.queryPropTree(typePropName);
- if (json)
- return createTypeInfoOutputMetaData(*json, grouped);
- else
- {
- StringBuffer binTypePropName(typePropName);
- const char *jsonBin = actNode.queryProp(binTypePropName.append("Bin"));
- if (!jsonBin)
- return nullptr;
- MemoryBuffer mb;
- JBASE64_Decode(jsonBin, mb);
- return createTypeInfoOutputMetaData(mb, grouped);
- }
- }
- class CRemoteDiskBaseActivity : public CSimpleInterfaceOf<IRemoteReadActivity>, implements IVirtualFieldCallback
- {
- typedef CSimpleInterfaceOf<IRemoteReadActivity> PARENT;
- protected:
- StringAttr fileName; // physical filename
- Linked<IOutputMetaData> inMeta, outMeta;
- unsigned __int64 processed = 0;
- bool outputGrouped = false;
- bool opened = false;
- bool eofSeen = false;
- const RtlRecord *record = nullptr;
- RowFilter filters;
- RtlDynRow *filterRow = nullptr;
- // virtual field values
- StringAttr logicalFilename;
- unsigned numInputFields = 0;
- inline bool fieldFilterMatch(const void * buffer)
- {
- if (filterRow)
- {
- filterRow->setRow(buffer, filters.getNumFieldsRequired());
- return filters.matches(*filterRow);
- }
- else
- return true;
- }
- public:
- IMPLEMENT_IINTERFACE_USING(PARENT);
- CRemoteDiskBaseActivity(IPropertyTree &config, IFileDescriptor *fileDesc)
- {
- fileName.set(config.queryProp("fileName"));
- if (isEmptyString(fileName))
- throw createDafsException(DAFSERR_cmdstream_protocol_failure, "CRemoteDiskBaseActivity: fileName missing");
- logicalFilename.set(config.queryProp("virtualFields/logicalFilename"));
- }
- ~CRemoteDiskBaseActivity()
- {
- delete filterRow;
- }
- void setupInputMeta(const IPropertyTree &config, IOutputMetaData *_inMeta)
- {
- inMeta.setown(_inMeta);
- record = &inMeta->queryRecordAccessor(true);
- numInputFields = record->getNumFields();
- if (config.hasProp("keyFilter"))
- {
- filterRow = new RtlDynRow(*record);
- Owned<IPropertyTreeIterator> filterIter = config.getElements("keyFilter");
- ForEach(*filterIter)
- filters.addFilter(*record, filterIter->query().queryProp(nullptr));
- }
- }
- // IRemoteReadActivity impl.
- virtual unsigned __int64 queryProcessed() const override
- {
- return processed;
- }
- virtual IOutputMetaData *queryOutputMeta() const override
- {
- return outMeta;
- }
- virtual bool isGrouped() const override
- {
- return outputGrouped;
- }
- virtual void serializeCursor(MemoryBuffer &tgt) const override
- {
- throwUnexpected();
- }
- virtual void restoreCursor(MemoryBuffer &src) override
- {
- throwUnexpected();
- }
- virtual void flushStatistics(CClientStats &stats) override
- {
- throwUnexpected();
- }
- virtual IRemoteReadActivity *queryIsReadActivity()
- {
- return this;
- }
- virtual bool requiresPostProject() const override
- {
- return false;
- }
- //interface IVirtualFieldCallback
- virtual const char * queryLogicalFilename(const void * row) override
- {
- return logicalFilename.str();
- }
- virtual unsigned __int64 getFilePosition(const void * row) override
- {
- throwUnexpected();
- }
- virtual unsigned __int64 getLocalFilePosition(const void * row) override
- {
- throwUnexpected();
- }
- virtual const byte * lookupBlob(unsigned __int64 id) override
- {
- throwUnexpected();
- }
- };
- class CRemoteStreamReadBaseActivity : public CRemoteDiskBaseActivity, implements IFileSerialStreamCallback
- {
- typedef CRemoteDiskBaseActivity PARENT;
- protected:
- Owned<ISerialStream> inputStream;
- Owned<IFileIO> iFileIO;
- unsigned __int64 chooseN = 0;
- unsigned __int64 startPos = 0;
- bool compressed = false;
- bool cursorDirty = false;
- // virtual field values
- unsigned partNum = 0;
- offset_t baseFpos = 0;
- unsigned __int64 bytesRead = 0;
- virtual bool refreshCursor()
- {
- if (inputStream->tell() != startPos)
- {
- inputStream->reset(startPos);
- return true;
- }
- return false;
- }
- bool checkOpen() // NB: returns true if this call opened file
- {
- if (opened)
- {
- if (!cursorDirty)
- return false;
- refreshCursor();
- eofSeen = false;
- cursorDirty = false;
- return false;
- }
- cursorDirty = false;
- OwnedIFile iFile = createIFile(fileName);
- assertex(iFile);
- iFileIO.setown(createCompressedFileReader(iFile));
- if (iFileIO)
- {
- if (!compressed)
- {
- WARNLOG("meta info did not mark file '%s' as compressed, but detected file as compressed", fileName.get());
- compressed = true;
- }
- }
- else
- {
- iFileIO.setown(iFile->open(IFOread));
- if (!iFileIO)
- throw createDafsExceptionV(DAFSERR_cmdstream_protocol_failure, "Failed to open: '%s'", fileName.get());
- if (compressed)
- {
- WARNLOG("meta info marked file '%s' as compressed, but detected file as uncompressed", fileName.get());
- compressed = false;
- }
- }
- inputStream.setown(createFileSerialStream(iFileIO, startPos, (offset_t)-1, (size32_t)-1, this));
- opened = true;
- eofSeen = false;
- return true;
- }
- void close()
- {
- iFileIO.clear();
- opened = false;
- eofSeen = true;
- }
- // IFileSerialStreamCallback impl.
- virtual void process(offset_t ofs, size32_t sz, const void *buf) override
- {
- bytesRead += sz;
- }
- public:
- CRemoteStreamReadBaseActivity(IPropertyTree &config, IFileDescriptor *fileDesc) : PARENT(config, fileDesc)
- {
- compressed = config.getPropBool("compressed");
- chooseN = config.getPropInt64("chooseN", defaultFileStreamChooseNLimit);
- partNum = config.getPropInt("virtualFields/partNum");
- baseFpos = (offset_t)config.getPropInt64("virtualFields/baseFpos");
- }
- virtual void flushStatistics(CClientStats &stats) override
- {
- // NB: will be called by same thread that is reading.
- stats.addRead(bytesRead);
- bytesRead = 0;
- }
- // IVirtualFieldCallback impl.
- virtual unsigned __int64 getFilePosition(const void * row) override
- {
- return inputStream->tell() + baseFpos;
- }
- virtual unsigned __int64 getLocalFilePosition(const void * row) override
- {
- return makeLocalFposOffset(partNum, inputStream->tell());
- }
- };
- class CRemoteDiskReadActivity : public CRemoteStreamReadBaseActivity
- {
- typedef CRemoteStreamReadBaseActivity PARENT;
- CThorContiguousRowBuffer prefetchBuffer;
- Owned<ISourceRowPrefetcher> prefetcher;
- bool inputGrouped = false;
- bool eogPending = false;
- bool someInGroup = false;
- Owned<const IDynamicTransform> translator;
- virtual bool refreshCursor() override
- {
- if (prefetchBuffer.tell() != startPos)
- {
- inputStream->reset(startPos);
- prefetchBuffer.clearStream();
- prefetchBuffer.setStream(inputStream);
- return true;
- }
- return false;
- }
- bool checkOpen()
- {
- if (!PARENT::checkOpen()) // returns true if it opened file
- return false;
- prefetchBuffer.setStream(inputStream);
- prefetcher.setown(inMeta->createDiskPrefetcher());
- return true;
- }
- public:
- CRemoteDiskReadActivity(IPropertyTree &config, IFileDescriptor *fileDesc) : PARENT(config, fileDesc), prefetchBuffer(nullptr)
- {
- inputGrouped = config.getPropBool("inputGrouped", false);
- setupInputMeta(config, getTypeInfoOutputMetaData(config, "input", inputGrouped));
- outputGrouped = config.getPropBool("outputGrouped", false);
- if (!inputGrouped && outputGrouped)
- outputGrouped = false; // perhaps should fire error
- outMeta.setown(getTypeInfoOutputMetaData(config, "output", outputGrouped));
- if (!outMeta)
- outMeta.set(inMeta);
- translator.setown(createRecordTranslator(outMeta->queryRecordAccessor(true), *record));
- }
- // IRemoteReadActivity impl.
- virtual const void *nextRow(MemoryBufferBuilder &outBuilder, size32_t &retSz) override
- {
- if (eogPending || eofSeen)
- {
- eogPending = false;
- someInGroup = false;
- retSz = 0;
- return nullptr;
- }
- checkOpen();
- while (!eofSeen && (processed < chooseN))
- {
- while (!prefetchBuffer.eos())
- {
- prefetcher->readAhead(prefetchBuffer);
- size32_t inputRowSz = prefetchBuffer.queryRowSize();
- bool eog = false;
- if (inputGrouped)
- {
- prefetchBuffer.skip(sizeof(eog));
- if (outputGrouped)
- {
- byte b = *(prefetchBuffer.queryRow()+inputRowSz);
- memcpy(&eog, prefetchBuffer.queryRow()+inputRowSz, sizeof(eog));
- }
- }
- const byte *next = prefetchBuffer.queryRow();
- size32_t rowSz; // use local var instead of reference param for efficiency
- if (fieldFilterMatch(next))
- rowSz = translator->translate(outBuilder, *this, next);
- else
- rowSz = 0;
- prefetchBuffer.finishedRow();
- const void *ret = outBuilder.getSelf();
- outBuilder.finishRow(rowSz);
- if (rowSz)
- {
- processed++;
- eogPending = eog;
- someInGroup = true;
- retSz = rowSz;
- return ret;
- }
- else if (eog)
- {
- eogPending = false;
- if (someInGroup)
- {
- someInGroup = false;
- return nullptr;
- }
- }
- }
- eofSeen = true;
- }
- close();
- retSz = 0;
- return nullptr;
- }
- virtual void serializeCursor(MemoryBuffer &tgt) const override
- {
- tgt.append(prefetchBuffer.tell());
- tgt.append(processed);
- tgt.append(someInGroup);
- tgt.append(eogPending);
- }
- virtual void restoreCursor(MemoryBuffer &src) override
- {
- cursorDirty = true;
- src.read(startPos);
- src.read(processed);
- src.read(someInGroup);
- src.read(eogPending);
- }
- virtual StringBuffer &getInfoStr(StringBuffer &out) const override
- {
- return out.appendf("diskread[%s]", fileName.get());
- }
- //interface IVirtualFieldCallback
- virtual unsigned __int64 getFilePosition(const void * row) override
- {
- return prefetchBuffer.tell() + baseFpos;
- }
- };
- class CRemoteExternalFormatReadActivity : public CRemoteStreamReadBaseActivity
- {
- typedef CRemoteStreamReadBaseActivity PARENT;
- protected:
- Owned<const IDynamicFieldValueFetcher> fieldFetcher;
- Owned<const IDynamicTransform> translator;
- bool postProject = false;
- public:
- CRemoteExternalFormatReadActivity(IPropertyTree &config, IFileDescriptor *fileDesc) : PARENT(config, fileDesc)
- {
- setupInputMeta(config, getTypeInfoOutputMetaData(config, "input", false));
- outMeta.setown(getTypeInfoOutputMetaData(config, "output", false));
- const RtlRecord *outRecord = record;
- if (filterRow)
- {
- if (outMeta)
- postProject = true;
- outMeta.set(inMeta);
- }
- else
- {
- if (outMeta)
- outRecord = &outMeta->queryRecordAccessor(true);
- else
- outMeta.set(inMeta);
- }
- translator.setown(createRecordTranslatorViaCallback(*outRecord, *record, type_utf8));
- }
- virtual bool requiresPostProject() const override
- {
- return postProject;
- }
- };
- class CNullNestedRowIterator : public CSimpleInterfaceOf<IDynamicRowIterator>
- {
- public:
- virtual bool first() override { return false; }
- virtual bool next() override { return false; }
- virtual bool isValid() override { return false; }
- virtual IDynamicFieldValueFetcher &query() override
- {
- throwUnexpected();
- }
- } nullNestedRowIterator;
- class CRemoteCsvReadActivity : public CRemoteExternalFormatReadActivity
- {
- typedef CRemoteExternalFormatReadActivity PARENT;
- StringBuffer csvQuote, csvSeparate, csvTerminate, csvEscape;
- unsigned __int64 headerLines = 0;
- unsigned __int64 maxRowSize = 0;
- bool preserveWhitespace = false;
- CSVSplitter csvSplitter;
- class CFieldFetcher : public CSimpleInterfaceOf<IDynamicFieldValueFetcher>
- {
- CSVSplitter &csvSplitter;
- unsigned numInputFields;
- public:
- CFieldFetcher(CSVSplitter &_csvSplitter, unsigned _numInputFields) : csvSplitter(_csvSplitter), numInputFields(_numInputFields)
- {
- }
- virtual const byte *queryValue(unsigned fieldNum, size_t &sz) const override
- {
- dbgassertex(fieldNum < numInputFields);
- sz = csvSplitter.queryLengths()[fieldNum];
- return csvSplitter.queryData()[fieldNum];
- }
- virtual IDynamicRowIterator *getNestedIterator(unsigned fieldNum) const override
- {
- return LINK(&nullNestedRowIterator);
- }
- virtual size_t getSize(unsigned fieldNum) const override { throwUnexpected(); }
- virtual size32_t getRecordSize() const override { throwUnexpected(); }
- };
- bool checkOpen()
- {
- if (!PARENT::checkOpen())
- return false;
- csvSplitter.init(numInputFields, maxRowSize, csvQuote, csvSeparate, csvTerminate, csvEscape, preserveWhitespace);
- if (headerLines)
- {
- do
- {
- size32_t lineLength = csvSplitter.splitLine(inputStream, maxRowSize);
- if (0 == lineLength)
- break;
- inputStream->skip(lineLength);
- }
- while (--headerLines);
- }
- if (!fieldFetcher)
- fieldFetcher.setown(new CFieldFetcher(csvSplitter, numInputFields));
- return true;
- }
- const unsigned defaultMaxCsvRowSize = 10; // MB
- public:
- CRemoteCsvReadActivity(IPropertyTree &config, IFileDescriptor *fileDesc) : PARENT(config, fileDesc)
- {
- maxRowSize = config.getPropInt64("ActivityOptions/maxRowSize", defaultMaxCsvRowSize) * 1024 * 1024;
- preserveWhitespace = config.getPropBool("ActivityOptions/preserveWhitespace");
- if (!config.getProp("ActivityOptions/csvQuote", csvQuote))
- {
- if (!fileDesc->queryProperties().getProp("@csvQuote", csvQuote))
- csvQuote.append("\"");
- }
- if (!config.getProp("ActivityOptions/csvSeparate", csvSeparate))
- {
- if (!fileDesc->queryProperties().getProp("@csvSeparate", csvSeparate))
- csvSeparate.append("\\,");
- }
- if (!config.getProp("ActivityOptions/csvTerminate", csvTerminate))
- {
- if (!fileDesc->queryProperties().getProp("@csvTerminate", csvTerminate))
- csvTerminate.append("\\n,\\r\\n");
- }
- if (!config.getProp("ActivityOptions/csvEscape", csvEscape))
- fileDesc->queryProperties().getProp("@csvEscape", csvEscape);
- headerLines = config.getPropInt64("ActivityOptions/headerLines"); // really this should be a published attribute too
- }
- virtual StringBuffer &getInfoStr(StringBuffer &out) const override
- {
- return out.appendf("csvread[%s]", fileName.get());
- }
- // IRemoteReadActivity impl.
- virtual const void *nextRow(MemoryBufferBuilder &outBuilder, size32_t &retSz) override
- {
- if (eofSeen)
- {
- retSz = 0;
- return nullptr;
- }
- checkOpen();
- while (!eofSeen && (processed < chooseN))
- {
- size32_t lineLength = csvSplitter.splitLine(inputStream, maxRowSize);
- if (!lineLength)
- break;
- retSz = translator->translate(outBuilder, *this, *fieldFetcher);
- dbgassertex(retSz);
- const void *ret = outBuilder.getSelf();
- if (fieldFilterMatch(ret))
- {
- outBuilder.finishRow(retSz);
- ++processed;
- inputStream->skip(lineLength);
- return ret;
- }
- else
- outBuilder.removeBytes(retSz);
- inputStream->skip(lineLength);
- }
- eofSeen = true;
- close();
- retSz = 0;
- return nullptr;
- }
- };
- class CRemoteMarkupReadActivity : public CRemoteExternalFormatReadActivity, implements IXMLSelect
- {
- typedef CRemoteExternalFormatReadActivity PARENT;
- ThorActivityKind kind;
- IXmlToRowTransformer *xmlTransformer;
- Linked<IColumnProvider> lastMatch;
- Owned<IXMLParse> xmlParser;
- bool noRoot = false;
- bool useXmlContents = false;
- // JCSMORE - it would be good if these were cached/reused (can I assume anything using fetcher is single threaded?)
- class CFieldFetcher : public CSimpleInterfaceOf<IDynamicFieldValueFetcher>
- {
- unsigned numInputFields;
- const RtlRecord &recInfo;
- Linked<IColumnProvider> currentMatch;
- const char **compoundXPaths = nullptr;
- const char *queryCompoundXPath(unsigned fieldNum) const
- {
- if (compoundXPaths && compoundXPaths[fieldNum])
- return compoundXPaths[fieldNum];
- else
- return recInfo.queryXPath(fieldNum);
- }
- public:
- CFieldFetcher(const RtlRecord &_recInfo, IColumnProvider *_currentMatch) : recInfo(_recInfo), currentMatch(_currentMatch)
- {
- numInputFields = recInfo.getNumFields();
- // JCSMORE - should this be done (optionally) when RtlRecord is created?
- for (unsigned fieldNum=0; fieldNum<numInputFields; fieldNum++)
- {
- if (recInfo.queryType(fieldNum)->queryChildType())
- {
- const char *xpath = recInfo.queryXPath(fieldNum);
- dbgassertex(xpath);
- const char *ptr = xpath;
- char *expandedXPath = nullptr;
- char *expandedXPathPtr = nullptr;
- while (true)
- {
- if (*ptr == xpathCompoundSeparatorChar)
- {
- if (!compoundXPaths)
- {
- compoundXPaths = new const char *[numInputFields];
- memset(compoundXPaths, 0, sizeof(const char *)*numInputFields);
- }
- size_t sz = strlen(xpath)+1;
- expandedXPath = new char[sz];
- expandedXPathPtr = expandedXPath;
- if (ptr == xpath) // if leading char, just skip
- ++ptr;
- else
- {
- size32_t len = ptr-xpath;
- memcpy(expandedXPath, xpath, len);
- expandedXPathPtr = expandedXPath + len;
- *expandedXPathPtr++ = '/';
- ++ptr;
- }
- while (*ptr)
- {
- if (*ptr == xpathCompoundSeparatorChar)
- {
- *expandedXPathPtr++ = '/';
- ++ptr;
- }
- else
- *expandedXPathPtr++ = *ptr++;
- }
- }
- else
- ptr++;
- if ('\0' == *ptr)
- {
- if (expandedXPath)
- {
- *expandedXPathPtr = '\0';
- compoundXPaths[fieldNum] = expandedXPath;
- }
- break;
- }
- }
- }
- }
- }
- ~CFieldFetcher()
- {
- if (compoundXPaths)
- {
- for (unsigned fieldNum=0; fieldNum<numInputFields; fieldNum++)
- delete [] compoundXPaths[fieldNum];
- delete [] compoundXPaths;
- }
- }
- void setCurrentMatch(IColumnProvider *_currentMatch)
- {
- currentMatch.set(_currentMatch);
- }
- // IDynamicFieldValueFetcher impl.
- virtual const byte *queryValue(unsigned fieldNum, size_t &sz) const override
- {
- dbgassertex(fieldNum < numInputFields);
- dbgassertex(currentMatch);
- size32_t rawSz;
- const char *ret = currentMatch->readRaw(recInfo.queryXPath(fieldNum), rawSz);
- sz = rawSz;
- return (const byte *)ret;
- }
- virtual IDynamicRowIterator *getNestedIterator(unsigned fieldNum) const override
- {
- dbgassertex(fieldNum < numInputFields);
- dbgassertex(currentMatch);
- const RtlRecord *nested = recInfo.queryNested(fieldNum);
- if (!nested)
- return nullptr;
- class CIterator : public CSimpleInterfaceOf<IDynamicRowIterator>
- {
- XmlChildIterator xmlIter;
- Linked<IDynamicFieldValueFetcher> curFieldValueFetcher;
- Linked<IColumnProvider> parentMatch;
- const RtlRecord &nestedRecInfo;
- public:
- CIterator(const RtlRecord &_nestedRecInfo, IColumnProvider *_parentMatch, const char *xpath) : nestedRecInfo(_nestedRecInfo), parentMatch(_parentMatch)
- {
- xmlIter.initOwn(parentMatch->getChildIterator(xpath));
- }
- virtual bool first() override
- {
- IColumnProvider *child = xmlIter.first();
- if (!child)
- {
- curFieldValueFetcher.clear();
- return false;
- }
- curFieldValueFetcher.setown(new CFieldFetcher(nestedRecInfo, child));
- return true;
- }
- virtual bool next() override
- {
- IColumnProvider *child = xmlIter.next();
- if (!child)
- {
- curFieldValueFetcher.clear();
- return false;
- }
- curFieldValueFetcher.setown(new CFieldFetcher(nestedRecInfo, child));
- return true;
- }
- virtual bool isValid() override
- {
- return nullptr != curFieldValueFetcher.get();
- }
- virtual IDynamicFieldValueFetcher &query() override
- {
- assertex(curFieldValueFetcher);
- return *curFieldValueFetcher;
- }
- };
- // JCSMORE - it would be good if these were cached/reused (can I assume anything using parent fetcher is single threaded?)
- return new CIterator(*nested, currentMatch, queryCompoundXPath(fieldNum));
- }
- virtual size_t getSize(unsigned fieldNum) const override { throwUnexpected(); }
- virtual size32_t getRecordSize() const override { throwUnexpected(); }
- };
- bool checkOpen()
- {
- if (!PARENT::checkOpen())
- return false;
- class CSimpleStream : public CSimpleInterfaceOf<ISimpleReadStream>
- {
- Linked<ISerialStream> stream;
- public:
- CSimpleStream(ISerialStream *_stream) : stream(_stream)
- {
- }
- // ISimpleReadStream impl.
- virtual size32_t read(size32_t max_len, void * data) override
- {
- size32_t got;
- const void *res = stream->peek(max_len, got);
- if (got)
- {
- if (got>max_len)
- got = max_len;
- memcpy(data, res, got);
- stream->skip(got);
- }
- return got;
- }
- };
- Owned<ISimpleReadStream> simpleStream = new CSimpleStream(inputStream);
- if (kind==TAKjsonread)
- xmlParser.setown(createJSONParse(*simpleStream, xpath, *this, noRoot?ptr_noRoot:ptr_none, useXmlContents));
- else
- xmlParser.setown(createXMLParse(*simpleStream, xpath, *this, noRoot?ptr_noRoot:ptr_none, useXmlContents));
- if (!fieldFetcher)
- fieldFetcher.setown(new CFieldFetcher(*record, nullptr));
- return true;
- }
- protected:
- StringBuffer xpath;
- StringBuffer customRowTag;
- public:
- IMPLEMENT_IINTERFACE_USING(PARENT);
- CRemoteMarkupReadActivity(IPropertyTree &config, IFileDescriptor *fileDesc, ThorActivityKind _kind) : PARENT(config, fileDesc), kind(_kind)
- {
- config.getProp("ActivityOptions/rowTag", customRowTag);
- noRoot = config.getPropBool("noRoot");
- }
- IColumnProvider *queryMatch() const { return lastMatch; }
- virtual StringBuffer &getInfoStr(StringBuffer &out) const override
- {
- return out.appendf("%s[%s]", getActivityText(kind), fileName.get());
- }
- // IRemoteReadActivity impl.
- virtual const void *nextRow(MemoryBufferBuilder &outBuilder, size32_t &retSz) override
- {
- if (eofSeen)
- {
- retSz = 0;
- return nullptr;
- }
- checkOpen();
- while (xmlParser->next())
- {
- if (lastMatch)
- {
- ((CFieldFetcher *)fieldFetcher.get())->setCurrentMatch(lastMatch);
- retSz = translator->translate(outBuilder, *this, *fieldFetcher);
- dbgassertex(retSz);
- lastMatch.clear();
- const void *ret = outBuilder.getSelf();
- if (fieldFilterMatch(ret))
- {
- outBuilder.finishRow(retSz);
- ++processed;
- return ret;
- }
- else
- outBuilder.removeBytes(retSz);
- }
- }
- eofSeen = true;
- close();
- retSz = 0;
- return nullptr;
- }
- // IXMLSelect impl.
- virtual void match(IColumnProvider &entry, offset_t startOffset, offset_t endOffset)
- {
- lastMatch.set(&entry);
- }
- };
- class CRemoteXmlReadActivity : public CRemoteMarkupReadActivity
- {
- typedef CRemoteMarkupReadActivity PARENT;
- public:
- CRemoteXmlReadActivity(IPropertyTree &config, IFileDescriptor *fileDesc) : PARENT(config, fileDesc, TAKxmlread)
- {
- xpath.set("/Dataset/");
- if (customRowTag.isEmpty()) // no override
- fileDesc->queryProperties().getProp("@rowTag", xpath);
- else
- xpath.append(customRowTag);
- }
- };
- class CRemoteJsonReadActivity : public CRemoteMarkupReadActivity
- {
- typedef CRemoteMarkupReadActivity PARENT;
- public:
- CRemoteJsonReadActivity(IPropertyTree &config, IFileDescriptor *fileDesc) : PARENT(config, fileDesc, TAKjsonread)
- {
- xpath.set("/");
- if (customRowTag.isEmpty()) // no override
- fileDesc->queryProperties().getProp("@rowTag", xpath);
- else
- xpath.append(customRowTag);
- }
- };
- /* A IRemoteReadActivity that projects to output format
- * Created if input activity requires filtering, but it must 1st translate from external format to the actual format
- * NB: processor, grouped and cursor are same as input.
- */
- class CRemoteCompoundReadProjectActivity : public CSimpleInterfaceOf<IRemoteReadActivity>
- {
- Linked<IRemoteReadActivity> input;
- Owned<IOutputMetaData> outMeta;
- Owned<const IDynamicTransform> translator;
- UnexpectedVirtualFieldCallback fieldCallback;
- MemoryBuffer inputRowMb;
- MemoryBufferBuilder *inputRowBuilder;
- public:
- CRemoteCompoundReadProjectActivity(IPropertyTree &config, IRemoteReadActivity *_input) : input(_input)
- {
- IOutputMetaData *inMeta = input->queryOutputMeta();
- outMeta.setown(getTypeInfoOutputMetaData(config, "output", false));
- dbgassertex(outMeta);
- const RtlRecord &inRecord = inMeta->queryRecordAccessor(true);
- const RtlRecord &outRecord = outMeta->queryRecordAccessor(true);
- translator.setown(createRecordTranslator(outRecord, inRecord));
- inputRowBuilder = new MemoryBufferBuilder(inputRowMb, inMeta->getMinRecordSize());
- }
- ~CRemoteCompoundReadProjectActivity()
- {
- delete inputRowBuilder;
- }
- virtual StringBuffer &getInfoStr(StringBuffer &out) const override
- {
- return input->getInfoStr(out).append(" - CompoundProject");
- }
- // IRemoteReadActivity impl.
- virtual unsigned __int64 queryProcessed() const override
- {
- return input->queryProcessed();
- }
- virtual IOutputMetaData *queryOutputMeta() const override
- {
- return outMeta;
- }
- virtual bool isGrouped() const override
- {
- return input->isGrouped();
- }
- virtual void serializeCursor(MemoryBuffer &tgt) const override
- {
- input->serializeCursor(tgt);
- }
- virtual void restoreCursor(MemoryBuffer &src) override
- {
- input->restoreCursor(src);
- }
- virtual void flushStatistics(CClientStats &stats) override
- {
- input->flushStatistics(stats);
- }
- virtual IRemoteReadActivity *queryIsReadActivity()
- {
- return this;
- }
- virtual const void *nextRow(MemoryBufferBuilder &outBuilder, size32_t &retSz) override
- {
- size32_t rowSz;
- const void *row = input->nextRow(*inputRowBuilder, rowSz);
- if (!row)
- {
- retSz = 0;
- return nullptr;
- }
- retSz = translator->translate(outBuilder, fieldCallback, (const byte *)row);
- const void *ret = outBuilder.getSelf();
- outBuilder.finishRow(retSz);
- return ret;
- }
- virtual bool requiresPostProject() const override
- {
- return false;
- }
- };
- class CRemoteIndexBaseActivity : public CRemoteDiskBaseActivity
- {
- typedef CRemoteDiskBaseActivity PARENT;
- protected:
- bool isTlk = false;
- bool allowPreload = false;
- unsigned fileCrc = 0;
- Owned<IKeyIndex> keyIndex;
- Owned<IKeyManager> keyManager;
- void checkOpen()
- {
- if (opened)
- return;
- Owned<IFile> indexFile = createIFile(fileName);
- CDateTime modTime;
- indexFile->getTime(nullptr, &modTime, nullptr);
- time_t modTimeTT = modTime.getSimple();
- CRC32 crc32(fileCrc);
- crc32.tally(sizeof(time_t), &modTimeTT);
- unsigned crc = crc32.get();
- keyIndex.setown(createKeyIndex(fileName, crc, isTlk, allowPreload));
- keyManager.setown(createLocalKeyManager(*record, keyIndex, nullptr, true, false));
- filters.createSegmentMonitors(keyManager);
- keyManager->finishSegmentMonitors();
- keyManager->reset();
- opened = true;
- }
- void close()
- {
- keyManager.clear();
- keyIndex.clear();
- opened = false;
- eofSeen = true;
- }
- public:
- CRemoteIndexBaseActivity(IPropertyTree &config, IFileDescriptor *fileDesc) : PARENT(config, fileDesc)
- {
- setupInputMeta(config, getTypeInfoOutputMetaData(config, "input", false));
- isTlk = config.getPropBool("isTlk");
- allowPreload = config.getPropBool("allowPreload");
- fileCrc = config.getPropInt("crc");
- }
- virtual void flushStatistics(CClientStats &stats) override
- {
- // TBD, IKeyCursor should probably have a getStatistic(StatisticKind kind) implementation
- }
- };
- class CRemoteIndexReadActivity : public CRemoteIndexBaseActivity
- {
- typedef CRemoteIndexBaseActivity PARENT;
- Owned<const IDynamicTransform> translator;
- unsigned __int64 chooseN = 0;
- public:
- CRemoteIndexReadActivity(IPropertyTree &config, IFileDescriptor *fileDesc) : PARENT(config, fileDesc)
- {
- chooseN = config.getPropInt64("chooseN", defaultFileStreamChooseNLimit);
- outMeta.setown(getTypeInfoOutputMetaData(config, "output", false));
- if (outMeta)
- translator.setown(createRecordTranslator(outMeta->queryRecordAccessor(true), *record));
- else
- outMeta.set(inMeta);
- }
- // IRemoteReadActivity impl.
- virtual const void *nextRow(MemoryBufferBuilder &outBuilder, size32_t &retSz) override
- {
- if (eofSeen)
- {
- retSz = 0;
- return nullptr;
- }
- checkOpen();
- if (!eofSeen)
- {
- if (processed < chooseN)
- {
- while (keyManager->lookup(true))
- {
- const byte *keyRow = keyManager->queryKeyBuffer();
- if (fieldFilterMatch(keyRow))
- {
- if (translator)
- retSz = translator->translate(outBuilder, *this, keyRow);
- else
- {
- retSz = keyManager->queryRowSize();
- outBuilder.ensureCapacity(retSz, nullptr);
- memcpy(outBuilder.getSelf(), keyRow, retSz);
- }
- dbgassertex(retSz);
- const void *ret = outBuilder.getSelf();
- outBuilder.finishRow(retSz);
- ++processed;
- return ret;
- }
- }
- retSz = 0;
- }
- eofSeen = true;
- }
- close();
- return nullptr;
- }
- virtual void serializeCursor(MemoryBuffer &tgt) const override
- {
- keyManager->serializeCursorPos(tgt);
- tgt.append(processed);
- /* JCSMORE (see HPCC-19640), serialize seek/scan data to client
- tgt.append(keyManager->querySeeks());
- tgt.append(keyManager->queryScans());
- */
- }
- virtual void restoreCursor(MemoryBuffer &src) override
- {
- checkOpen();
- eofSeen = false;
- keyManager->deserializeCursorPos(src);
- src.read(processed);
- }
- virtual StringBuffer &getInfoStr(StringBuffer &out) const override
- {
- return out.appendf("indexread[%s]", fileName.get());
- }
- };
- class CRemoteWriteBaseActivity : public CSimpleInterfaceOf<IRemoteWriteActivity>
- {
- protected:
- StringAttr fileName; // physical filename
- Linked<IOutputMetaData> meta;
- unsigned __int64 processed = 0;
- unsigned __int64 bytesWritten = 0;
- bool opened = false;
- bool eofSeen = false;
- Owned<IFileIO> iFileIO;
- bool grouped = false;
- void close()
- {
- iFileIO.clear();
- opened = false;
- eofSeen = true;
- }
- public:
- CRemoteWriteBaseActivity(IPropertyTree &config, IFileDescriptor *fileDesc)
- {
- fileName.set(config.queryProp("fileName"));
- if (isEmptyString(fileName))
- throw createDafsException(DAFSERR_cmdstream_protocol_failure, "CRemoteWriteBaseActivity: fileName missing");
- grouped = config.getPropBool("inputGrouped");
- meta.setown(getTypeInfoOutputMetaData(config, "input", grouped));
- }
- ~CRemoteWriteBaseActivity()
- {
- }
- // IRemoteWriteActivity impl.
- virtual unsigned __int64 queryProcessed() const override
- {
- return processed;
- }
- virtual IOutputMetaData *queryOutputMeta() const override
- {
- return meta;
- }
- virtual bool isGrouped() const override
- {
- return grouped;
- }
- virtual void serializeCursor(MemoryBuffer &tgt) const override
- {
- throwUnexpected();
- }
- virtual void restoreCursor(MemoryBuffer &src) override
- {
- throwUnexpected();
- }
- virtual StringBuffer &getInfoStr(StringBuffer &out) const override
- {
- return out.appendf("diskwrite[%s]", fileName.get());
- }
- virtual void write(size32_t sz, const void *rowData) override
- {
- throwUnexpected(); // method should be implemented in derived classes.
- }
- virtual void flushStatistics(CClientStats &stats) override
- {
- // NB: will be called by same thread that is writing.
- stats.addWrite(bytesWritten);
- bytesWritten = 0;
- }
- virtual IRemoteWriteActivity *queryIsWriteActivity()
- {
- return this;
- }
- };
- class CRemoteDiskWriteActivity : public CRemoteWriteBaseActivity
- {
- typedef CRemoteWriteBaseActivity PARENT;
- unsigned compressionFormat = 0;
- bool eogPending = false;
- bool someInGroup = false;
- size32_t recordSize = 0;
- Owned<IFileIOStream> iFileIOStream;
- bool append = false;
- void checkOpen()
- {
- if (opened)
- return;
- if (!recursiveCreateDirectoryForFile(fileName))
- throw createDafsExceptionV(DAFSERR_cmdstream_openfailure, "Failed to create dirtory for file: '%s'", fileName.get());
- OwnedIFile iFile = createIFile(fileName);
- assertex(iFile);
- /* NB: if concurrent writers were supported, then would need mutex here, during open/create
- * multiple activities, each with there own handle would be possible, with mutex during write.
- * Would need mutex per physical filename active.
- */
- if (compressionFormat)
- iFileIO.setown(createCompressedFileWriter(iFile, recordSize, append, true, nullptr, compressionFormat));
- else
- {
- iFileIO.setown(iFile->open(append ? IFOwrite : IFOcreate));
- if (!iFileIO)
- throw createDafsExceptionV(DAFSERR_cmdstream_openfailure, "Failed to open: '%s' for write", fileName.get());
- }
- iFileIOStream.setown(createIOStream(iFileIO));
- if (append)
- iFileIOStream->seek(0, IFSend);
- opened = true;
- eofSeen = false;
- }
- public:
- CRemoteDiskWriteActivity(IPropertyTree &config, IFileDescriptor *fileDesc) : PARENT(config, fileDesc)
- {
- const char *compressed = config.queryProp("compressed"); // the compression format for the serialized rows in the transport
- if (!isEmptyString(compressed))
- {
- // boolean or format allowed
- if (strieq("true", compressed))
- compressionFormat = translateToCompMethod(nullptr); // gets default
- else if (strieq("false", compressed))
- compressionFormat = 0;
- else
- compressionFormat = translateToCompMethod(compressed);
- }
- append = config.getPropBool("append");
- }
- virtual void write(size32_t sz, const void *rowData) override
- {
- checkOpen();
- iFileIOStream->write(sz, rowData);
- bytesWritten += sz;
- }
- virtual void serializeCursor(MemoryBuffer &tgt) const override
- {
- tgt.append(iFileIOStream->tell());
- }
- virtual void restoreCursor(MemoryBuffer &src) override
- {
- offset_t pos;
- src.read(pos);
- checkOpen();
- iFileIOStream->seek(pos, IFSbegin);
- }
- };
- // create a { unsigned8 } output meta for the count
- static const RtlIntTypeInfo indexCountFieldType(type_unsigned|type_int, 8);
- static const RtlFieldStrInfo indexCountField("count", nullptr, &indexCountFieldType);
- static const RtlFieldInfo * const indexCountFields[2] = { &indexCountField, nullptr };
- static const RtlRecordTypeInfo indexCountRecord(type_record, 2, indexCountFields);
- class CRemoteIndexCountActivity : public CRemoteIndexBaseActivity
- {
- typedef CRemoteIndexBaseActivity PARENT;
- unsigned __int64 rowLimit = 0;
- public:
- CRemoteIndexCountActivity(IPropertyTree &config, IFileDescriptor *fileDesc) : PARENT(config, fileDesc)
- {
- rowLimit = config.getPropInt64("chooseN");
- outMeta.setown(new CDynamicOutputMetaData(indexCountRecord));
- }
- // IRemoteReadActivity impl.
- virtual const void *nextRow(MemoryBufferBuilder &outBuilder, size32_t &retSz) override
- {
- if (eofSeen)
- {
- retSz = 0;
- return nullptr;
- }
- checkOpen();
- unsigned __int64 count = 0;
- if (!eofSeen)
- {
- if (rowLimit)
- count = keyManager->checkCount(rowLimit);
- else
- count = keyManager->getCount();
- }
- void *tgt = outBuilder.ensureCapacity(sizeof(count), "count");
- const void *ret = outBuilder.getSelf();
- memcpy(tgt, &count, sizeof(count));
- outBuilder.finishRow(sizeof(count));
- close();
- return ret;
- }
- virtual StringBuffer &getInfoStr(StringBuffer &out) const override
- {
- return out.appendf("indexcount[%s]", fileName.get());
- }
- };
- void checkExpiryTime(IPropertyTree &metaInfo)
- {
- const char *expiryTime = metaInfo.queryProp("expiryTime");
- if (isEmptyString(expiryTime))
- throw createDafsException(DAFSERR_cmdstream_invalidexpiry, "createRemoteActivity: invalid expiry specification");
- CDateTime expiryTimeDt;
- try
- {
- expiryTimeDt.setString(expiryTime);
- }
- catch (IException *e)
- {
- e->Release();
- throw createDafsException(DAFSERR_cmdstream_invalidexpiry, "createRemoteActivity: invalid expiry specification");
- }
- CDateTime nowDt;
- nowDt.setNow();
- if (nowDt >= expiryTimeDt)
- throw createDafsException(DAFSERR_cmdstream_authexpired, "createRemoteActivity: authorization expired");
- }
- IFileDescriptor *verifyMetaInfo(IPropertyTree &actNode, bool authorizedOnly, const IPropertyTree *keyPairInfo)
- {
- if (!authorizedOnly) // if configured false, allows unencrypted meta info
- {
- if (actNode.hasProp("fileName"))
- return nullptr;
- }
- StringBuffer metaInfoB64;
- actNode.getProp("metaInfo", metaInfoB64);
- if (0 == metaInfoB64.length())
- throw createDafsException(DAFSERR_cmdstream_protocol_failure, "createRemoteActivity: missing metaInfo");
- MemoryBuffer compressedMetaInfoMb;
- JBASE64_Decode(metaInfoB64.str(), compressedMetaInfoMb);
- MemoryBuffer decompressedMetaInfoMb;
- fastLZDecompressToBuffer(decompressedMetaInfoMb, compressedMetaInfoMb);
- Owned<IPropertyTree> metaInfoEnvelope = createPTree(decompressedMetaInfoMb);
- Owned<IPropertyTree> metaInfo;
- #if defined(_USE_OPENSSL)
- MemoryBuffer metaInfoBlob;
- metaInfoEnvelope->getPropBin("metaInfoBlob", metaInfoBlob);
- bool isSigned = metaInfoBlob.length() != 0;
- if (authorizedOnly && !isSigned)
- throw createDafsException(DAFSERR_cmdstream_unauthorized, "createRemoteActivity: unathorized");
- if (isSigned)
- {
- metaInfo.setown(createPTree(metaInfoBlob));
- const char *keyPairName = metaInfo->queryProp("keyPairName");
- StringBuffer metaInfoSignature;
- if (!metaInfoEnvelope->getProp("signature", metaInfoSignature))
- throw createDafsException(DAFSERR_cmdstream_unauthorized, "createRemoteActivity: missing signature");
- VStringBuffer keyPairPath("KeyPair[@name=\"%s\"]", keyPairName);
- IPropertyTree *keyPair = keyPairInfo->queryPropTree(keyPairPath);
- if (!keyPair)
- throw createDafsException(DAFSERR_cmdstream_unauthorized, "createRemoteActivity: missing key pair definition");
- const char *publicKeyFName = keyPair->queryProp("@publicKey");
- if (isEmptyString(publicKeyFName))
- throw createDafsException(DAFSERR_cmdstream_unauthorized, "createRemoteActivity: missing public key definition");
- Owned<CLoadedKey> publicKey = loadPublicKeyFromFile(publicKeyFName); // NB: if cared could cache loaded keys
- if (!digiVerify(metaInfoSignature, metaInfoBlob.length(), metaInfoBlob.bytes(), *publicKey))
- throw createDafsException(DAFSERR_cmdstream_unauthorized, "createRemoteActivity: signature verification failed");
- checkExpiryTime(*metaInfo);
- }
- else
- #endif
- metaInfo.set(metaInfoEnvelope);
- assertex(actNode.hasProp("filePart"));
- unsigned partNum = actNode.getPropInt("filePart");
- assertex(partNum);
- unsigned partCopy = actNode.getPropInt("filePartCopy", 1);
- Owned<IFileDescriptor> fileDesc;
- unsigned metaInfoVersion = metaInfo->getPropInt("version");
- switch (metaInfoVersion)
- {
- case 0:
- // implies unsigned direct request from engines (on unsecure port)
- // fall through
- case 1: // legacy
- {
- IPropertyTree *fileInfo = metaInfo->queryPropTree("FileInfo");
- assertex(fileInfo);
- VStringBuffer xpath("Part[%u]/Copy[%u]/@filePath", partNum, partCopy);
- StringBuffer partFileName;
- fileInfo->getProp(xpath, partFileName);
- if (!partFileName.length())
- throw createDafsException(DAFSERR_cmdstream_protocol_failure, "createRemoteActivity: invalid file info");
- actNode.setProp("fileName", partFileName.str());
- break;
- }
- case 2: // serialized compact IFileDescriptor
- {
- IPropertyTree *fileInfo = metaInfo->queryPropTree("FileInfo");
- fileDesc.setown(deserializeFileDescriptorTree(fileInfo));
- RemoteFilename rfn;
- fileDesc->getFilename(partNum-1, partCopy-1, rfn);
- StringBuffer path;
- rfn.getLocalPath(path);
- actNode.setProp("fileName", path.str());
- break;
- }
- default:
- throwUnexpected();
- }
- verifyex(actNode.removeProp("metaInfo")); // no longer needed
- return fileDesc.getClear();
- }
- template<class ActivityClass> IRemoteReadActivity *createConditionalProjectingActivity(IPropertyTree &actNode, IFileDescriptor *fileDesc)
- {
- Owned<IRemoteReadActivity> activity = new ActivityClass(actNode, fileDesc);
- if (activity->requiresPostProject())
- return new CRemoteCompoundReadProjectActivity(actNode, activity);
- else
- return activity.getClear();
- }
- IRemoteActivity *createRemoteActivity(IPropertyTree &actNode, bool authorizedOnly, const IPropertyTree *keyPairInfo)
- {
- Owned<IFileDescriptor> fileDesc = verifyMetaInfo(actNode, authorizedOnly, keyPairInfo);
- const char *partFileName = actNode.queryProp("fileName");
- const char *kindStr = actNode.queryProp("kind");
- ThorActivityKind kind = TAKnone;
- if (kindStr)
- {
- if (strieq("diskread", kindStr))
- kind = TAKdiskread;
- if (strieq("csvread", kindStr))
- kind = TAKcsvread;
- else if (strieq("xmlread", kindStr))
- kind = TAKxmlread;
- else if (strieq("jsonread", kindStr))
- kind = TAKjsonread;
- else if (strieq("indexread", kindStr))
- kind = TAKindexread;
- else if (strieq("indexcount", kindStr))
- kind = TAKindexcount;
- else if (strieq("diskwrite", kindStr))
- kind = TAKdiskwrite;
- else if (strieq("indexwrite", kindStr))
- kind = TAKindexwrite;
- // else - auto-detect
- }
- Owned<IRemoteActivity> activity;
- switch (kind)
- {
- case TAKdiskread:
- {
- activity.setown(new CRemoteDiskReadActivity(actNode, fileDesc));
- break;
- }
- case TAKcsvread:
- {
- activity.setown(createConditionalProjectingActivity<CRemoteCsvReadActivity>(actNode, fileDesc));
- break;
- }
- case TAKxmlread:
- {
- activity.setown(createConditionalProjectingActivity<CRemoteXmlReadActivity>(actNode, fileDesc));
- break;
- }
- case TAKjsonread:
- {
- activity.setown(createConditionalProjectingActivity<CRemoteJsonReadActivity>(actNode, fileDesc));
- break;
- }
- case TAKindexread:
- {
- activity.setown(new CRemoteIndexReadActivity(actNode, fileDesc));
- break;
- }
- case TAKindexcount:
- {
- activity.setown(new CRemoteIndexCountActivity(actNode, fileDesc));
- break;
- }
- case TAKdiskwrite:
- {
- activity.setown(new CRemoteDiskWriteActivity(actNode, fileDesc));
- break;
- }
- default: // in absense of type, read is assumed and file format is auto-detected.
- {
- const char *action = actNode.queryProp("action");
- if (isIndexFile(partFileName))
- {
- if (!isEmptyString(action))
- {
- if (streq("count", action))
- activity.setown(new CRemoteIndexCountActivity(actNode, fileDesc));
- else
- throw createDafsExceptionV(DAFSERR_cmdstream_protocol_failure, "Unknown action '%s' on index '%s'", action, partFileName);
- }
- else
- activity.setown(new CRemoteIndexReadActivity(actNode, fileDesc));
- }
- else
- {
- if (!isEmptyString(action))
- {
- if (streq("count", action))
- throw createDafsException(DAFSERR_cmdstream_protocol_failure, "Remote Disk Counts currently unsupported");
- else
- throw createDafsExceptionV(DAFSERR_cmdstream_protocol_failure, "Unknown action '%s' on flat file '%s'", action, partFileName);
- }
- else
- {
- const char *kind = queryFileKind(fileDesc);
- if (isEmptyString(kind) || (streq("flat", kind)))
- activity.setown(new CRemoteDiskReadActivity(actNode, fileDesc));
- else if (streq("csv", kind))
- activity.setown(createConditionalProjectingActivity<CRemoteCsvReadActivity>(actNode, fileDesc));
- else if (streq("xml", kind))
- activity.setown(createConditionalProjectingActivity<CRemoteXmlReadActivity>(actNode, fileDesc));
- else if (streq("json", kind))
- activity.setown(createConditionalProjectingActivity<CRemoteJsonReadActivity>(actNode, fileDesc));
- else
- throw createDafsExceptionV(DAFSERR_cmdstream_protocol_failure, "Unknown file kind '%s'", kind);
- }
- }
- break;
- }
- }
- return activity.getClear();
- }
- IRemoteActivity *createOutputActivity(IPropertyTree &requestTree, bool authorizedOnly, const IPropertyTree *keyPairInfo)
- {
- IPropertyTree *actNode = requestTree.queryPropTree("node");
- assertex(actNode);
- return createRemoteActivity(*actNode, authorizedOnly, keyPairInfo);
- }
- #define MAX_KEYDATA_SZ 0x10000
- class CRemoteFileServer : implements IRemoteFileServer, public CInterface
- {
- class CThrottler;
- class CRemoteClientHandler : implements ISocketSelectNotify, public CInterface
- {
- bool calledByRowService;
- public:
- CRemoteFileServer *parent;
- Owned<ISocket> socket;
- StringAttr peerName;
- MemoryBuffer msg;
- bool selecthandled;
- size32_t left;
- StructArrayOf<OpenFileInfo> openFiles;
- Owned<IDirectoryIterator> opendir;
- unsigned lasttick, lastInactiveTick;
- atomic_t &globallasttick;
- unsigned previdx; // for debug
- IMPLEMENT_IINTERFACE;
- CRemoteClientHandler(CRemoteFileServer *_parent,ISocket *_socket,atomic_t &_globallasttick, bool _calledByRowService)
- : socket(_socket), globallasttick(_globallasttick), calledByRowService(_calledByRowService)
- {
- previdx = (unsigned)-1;
- StringBuffer peerBuf;
- char name[256];
- name[0] = 0;
- int port = socket->peer_name(name,sizeof(name)-1);
- if (port>=0)
- {
- peerBuf.append(name);
- if (port)
- peerBuf.append(':').append(port);
- peerName.set(peerBuf);
- }
- else
- {
- /* There's a possibility the socket closed before got here, in which case, peer name is unavailable
- * May potentially be unavailable for other reasons also.
- * Must be set, as used in client stats HT.
- * If socket closed, the handler will start up but notice closed and quit
- */
- peerName.set("UNKNOWN PEER NAME");
- }
- {
- CriticalBlock block(ClientCountSect);
- if (++ClientCount>MaxClientCount)
- MaxClientCount = ClientCount;
- if (TF_TRACE_CLIENT_CONN)
- {
- StringBuffer s;
- s.appendf("Connecting(%p) [%d,%d] to ",this,ClientCount,MaxClientCount);
- s.append(peerName);
- PROGLOG("%s", s.str());
- }
- }
- parent = _parent;
- left = 0;
- msg.setEndian(__BIG_ENDIAN);
- selecthandled = false;
- touch();
- }
- ~CRemoteClientHandler()
- {
- {
- CriticalBlock block(ClientCountSect);
- ClientCount--;
- if (TF_TRACE_CLIENT_CONN) {
- PROGLOG("Disconnecting(%p) [%d,%d] ",this,ClientCount,MaxClientCount);
- }
- }
- ISocket *sock = socket.getClear();
- try {
- sock->Release();
- }
- catch (IException *e) {
- EXCLOG(e,"~CRemoteClientHandler");
- e->Release();
- }
- }
- bool isRowServiceClient() const { return calledByRowService; }
- bool notifySelected(ISocket *sock,unsigned selected)
- {
- if (TF_TRACE_FULL)
- PROGLOG("notifySelected(%p)",this);
- if (sock!=socket)
- WARNLOG("notifySelected - invalid socket passed");
- size32_t avail = (size32_t)socket->avail_read();
- if (avail)
- touch();
- else if (left)
- {
- WARNLOG("notifySelected: Closing mid packet, %d remaining", left);
- msg.clear();
- parent->notify(this, msg); // notifying of graceful close
- return false;
- }
- if (left==0)
- {
- try
- {
- left = avail?receiveDaFsBufferSize(socket):0;
- }
- catch (IException *e)
- {
- EXCLOG(e,"notifySelected(1)");
- e->Release();
- left = 0;
- }
- if (left)
- {
- avail = (size32_t)socket->avail_read();
- try
- {
- msg.ensureCapacity(left);
- }
- catch (IException *e)
- {
- EXCLOG(e,"notifySelected(2)");
- e->Release();
- left = 0;
- // if too big then corrupted packet so read avail to try and consume
- char fbuf[1024];
- while (avail)
- {
- size32_t rd = avail>sizeof(fbuf)?sizeof(fbuf):avail;
- try
- {
- socket->read(fbuf, rd); // don't need timeout here
- avail -= rd;
- }
- catch (IException *e)
- {
- EXCLOG(e,"notifySelected(2) flush");
- e->Release();
- break;
- }
- }
- avail = 0;
- left = 0;
- }
- }
- }
- size32_t toread = left>avail?avail:left;
- if (toread)
- {
- try
- {
- socket->read(msg.reserve(toread), toread); // don't need timeout here
- }
- catch (IException *e)
- {
- EXCLOG(e,"notifySelected(3)");
- e->Release();
- toread = left;
- msg.clear();
- }
- }
- if (TF_TRACE_FULL)
- PROGLOG("notifySelected %d,%d",toread,left);
- left -= toread;
- if (left==0)
- {
- // DEBUG
- parent->notify(this, msg); // consumes msg
- }
- return false;
- }
- void logPrevHandle()
- {
- if (previdx<openFiles.ordinality())
- {
- const OpenFileInfo &fileInfo = openFiles.item(previdx);
- PROGLOG("Previous handle(%d): %s", fileInfo.handle, fileInfo.filename->text.get());
- }
- }
- bool throttleCommand(MemoryBuffer &msg)
- {
- RemoteFileCommandType cmd = RFCunknown;
- Owned<IException> e;
- try
- {
- msg.read(cmd);
- parent->throttleCommand(cmd, msg, this);
- return true;
- }
- catch (IException *_e)
- {
- e.setown(_e);
- }
- /* processCommand() will handle most exception and replies,
- * but if throttleCommand fails before it gets that far, this will handle
- */
- MemoryBuffer reply;
- initSendBuffer(reply);
- unsigned err = (cmd == RFCopenIO) ? RFSERR_OpenFailed : 0;
- parent->formatException(reply, e, cmd, false, err, this);
- sendDaFsBuffer(socket, reply);
- return false;
- }
- void processCommand(RemoteFileCommandType cmd, MemoryBuffer &msg, CThrottler *throttler)
- {
- MemoryBuffer reply;
- bool testSocketFlag = parent->processCommand(cmd, msg, initSendBuffer(reply), this, throttler);
- sendDaFsBuffer(socket, reply, testSocketFlag);
- }
- bool immediateCommand() // returns false if socket closed or failure
- {
- MemoryBuffer msg;
- msg.setEndian(__BIG_ENDIAN);
- touch();
- size32_t avail = (size32_t)socket->avail_read();
- if (avail==0)
- return false;
- receiveDaFsBuffer(socket, msg, 5); // shouldn't timeout as data is available
- touch();
- if (msg.length()==0)
- return false;
- return throttleCommand(msg);
- }
- void process(MemoryBuffer &msg)
- {
- if (selecthandled)
- throttleCommand(msg);
- else
- {
- // msg only used/filled if process() has been triggered by notify()
- while (parent->threadRunningCount()<=parent->targetActiveThreads) // if too many threads add to select handler
- {
- int w;
- try
- {
- w = socket->wait_read(1000);
- }
- catch (IException *e)
- {
- EXCLOG(e, "CRemoteClientHandler::main wait_read error");
- e->Release();
- parent->onCloseSocket(this,1);
- return;
- }
- if (w==0)
- break;
- if ((w<0)||!immediateCommand())
- {
- if (w<0)
- WARNLOG("CRemoteClientHandler::main wait_read error");
- parent->onCloseSocket(this,1);
- return;
- }
- }
- /* This is a bit confusing..
- * The addClient below, adds this request to a selecthandler handled by another thread
- * and passes ownership of 'this' (CRemoteClientHandler)
- *
- * When notified, the selecthandler will launch a new pool thread to handle the request
- * If the pool thread limit is hit, the selecthandler will be blocked [ see comment in CRemoteFileServer::notify() ]
- *
- * Either way, a thread pool slot is occupied when processing a request.
- * Blocked threads, will be blocked for up to 1 minute (as defined by createThreadPool call)
- * IOW, if there are lots of incoming clients that can't be serviced by the CThrottler limit,
- * a large number of pool threads will build up after a while.
- *
- * The CThrottler mechanism, imposes a further hard limit on how many concurrent request threads can be active.
- * If the thread pool had an absolute limit (instead of just introducing a delay), then I don't see the point
- * in this additional layer of throttling..
- */
- selecthandled = true;
- parent->addClient(this); // add to select handler
- // NB: this (CRemoteClientHandler) is now linked by the selecthandler and owned by the 'clients' list
- }
- }
- bool timedOut()
- {
- return (msTick()-lasttick)>CLIENT_TIMEOUT;
- }
- bool inactiveTimedOut()
- {
- unsigned ms = msTick();
- if ((ms-lastInactiveTick)>CLIENT_INACTIVEWARNING_TIMEOUT)
- {
- lastInactiveTick = ms;
- return true;
- }
- return false;
- }
- void touch()
- {
- lastInactiveTick = lasttick = msTick();
- atomic_set(&globallasttick,lasttick);
- }
- const char *queryPeerName()
- {
- return peerName;
- }
- bool getInfo(StringBuffer &str)
- {
- str.append("client(");
- const char *name = queryPeerName();
- bool ok;
- if (name)
- {
- ok = true;
- str.append(name);
- }
- else
- ok = false;
- unsigned ms = msTick();
- str.appendf("): last touch %d ms ago (%d, %d)",ms-lasttick,lasttick,ms);
- ForEachItemIn(i, openFiles)
- {
- const OpenFileInfo &fileInfo = openFiles.item(i);
- str.appendf("\n %d: ", fileInfo.handle);
- str.append(fileInfo.filename->text.get());
- }
- return ok;
- }
- };
- class CThrottleQueueItem : public CSimpleInterface
- {
- public:
- RemoteFileCommandType cmd;
- Linked<CRemoteClientHandler> client;
- MemoryBuffer msg;
- CCycleTimer timer;
- CThrottleQueueItem(RemoteFileCommandType _cmd, MemoryBuffer &_msg, CRemoteClientHandler *_client) : cmd(_cmd), client(_client)
- {
- msg.swapWith(_msg);
- }
- };
- class CThrottler
- {
- Semaphore sem;
- CriticalSection crit, configureCrit;
- StringAttr title;
- unsigned limit, delayMs, cpuThreshold, queueLimit;
- unsigned disabledLimit;
- unsigned __int64 totalThrottleDelay;
- CCycleTimer totalThrottleDelayTimer;
- QueueOf<CThrottleQueueItem, false> queue;
- unsigned statsIntervalSecs;
- public:
- CThrottler(const char *_title) : title(_title)
- {
- totalThrottleDelay = 0;
- limit = 0;
- delayMs = DEFAULT_STDCMD_THROTTLEDELAYMS;
- cpuThreshold = DEFAULT_STDCMD_THROTTLECPULIMIT;
- disabledLimit = 0;
- queueLimit = DEFAULT_STDCMD_THROTTLEQUEUELIMIT;
- statsIntervalSecs = DEFAULT_STDCMD_THROTTLECPULIMIT;
- }
- ~CThrottler()
- {
- for (;;)
- {
- Owned<CThrottleQueueItem> item = queue.dequeue();
- if (!item)
- break;
- }
- }
- unsigned queryLimit() const { return limit; }
- unsigned queryDelayMs() const { return delayMs; };;
- unsigned queryCpuThreshold() const { return cpuThreshold; }
- unsigned queryQueueLimit() const { return queueLimit; }
- StringBuffer &getInfoSummary(StringBuffer &info)
- {
- info.appendf("Throttler(%s) - limit=%u, delayMs=%u, cpuThreshold=%u, queueLimit=%u", title.get(), limit, delayMs, cpuThreshold, queueLimit).newline();
- unsigned elapsedSecs = totalThrottleDelayTimer.elapsedMs()/1000;
- time_t simple;
- time(&simple);
- simple -= elapsedSecs;
- CDateTime dt;
- dt.set(simple);
- StringBuffer dateStr;
- dt.getTimeString(dateStr, true);
- info.appendf("Throttler(%s): statistics since %s", title.get(), dateStr.str()).newline();
- info.appendf("Total delay of %0.2f seconds", ((double)totalThrottleDelay)/1000).newline();
- info.appendf("Requests currently queued: %u", queue.ordinality());
- return info;
- }
- void getInfo(StringBuffer &info)
- {
- CriticalBlock b(crit);
- getInfoSummary(info).newline();
- }
- void configure(unsigned _limit, unsigned _delayMs, unsigned _cpuThreshold, unsigned _queueLimit)
- {
- if (_limit > THROTTLE_MAX_LIMIT || _delayMs > THROTTLE_MAX_DELAYMS || _cpuThreshold > THROTTLE_MAX_CPUTHRESHOLD || _queueLimit > THROTTLE_MAX_QUEUELIMIT)
- throw MakeStringException(0, "Throttler(%s), rejecting configure command: limit=%u (max permitted=%u), delayMs=%u (max permitted=%u), cpuThreshold=%u (max permitted=%u), queueLimit=%u (max permitted=%u)",
- title.str(), _limit, THROTTLE_MAX_LIMIT, _delayMs, THROTTLE_MAX_DELAYMS, _cpuThreshold,
- THROTTLE_MAX_CPUTHRESHOLD, _queueLimit, THROTTLE_MAX_QUEUELIMIT);
- CriticalBlock b(configureCrit);
- int delta = 0;
- if (_limit)
- {
- if (disabledLimit) // if transitioning from disabled to some throttling
- {
- assertex(0 == limit);
- delta = _limit - disabledLimit; // + or -
- disabledLimit = 0;
- }
- else
- delta = _limit - limit; // + or -
- }
- else if (0 == disabledLimit)
- {
- PROGLOG("Throttler(%s): disabled, previous limit: %u", title.get(), limit);
- /* disabling - set limit immediately to let all new transaction through.
- * NB: the semaphore signals are not consumed in this case, because transactions could be waiting on it.
- * Instead the existing 'limit' is kept in 'disabledLimit', so that if/when throttling is
- * re-enabled, it is used as a basis for increasing or consuming the semaphore signal count.
- */
- disabledLimit = limit;
- limit = 0;
- }
- if (delta > 0)
- {
- PROGLOG("Throttler(%s): Increasing limit from %u to %u", title.get(), limit, _limit);
- sem.signal(delta);
- limit = _limit;
- // NB: If throttling was off, this doesn't effect transactions in progress, i.e. will only throttle new transactions coming in.
- }
- else if (delta < 0)
- {
- PROGLOG("Throttler(%s): Reducing limit from %u to %u", title.get(), limit, _limit);
- // NB: This is not expected to take long
- CCycleTimer timer;
- while (delta < 0)
- {
- if (sem.wait(1000))
- ++delta;
- else
- PROGLOG("Throttler(%s): Waited %0.2f seconds so far for up to a maximum of %u (previous limit) transactions to complete, %u completed", title.get(), ((double)timer.elapsedMs())/1000, limit, -delta);
- }
- limit = _limit;
- // NB: doesn't include transactions in progress, i.e. will only throttle new transactions coming in.
- }
- if (_delayMs != delayMs)
- {
- PROGLOG("Throttler(%s): New delayMs=%u, previous: %u", title.get(), _delayMs, delayMs);
- delayMs = _delayMs;
- }
- if (_cpuThreshold != cpuThreshold)
- {
- PROGLOG("Throttler(%s): New cpuThreshold=%u, previous: %u", title.get(), _cpuThreshold, cpuThreshold);
- cpuThreshold = _cpuThreshold;
- }
- if (((unsigned)-1) != _queueLimit && _queueLimit != queueLimit)
- {
- PROGLOG("Throttler(%s): New queueLimit=%u%s, previous: %u", title.get(), _queueLimit, 0==_queueLimit?"(disabled)":"", queueLimit);
- queueLimit = _queueLimit;
- }
- }
- void setStatsInterval(unsigned _statsIntervalSecs)
- {
- if (_statsIntervalSecs != statsIntervalSecs)
- {
- PROGLOG("Throttler(%s): New statsIntervalSecs=%u, previous: %u", title.get(), _statsIntervalSecs, statsIntervalSecs);
- statsIntervalSecs = _statsIntervalSecs;
- }
- }
- void take(RemoteFileCommandType cmd) // cmd for info. only
- {
- for (;;)
- {
- if (sem.wait(delayMs))
- return;
- PROGLOG("Throttler(%s): transaction delayed [cmd=%s]", title.get(), getRFCText(cmd));
- }
- }
- void release()
- {
- sem.signal();
- }
- StringBuffer &getStats(StringBuffer &stats, bool reset)
- {
- CriticalBlock b(crit);
- getInfoSummary(stats);
- if (reset)
- {
- totalThrottleDelayTimer.reset();
- totalThrottleDelay = 0;
- }
- return stats;
- }
- void addCommand(RemoteFileCommandType cmd, MemoryBuffer &msg, CRemoteClientHandler *client)
- {
- CCycleTimer timer;
- Owned<IException> exception;
- bool hadSem = true;
- if (!sem.wait(delayMs))
- {
- CriticalBlock b(crit);
- if (!sem.wait(0)) // check hasn't become available
- {
- unsigned cpu = getLatestCPUUsage();
- if (getLatestCPUUsage()<cpuThreshold)
- {
- /* Allow to proceed, despite hitting throttle limit because CPU < threshold
- * NB: The overall number of threads is still capped by the thread pool.
- */
- unsigned ms = timer.elapsedMs();
- totalThrottleDelay += ms;
- PROGLOG("Throttler(%s): transaction delayed [cmd=%s] for : %u milliseconds, proceeding as cpu(%u)<throttleCPULimit(%u)", title.get(), getRFCText(cmd), cpu, ms, cpuThreshold);
- hadSem = false;
- }
- else
- {
- if (queueLimit && queue.ordinality()>=queueLimit)
- throw MakeStringException(0, "Throttler(%s), the maxiumum number of items are queued (%u), rejecting new command[%s]", title.str(), queue.ordinality(), getRFCText(cmd));
- queue.enqueue(new CThrottleQueueItem(cmd, msg, client)); // NB: takes over ownership of 'client' from running thread
- PROGLOG("Throttler(%s): transaction delayed [cmd=%s], queuing (%u queueud), [client=%p, sock=%u]", title.get(), getRFCText(cmd), queue.ordinality(), client, client->socket->OShandle());
- return;
- }
- }
- }
- /* Guarantee that sem is released.
- * Should normally release on clean exit when queue is empty.
- */
- struct ReleaseSem
- {
- Semaphore *sem;
- ReleaseSem(Semaphore *_sem) { sem = _sem; }
- ~ReleaseSem() { if (sem) sem->signal(); }
- } releaseSem(hadSem?&sem:NULL);
- /* Whilst holding on this throttle slot (i.e. before signalling semaphore back), process
- * queued items. NB: other threads that are finishing will do also.
- * Queued items are processed 1st, then the current request, then anything that was queued when handling current request
- * Throttle slot (semaphore) is only given back when no more to do.
- */
- Linked<CRemoteClientHandler> currentClient;
- MemoryBuffer currentMsg;
- unsigned ms;
- for (;;)
- {
- RemoteFileCommandType currentCmd;
- {
- CriticalBlock b(crit);
- Owned<CThrottleQueueItem> item = queue.dequeue();
- if (item)
- {
- currentCmd = item->cmd;
- currentClient.setown(item->client.getClear());
- currentMsg.swapWith(item->msg);
- ms = item->timer.elapsedMs();
- }
- else
- {
- if (NULL == client) // previously handled and queue empty
- {
- /* Commands are only queued if semaphore is exhaused (checked inside crit)
- * so only signal the semaphore inside the crit, after checking if there are no queued items
- */
- if (hadSem)
- {
- releaseSem.sem = NULL;
- sem.signal();
- }
- break;
- }
- currentCmd = cmd;
- currentClient.set(client); // process current request after dealing with queue
- currentMsg.swapWith(msg);
- ms = timer.elapsedMs();
- client = NULL;
- }
- }
- if (ms >= 1000)
- {
- if (ms>delayMs)
- PROGLOG("Throttler(%s): transaction delayed [cmd=%s] for : %u seconds", title.get(), getRFCText(currentCmd), ms/1000);
- }
- {
- CriticalBlock b(crit);
- totalThrottleDelay += ms;
- }
- try
- {
- currentClient->processCommand(currentCmd, currentMsg, this);
- }
- catch (IException *e)
- {
- EXCLOG(e, "addCommand: processCommand failed");
- e->Release();
- }
- }
- }
- };
- // temporarily release a throttler slot
- class CThrottleReleaseBlock
- {
- CThrottler &throttler;
- RemoteFileCommandType cmd;
- public:
- CThrottleReleaseBlock(CThrottler &_throttler, RemoteFileCommandType _cmd) : throttler(_throttler), cmd(_cmd)
- {
- throttler.release();
- }
- ~CThrottleReleaseBlock()
- {
- throttler.take(cmd);
- }
- };
- int lasthandle;
- CriticalSection sect;
- Owned<ISocket> acceptsock;
- Owned<ISocket> securesock;
- Owned<ISocket> rowServiceSock;
- bool rowServiceOnStdPort = true; // should row service commands be processed on std. service port
- bool rowServiceSSL = false;
- Owned<ISocketSelectHandler> selecthandler;
- Owned<IThreadPool> threads; // for commands
- bool stopping;
- unsigned clientcounttick;
- unsigned closedclients;
- CAsyncCommandManager asyncCommandManager;
- CThrottler stdCmdThrottler, slowCmdThrottler;
- CClientStatsTable clientStatsTable;
- atomic_t globallasttick;
- unsigned targetActiveThreads;
- Linked<IPropertyTree> keyPairInfo;
- class CHandleTracer
- {
- CTimeMon timer;
- CriticalSection crit;
- Owned<IFile> stdIOIFile;
- std::vector<Owned<IFileIO>> reservedHandles;
- unsigned handlesToReserve = 3; // need a few for pipe process to succeed
- void reserveHandles()
- {
- if (stdIOIFile)
- {
- for (unsigned r=0; r<handlesToReserve; r++)
- {
- IFileIO *iFileIO = stdIOIFile->open(IFOread);
- if (iFileIO)
- reservedHandles.push_back(iFileIO);
- }
- }
- }
- void releaseHandles()
- {
- reservedHandles.clear();
- }
- public:
- CHandleTracer()
- {
- /* Reserve handles, so that when we run out, we hope to release them
- * and thereby have enough to use when reading current state.
- */
- stdIOIFile.setown(createIFile("stdout:"));
- timer.reset(0);
- reserveHandles();
- }
- void traceIfReady()
- {
- CriticalBlock b(crit);
- if (timer.timedout())
- {
- DBGLOG("Open handles:");
- releaseHandles();
- /* NB: can't guarantee that handles will be available after releaseHandles(), if other threads have allocated them.
- * If printLsOf fails, mark timer to retry again on next event in shorter time period.
- */
- if (!printLsOf())
- {
- DBGLOG("Failed to run lsof");
- timer.reset(1000); // next attempt in >=1 second
- }
- else
- timer.reset(60*1000); // next trace in >=1 minute
- reserveHandles();
- }
- }
- } handleTracer;
- int getNextHandle()
- {
- // called in sect critical block
- for (;;) {
- if (lasthandle==INT_MAX)
- lasthandle = 1;
- else
- lasthandle++;
- unsigned idx1;
- unsigned idx2;
- if (!findHandle(lasthandle,idx1,idx2))
- return lasthandle;
- }
- }
- bool findHandle(int handle,unsigned &clientidx,unsigned &handleidx)
- {
- // called in sect critical block
- clientidx = (unsigned)-1;
- handleidx = (unsigned)-1;
- ForEachItemIn(i,clients) {
- CRemoteClientHandler &client = clients.item(i);
- ForEachItemIn(j, client.openFiles)
- {
- if (client.openFiles.item(j).handle==handle)
- {
- handleidx = j;
- clientidx = i;
- return true;
- }
- }
- }
- return false;
- }
- unsigned readKeyData(IKeyManager *keyManager, unsigned maxRecs, MemoryBuffer &reply, bool &maxHit)
- {
- DelayedSizeMarker keyDataSzReturned(reply);
- unsigned numRecs = 0;
- maxHit = false;
- unsigned pos = reply.length();
- while (keyManager->lookup(true))
- {
- unsigned size = keyManager->queryRowSize();
- const byte *result = keyManager->queryKeyBuffer();
- reply.append(size);
- reply.append(size, result);
- ++numRecs;
- if (maxRecs && (0 == --maxRecs))
- {
- maxHit = true;
- break;
- }
- if (reply.length()-pos >= MAX_KEYDATA_SZ)
- {
- maxHit = true;
- break;
- }
- }
- keyDataSzReturned.write();
- return numRecs;
- }
- class cCommandProcessor: public CInterface, implements IPooledThread
- {
- Owned<CRemoteClientHandler> client;
- MemoryBuffer msg;
- public:
- IMPLEMENT_IINTERFACE;
- struct cCommandProcessorParams
- {
- cCommandProcessorParams() { msg.setEndian(__BIG_ENDIAN); }
- CRemoteClientHandler *client;
- MemoryBuffer msg;
- };
- virtual void init(void *_params) override
- {
- cCommandProcessorParams ¶ms = *(cCommandProcessorParams *)_params;
- client.setown(params.client);
- msg.swapWith(params.msg);
- }
- virtual void threadmain() override
- {
- // idea is that initially we process commands inline then pass over to select handler
- try
- {
- client->process(msg);
- }
- catch (IException *e)
- {
- // suppress some errors
- EXCLOG(e,"cCommandProcessor::threadmain");
- e->Release();
- }
- try
- {
- client.clear();
- }
- catch (IException *e)
- {
- // suppress some more errors clearing client
- EXCLOG(e,"cCommandProcessor::threadmain(2)");
- e->Release();
- }
- }
- virtual bool stop() override
- {
- return true;
- }
- virtual bool canReuse() const override
- {
- return false; // want to free owned socket
- }
- };
- IArrayOf<CRemoteClientHandler> clients;
- void validateSSLSetup()
- {
- if (!securitySettings.certificate)
- throw createDafsException(DAFSERR_serverinit_failed, "SSL Certificate information not found in environment.conf");
- if (!checkFileExists(securitySettings.certificate))
- throw createDafsException(DAFSERR_serverinit_failed, "SSL Certificate File not found in environment.conf");
- if (!securitySettings.privateKey)
- throw createDafsException(DAFSERR_serverinit_failed, "SSL Key information not found in environment.conf");
- if (!checkFileExists(securitySettings.privateKey))
- throw createDafsException(DAFSERR_serverinit_failed, "SSL Key File not found in environment.conf");
- }
- public:
- IMPLEMENT_IINTERFACE
- CRemoteFileServer(unsigned maxThreads, unsigned maxThreadsDelayMs, unsigned maxAsyncCopy, IPropertyTree *_keyPairInfo)
- : asyncCommandManager(maxAsyncCopy), stdCmdThrottler("stdCmdThrotlter"), slowCmdThrottler("slowCmdThrotlter"), keyPairInfo(_keyPairInfo)
- {
- lasthandle = 0;
- selecthandler.setown(createSocketSelectHandler(NULL));
- stdCmdThrottler.configure(DEFAULT_STDCMD_PARALLELREQUESTLIMIT, DEFAULT_STDCMD_THROTTLEDELAYMS, DEFAULT_STDCMD_THROTTLECPULIMIT, DEFAULT_STDCMD_THROTTLEQUEUELIMIT);
- slowCmdThrottler.configure(DEFAULT_SLOWCMD_PARALLELREQUESTLIMIT, DEFAULT_SLOWCMD_THROTTLEDELAYMS, DEFAULT_SLOWCMD_THROTTLECPULIMIT, DEFAULT_SLOWCMD_THROTTLEQUEUELIMIT);
- unsigned targetMinThreads=maxThreads*20/100; // 20%
- if (0 == targetMinThreads) targetMinThreads = 1;
- targetActiveThreads=maxThreads*80/100; // 80%
- if (0 == targetActiveThreads) targetActiveThreads = 1;
- class CCommandFactory : public CSimpleInterfaceOf<IThreadFactory>
- {
- CRemoteFileServer &parent;
- public:
- CCommandFactory(CRemoteFileServer &_parent) : parent(_parent) { }
- virtual IPooledThread *createNew()
- {
- return parent.createCommandProcessor();
- }
- };
- Owned<IThreadFactory> factory = new CCommandFactory(*this); // NB: pool links factory, so takes ownership
- threads.setown(createThreadPool("CRemoteFileServerPool", factory, NULL, maxThreads, maxThreadsDelayMs,
- #ifdef __64BIT__
- 0, // Unlimited stack size
- #else
- 0x10000,
- #endif
- INFINITE,targetMinThreads));
- threads->setStartDelayTracing(60); // trace amount delayed every minute.
- PROGLOG("CRemoteFileServer: maxThreads = %u, maxThreadsDelayMs = %u, maxAsyncCopy = %u", maxThreads, maxThreadsDelayMs, maxAsyncCopy);
- stopping = false;
- clientcounttick = msTick();
- closedclients = 0;
- atomic_set(&globallasttick,msTick());
- }
- ~CRemoteFileServer()
- {
- #ifdef _DEBUG
- PROGLOG("Exiting CRemoteFileServer");
- #endif
- asyncCommandManager.join();
- clients.kill();
- #ifdef _DEBUG
- PROGLOG("Exited CRemoteFileServer");
- #endif
- }
- bool lookupFileIOHandle(int handle, OpenFileInfo &fileInfo, unsigned newFlags=0)
- {
- if (handle<=0)
- return false;
- CriticalBlock block(sect);
- unsigned clientidx;
- unsigned handleidx;
- if (!findHandle(handle,clientidx,handleidx))
- return false;
- CRemoteClientHandler &client = clients.item(clientidx);
- OpenFileInfo &openFileInfo = client.openFiles.element(handleidx); // NB: links members
- openFileInfo.flags |= newFlags;
- fileInfo = openFileInfo;
- client.previdx = handleidx;
- return true;
- }
- //MORE: The file handles should timeout after a while, and accessing an old (invalid handle)
- // should throw a different exception
- bool checkFileIOHandle(int handle, IFileIO *&fileio, bool del=false)
- {
- fileio = NULL;
- if (handle<=0)
- return false;
- CriticalBlock block(sect);
- unsigned clientidx;
- unsigned handleidx;
- if (findHandle(handle,clientidx,handleidx))
- {
- CRemoteClientHandler &client = clients.item(clientidx);
- const OpenFileInfo &fileInfo = client.openFiles.item(handleidx);
- if (del)
- {
- if (fileInfo.flags & of_key)
- clearKeyStoreCacheEntry(fileInfo.fileIO);
- client.openFiles.remove(handleidx);
- client.previdx = (unsigned)-1;
- }
- else
- {
- fileio = client.openFiles.item(handleidx).fileIO;
- client.previdx = handleidx;
- }
- return true;
- }
- return false;
- }
- void checkFileIOHandle(MemoryBuffer &reply, int handle, IFileIO *&fileio, bool del=false)
- {
- if (!checkFileIOHandle(handle, fileio, del))
- throw createDafsException(RFSERR_InvalidFileIOHandle, nullptr);
- }
- void onCloseSocket(CRemoteClientHandler *client, int which)
- {
- if (!client)
- return;
- CriticalBlock block(sect);
- #ifdef _DEBUG
- StringBuffer s(client->queryPeerName());
- PROGLOG("onCloseSocket(%d) %s",which,s.str());
- #endif
- if (client->socket)
- {
- try
- {
- /* JCSMORE - shouldn't this really be dependent on whether selecthandled=true
- * It has not been added to the selecthandler
- * Harmless, but wasteful if so.
- */
- selecthandler->remove(client->socket);
- }
- catch (IException *e) {
- EXCLOG(e,"CRemoteFileServer::onCloseSocket.1");
- e->Release();
- }
- }
- try {
- clients.zap(*client);
- }
- catch (IException *e) {
- EXCLOG(e,"CRemoteFileServer::onCloseSocket.2");
- e->Release();
- }
- }
- bool cmdOpenFileIO(MemoryBuffer & msg, MemoryBuffer & reply, CRemoteClientHandler &client)
- {
- Owned<StringAttrItem> name = new StringAttrItem;
- byte mode;
- byte share;
- msg.read(name->text).read(mode).read(share);
- // also try to recv extra byte
- byte extra = 0;
- unsigned short sMode = IFUnone;
- unsigned short cFlags = IFUnone;
- if (msg.remaining() >= sizeof(byte))
- {
- msg.read(extra);
- // and then try to recv extra sMode, cFlags (always sent together)
- if (msg.remaining() >= (sizeof(sMode) + sizeof(cFlags)))
- msg.read(sMode).read(cFlags);
- }
- IFEflags extraFlags = (IFEflags)extra;
- // none => nocache for remote (hint)
- // can revert to previous behavior with conf file setting "allow_pgcache_flush=false"
- if (extraFlags == IFEnone)
- extraFlags = IFEnocache;
- Owned<IFile> file = createIFile(name->text);
- switch ((compatIFSHmode)share) {
- case compatIFSHnone:
- file->setCreateFlags(S_IRUSR|S_IWUSR);
- file->setShareMode(IFSHnone);
- break;
- case compatIFSHread:
- file->setShareMode(IFSHread);
- break;
- case compatIFSHwrite:
- file->setShareMode(IFSHfull);
- break;
- case compatIFSHexec:
- file->setCreateFlags(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
- break;
- case compatIFSHall:
- file->setCreateFlags(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); // bit excessive
- file->setShareMode(IFSHfull);
- break;
- }
- // use sMode, cFlags if sent
- if (sMode != IFUnone && cFlags != IFUnone)
- {
- file->setCreateFlags(cFlags);
- file->setShareMode((IFSHmode)sMode);
- }
- if (TF_TRACE_PRE_IO)
- PROGLOG("before open file '%s', (%d,%d,%d,%d,0%o)",name->text.get(),(int)mode,(int)share,extraFlags,sMode,cFlags);
- Owned<IFileIO> fileio = file->open((IFOmode)mode,extraFlags);
- int handle;
- if (fileio)
- {
- CriticalBlock block(sect);
- handle = getNextHandle();
- client.previdx = client.openFiles.ordinality();
- client.openFiles.append(OpenFileInfo(handle, fileio, name));
- }
- else
- handle = 0;
- reply.append(RFEnoerror);
- reply.append(handle);
- if (TF_TRACE)
- PROGLOG("open file '%s', (%d,%d) handle = %d",name->text.get(),(int)mode,(int)share,handle);
- return true;
- }
- bool cmdCloseFileIO(MemoryBuffer & msg, MemoryBuffer & reply)
- {
- int handle;
- msg.read(handle);
- IFileIO *fileio;
- checkFileIOHandle(reply, handle, fileio, true);
- if (TF_TRACE)
- PROGLOG("close file, handle = %d",handle);
- reply.append(RFEnoerror);
- return true;
- }
- void cmdRead(MemoryBuffer & msg, MemoryBuffer & reply, CClientStats &stats)
- {
- int handle;
- __int64 pos;
- size32_t len;
- msg.read(handle).read(pos).read(len);
- IFileIO *fileio;
- checkFileIOHandle(reply, handle, fileio);
- //arrange it so we read directly into the reply buffer...
- unsigned posOfErr = reply.length();
- reply.append((unsigned)RFEnoerror);
- size32_t numRead;
- unsigned posOfLength = reply.length();
- if (TF_TRACE_PRE_IO)
- PROGLOG("before read file, handle = %d, toread = %d",handle,len);
- reply.reserve(sizeof(numRead));
- void *data = reply.reserve(len);
- numRead = fileio->read(pos,len,data);
- stats.addRead(len);
- if (TF_TRACE)
- PROGLOG("read file, handle = %d, pos = %" I64F "d, toread = %d, read = %d",handle,pos,len,numRead);
- reply.setLength(posOfLength + sizeof(numRead) + numRead);
- reply.writeEndianDirect(posOfLength,sizeof(numRead),&numRead);
- }
- void cmdSize(MemoryBuffer & msg, MemoryBuffer & reply)
- {
- int handle;
- msg.read(handle);
- IFileIO *fileio;
- checkFileIOHandle(reply, handle, fileio);
- __int64 size = fileio->size();
- reply.append((unsigned)RFEnoerror).append(size);
- if (TF_TRACE)
- PROGLOG("size file, handle = %d, size = %" I64F "d",handle,size);
- }
- void cmdSetSize(MemoryBuffer & msg, MemoryBuffer & reply)
- {
- int handle;
- offset_t size;
- msg.read(handle).read(size);
- IFileIO *fileio;
- if (TF_TRACE)
- PROGLOG("set size file, handle = %d, size = %" I64F "d",handle,size);
- checkFileIOHandle(reply, handle, fileio);
- fileio->setSize(size);
- reply.append((unsigned)RFEnoerror);
- }
- void cmdWrite(MemoryBuffer & msg, MemoryBuffer & reply, CClientStats &stats)
- {
- int handle;
- __int64 pos;
- size32_t len;
- msg.read(handle).read(pos).read(len);
- IFileIO *fileio;
- checkFileIOHandle(reply, handle, fileio);
- const byte *data = (const byte *)msg.readDirect(len);
- if (TF_TRACE_PRE_IO)
- PROGLOG("before write file, handle = %d, towrite = %d",handle,len);
- size32_t numWritten = fileio->write(pos,len,data);
- stats.addWrite(numWritten);
- if (TF_TRACE)
- PROGLOG("write file, handle = %d, towrite = %d, written = %d",handle,len,numWritten);
- reply.append((unsigned)RFEnoerror).append(numWritten);
- }
- void cmdExists(MemoryBuffer & msg, MemoryBuffer & reply, CRemoteClientHandler &client)
- {
- StringAttr name;
- msg.read(name);
- if (TF_TRACE)
- PROGLOG("exists, '%s'",name.get());
- Owned<IFile> file=createIFile(name);
- bool e = file->exists();
- reply.append((unsigned)RFEnoerror).append(e);
- }
- void cmdRemove(MemoryBuffer & msg, MemoryBuffer & reply,CRemoteClientHandler &client)
- {
- StringAttr name;
- msg.read(name);
- if (TF_TRACE)
- PROGLOG("remove, '%s'",name.get());
- Owned<IFile> file=createIFile(name);
- bool e = file->remove();
- reply.append((unsigned)RFEnoerror).append(e);
- }
- void cmdGetVer(MemoryBuffer & msg, MemoryBuffer & reply)
- {
- if (TF_TRACE)
- PROGLOG("getVer");
- /* weird backward compatibility convention,
- * newer clients will send another unsigned to denote
- * and result in the numeric DAFILESRV_VERSION being returned
- * Ancient clients will get back the string form only (SERVER_VERSTRING)
- */
- if (msg.getPos()+sizeof(unsigned)>msg.length())
- reply.append((unsigned)RFEnoerror);
- else
- reply.append((unsigned)DAFILESRV_VERSION+0x10000);
- reply.append(DAFILESRV_VERSIONSTRING);
- }
- void cmdRename(MemoryBuffer & msg, MemoryBuffer & reply,CRemoteClientHandler &client)
- {
- StringAttr fromname;
- msg.read(fromname);
- StringAttr toname;
- msg.read(toname);
- if (TF_TRACE)
- PROGLOG("rename, '%s' to '%s'",fromname.get(),toname.get());
- Owned<IFile> file=createIFile(fromname);
- file->rename(toname);
- reply.append((unsigned)RFEnoerror);
- }
- void cmdMove(MemoryBuffer & msg, MemoryBuffer & reply,CRemoteClientHandler &client)
- {
- StringAttr fromname;
- msg.read(fromname);
- StringAttr toname;
- msg.read(toname);
- if (TF_TRACE)
- PROGLOG("move, '%s' to '%s'",fromname.get(),toname.get());
- Owned<IFile> file=createIFile(fromname);
- file->move(toname);
- reply.append((unsigned)RFEnoerror);
- }
- void cmdCopy(MemoryBuffer & msg, MemoryBuffer & reply, CRemoteClientHandler &client)
- {
- StringAttr fromname;
- msg.read(fromname);
- StringAttr toname;
- msg.read(toname);
- if (TF_TRACE)
- PROGLOG("copy, '%s' to '%s'",fromname.get(),toname.get());
- copyFile(toname, fromname);
- reply.append((unsigned)RFEnoerror);
- }
- void cmdAppend(MemoryBuffer & msg, MemoryBuffer & reply, CRemoteClientHandler &client, CClientStats &stats)
- {
- int handle;
- __int64 pos;
- __int64 len;
- StringAttr srcname;
- msg.read(handle).read(srcname).read(pos).read(len);
- IFileIO *fileio;
- checkFileIOHandle(reply, handle, fileio);
- Owned<IFile> file = createIFile(srcname.get());
- __int64 written = fileio->appendFile(file,pos,len);
- stats.addWrite(written);
- if (TF_TRACE)
- PROGLOG("append file, handle = %d, file=%s, pos = %" I64F "d len = %" I64F "d written = %" I64F "d",handle,srcname.get(),pos,len,written);
- reply.append((unsigned)RFEnoerror).append(written);
- }
- void cmdIsFile(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr name;
- msg.read(name);
- if (TF_TRACE)
- PROGLOG("isFile, '%s'",name.get());
- Owned<IFile> file=createIFile(name);
- unsigned ret = (unsigned)file->isFile();
- reply.append((unsigned)RFEnoerror).append(ret);
- }
- void cmdIsDir(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr name;
- msg.read(name);
- if (TF_TRACE)
- PROGLOG("isDir, '%s'",name.get());
- Owned<IFile> file=createIFile(name);
- unsigned ret = (unsigned)file->isDirectory();
- reply.append((unsigned)RFEnoerror).append(ret);
- }
- void cmdIsReadOnly(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr name;
- msg.read(name);
- if (TF_TRACE)
- PROGLOG("isReadOnly, '%s'",name.get());
- Owned<IFile> file=createIFile(name);
- unsigned ret = (unsigned)file->isReadOnly();
- reply.append((unsigned)RFEnoerror).append(ret);
- }
- void cmdSetReadOnly(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr name;
- bool set;
- msg.read(name).read(set);
- if (TF_TRACE)
- PROGLOG("setReadOnly, '%s' %d",name.get(),(int)set);
- Owned<IFile> file=createIFile(name);
- file->setReadOnly(set);
- reply.append((unsigned)RFEnoerror);
- }
- void cmdSetFilePerms(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr name;
- unsigned fPerms;
- msg.read(name).read(fPerms);
- if (TF_TRACE)
- PROGLOG("setFilePerms, '%s' 0%o",name.get(),fPerms);
- Owned<IFile> file=createIFile(name);
- file->setFilePermissions(fPerms);
- reply.append((unsigned)RFEnoerror);
- }
- void cmdGetTime(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr name;
- msg.read(name);
- if (TF_TRACE)
- PROGLOG("getTime, '%s'",name.get());
- Owned<IFile> file=createIFile(name);
- CDateTime createTime;
- CDateTime modifiedTime;
- CDateTime accessedTime;
- bool ret = file->getTime(&createTime,&modifiedTime,&accessedTime);
- reply.append((unsigned)RFEnoerror).append(ret);
- if (ret)
- {
- createTime.serialize(reply);
- modifiedTime.serialize(reply);
- accessedTime.serialize(reply);
- }
- }
- void cmdSetTime(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr name;
- bool creategot;
- CDateTime createTime;
- bool modifiedgot;
- CDateTime modifiedTime;
- bool accessedgot;
- CDateTime accessedTime;
- msg.read(name);
- msg.read(creategot);
- if (creategot)
- createTime.deserialize(msg);
- msg.read(modifiedgot);
- if (modifiedgot)
- modifiedTime.deserialize(msg);
- msg.read(accessedgot);
- if (accessedgot)
- accessedTime.deserialize(msg);
- if (TF_TRACE)
- PROGLOG("setTime, '%s'",name.get());
- Owned<IFile> file=createIFile(name);
- bool ret = file->setTime(creategot?&createTime:NULL,modifiedgot?&modifiedTime:NULL,accessedgot?&accessedTime:NULL);
- reply.append((unsigned)RFEnoerror).append(ret);
- }
- void cmdCreateDir(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr name;
- msg.read(name);
- if (TF_TRACE)
- PROGLOG("CreateDir, '%s'",name.get());
- Owned<IFile> dir=createIFile(name);
- bool ret = dir->createDirectory();
- reply.append((unsigned)RFEnoerror).append(ret);
- }
- void cmdGetDir(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr name;
- StringAttr mask;
- bool includedir;
- bool sub;
- byte stream = 0;
- msg.read(name).read(mask).read(includedir).read(sub);
- if (msg.remaining()>=sizeof(byte))
- {
- msg.read(stream);
- if (stream==1)
- client.opendir.clear();
- }
- if (TF_TRACE)
- PROGLOG("GetDir, '%s', '%s', stream='%u'",name.get(),mask.get(),stream);
- if (!stream && !containsFileWildcard(mask))
- {
- // if no streaming, and mask contains no wildcard, it is much more efficient to get the info without a directory iterator!
- StringBuffer fullFilename(name);
- addPathSepChar(fullFilename).append(mask);
- Owned<IFile> iFile = createIFile(fullFilename);
- // NB: This must preserve same serialization format as CRemoteDirectoryIterator::serialize produces for <=1 file.
- reply.append((unsigned)RFEnoerror);
- if (!iFile->exists())
- reply.append((byte)0);
- else
- {
- byte b=1;
- reply.append(b);
- bool isDir = fileBool::foundYes == iFile->isDirectory();
- reply.append(isDir);
- reply.append(isDir ? 0 : iFile->size());
- CDateTime dt;
- iFile->getTime(nullptr, &dt, nullptr);
- dt.serialize(reply);
- reply.append(mask);
- b = 0;
- reply.append(b);
- }
- }
- else
- {
- Owned<IFile> dir=createIFile(name);
- Owned<IDirectoryIterator> iter;
- if (stream>1)
- iter.set(client.opendir);
- else
- {
- iter.setown(dir->directoryFiles(mask.length()?mask.get():NULL,sub,includedir));
- if (stream != 0)
- client.opendir.set(iter);
- }
- if (!iter)
- throw createDafsException(RFSERR_GetDirFailed, nullptr);
- reply.append((unsigned)RFEnoerror);
- if (serializeRemoteDirectoryIterator(reply,iter,stream?0x100000:0,stream<2))
- {
- if (stream != 0)
- client.opendir.clear();
- }
- else
- {
- bool cont=true;
- reply.append(cont);
- }
- }
- }
- void cmdMonitorDir(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr name;
- StringAttr mask;
- bool includedir;
- bool sub;
- unsigned checkinterval;
- unsigned timeout;
- __int64 cancelid; // not yet used
- msg.read(name).read(mask).read(includedir).read(sub).read(checkinterval).read(timeout).read(cancelid);
- byte isprev;
- msg.read(isprev);
- Owned<IDirectoryIterator> prev;
- if (isprev==1)
- {
- SocketEndpoint ep;
- prev.setown(createRemoteDirectorIterator(ep, name, msg));
- }
- if (TF_TRACE)
- PROGLOG("MonitorDir, '%s' '%s'",name.get(),mask.get());
- Owned<IFile> dir=createIFile(name);
- Owned<IDirectoryDifferenceIterator> iter=dir->monitorDirectory(prev,mask.length()?mask.get():NULL,sub,includedir,checkinterval,timeout);
- reply.append((unsigned)RFEnoerror);
- byte state = (iter.get()==NULL)?0:1;
- reply.append(state);
- if (state==1)
- serializeRemoteDirectoryDiff(reply, iter);
- }
- void cmdCopySection(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr uuid;
- StringAttr fromFile;
- StringAttr toFile;
- offset_t toOfs;
- offset_t fromOfs;
- offset_t size;
- offset_t sizeDone=0;
- offset_t totalSize=(offset_t)-1;
- unsigned timeout;
- msg.read(uuid).read(fromFile).read(toFile).read(toOfs).read(fromOfs).read(size).read(timeout);
- AsyncCommandStatus status = asyncCommandManager.copySection(uuid,fromFile,toFile,toOfs,fromOfs,size,sizeDone,totalSize,timeout);
- reply.append((unsigned)RFEnoerror).append((unsigned)status).append(sizeDone).append(totalSize);
- }
- static void treeCopyFile(RemoteFilename &srcfn, RemoteFilename &dstfn, const char *net, const char *mask, IpAddress &ip, bool usetmp, CThrottler *throttler, CFflags copyFlags=CFnone)
- {
- unsigned start = msTick();
- Owned<IFile> dstfile = createIFile(dstfn);
- // the following is really to check the dest node is up and working (otherwise not much point in continuing!)
- if (dstfile->exists())
- PROGLOG("TREECOPY overwriting '%s'",dstfile->queryFilename());
- Owned<IFile> srcfile = createIFile(srcfn);
- unsigned lastmin = 0;
- if (!srcfn.queryIP().ipequals(dstfn.queryIP())) {
- CriticalBlock block(treeCopyCrit);
- for (;;) {
- CDateTime dt;
- offset_t sz;
- try {
- sz = srcfile->size();
- if (sz==(offset_t)-1) {
- if (TF_TRACE_TREE_COPY)
- PROGLOG("TREECOPY source not found '%s'",srcfile->queryFilename());
- break;
- }
- srcfile->getTime(NULL,&dt,NULL);
- }
- catch (IException *e) {
- EXCLOG(e,"treeCopyFile(1)");
- e->Release();
- break;
- }
- Linked<CTreeCopyItem> tc;
- unsigned now = msTick();
- ForEachItemInRev(i1,treeCopyArray) {
- CTreeCopyItem &item = treeCopyArray.item(i1);
- // prune old entries (not strictly needed buf I think better)
- if (now-item.lastused>TREECOPYPRUNETIME)
- treeCopyArray.remove(i1);
- else if (!tc.get()&&item.equals(srcfn,net,mask,sz,dt)) {
- tc.set(&item);
- item.lastused = now;
- }
- }
- if (!tc.get()) {
- if (treeCopyArray.ordinality()>=TREECOPY_CACHE_SIZE)
- treeCopyArray.remove(0);
- tc.setown(new CTreeCopyItem(srcfn,net,mask,sz,dt));
- treeCopyArray.append(*tc.getLink());
- }
- ForEachItemInRev(cand,tc->loc) { // rev to choose copied locations first (maybe optional?)
- if (!tc->busy->testSet(cand)) {
- // check file accessible and matches
- if (!cand&&dstfn.equals(tc->loc.item(cand))) // hmm trying to overwrite existing, better humor
- continue;
- bool ok = true;
- Owned<IFile> rmtfile = createIFile(tc->loc.item(cand));
- if (cand) { // only need to check if remote
- try {
- if (rmtfile->size()!=sz)
- ok = false;
- else {
- CDateTime fdt;
- rmtfile->getTime(NULL,&fdt,NULL);
- ok = fdt.equals(dt);
- }
- }
- catch (IException *e) {
- EXCLOG(e,"treeCopyFile(2)");
- e->Release();
- ok = false;
- }
- }
- if (ok) { // if not ok leave 'busy'
- // finally lets try and copy!
- try {
- if (TF_TRACE_TREE_COPY)
- PROGLOG("TREECOPY(started) %s to %s",rmtfile->queryFilename(),dstfile->queryFilename());
- {
- CriticalUnblock unblock(treeCopyCrit); // note we have tc linked
- rmtfile->copyTo(dstfile,DEFAULT_COPY_BLKSIZE,NULL,usetmp,copyFlags);
- }
- if (TF_TRACE_TREE_COPY)
- PROGLOG("TREECOPY(done) %s to %s",rmtfile->queryFilename(),dstfile->queryFilename());
- tc->busy->set(cand,false);
- if (treeCopyWaiting)
- treeCopySem.signal((treeCopyWaiting>1)?2:1);
- // add to known locations
- tc->busy->set(tc->loc.ordinality(),false); // prob already is clear
- tc->loc.append(dstfn);
- ip.ipset(tc->loc.item(cand).queryIP());
- return;
- }
- catch (IException *e) {
- if (cand==0) {
- tc->busy->set(0,false); // don't leave busy
- if (treeCopyWaiting)
- treeCopySem.signal();
- throw; // what more can we do!
- }
- EXCLOG(e,"treeCopyFile(3)");
- e->Release();
- }
- }
- }
- }
- // all locations busy
- if (msTick()-start>TREECOPYTIMEOUT) {
- WARNLOG("Treecopy %s wait timed out", srcfile->queryFilename());
- break;
- }
- treeCopyWaiting++; // note this isn't precise - just indication
- {
- CriticalUnblock unblock(treeCopyCrit);
- if (throttler)
- {
- CThrottleReleaseBlock block(*throttler, RFCtreecopy);
- treeCopySem.wait(TREECOPYPOLLTIME);
- }
- else
- treeCopySem.wait(TREECOPYPOLLTIME);
- }
- treeCopyWaiting--;
- if ((msTick()-start)/10*1000!=lastmin) {
- lastmin = (msTick()-start)/10*1000;
- PROGLOG("treeCopyFile delayed: %s to %s",srcfile->queryFilename(),dstfile->queryFilename());
- }
- }
- }
- else if (TF_TRACE_TREE_COPY)
- PROGLOG("TREECOPY source on same node as destination");
- if (TF_TRACE_TREE_COPY)
- PROGLOG("TREECOPY(started,fallback) %s to %s",srcfile->queryFilename(),dstfile->queryFilename());
- try {
- GetHostIp(ip);
- srcfile->copyTo(dstfile,DEFAULT_COPY_BLKSIZE,NULL,usetmp,copyFlags);
- }
- catch (IException *e) {
- EXCLOG(e,"TREECOPY(done,fallback)");
- throw;
- }
- if (TF_TRACE_TREE_COPY)
- PROGLOG("TREECOPY(done,fallback) %s to %s",srcfile->queryFilename(),dstfile->queryFilename());
- }
- void cmdTreeCopy(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client, CThrottler *throttler, bool usetmp=false)
- {
- RemoteFilename src;
- src.deserialize(msg);
- RemoteFilename dst;
- dst.deserialize(msg);
- StringAttr net;
- StringAttr mask;
- msg.read(net).read(mask);
- IpAddress ip;
- treeCopyFile(src,dst,net,mask,ip,usetmp,throttler);
- unsigned status = 0;
- reply.append((unsigned)RFEnoerror).append((unsigned)status);
- ip.ipserialize(reply);
- }
- void cmdTreeCopyTmp(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client, CThrottler *throttler)
- {
- cmdTreeCopy(msg, reply, client, throttler, true);
- }
- void cmdGetCRC(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr name;
- msg.read(name);
- if (TF_TRACE)
- PROGLOG("getCRC, '%s'",name.get());
- Owned<IFile> file=createIFile(name);
- unsigned ret = file->getCRC();
- reply.append((unsigned)RFEnoerror).append(ret);
- }
- void cmdStop(MemoryBuffer &msg, MemoryBuffer &reply)
- {
- PROGLOG("Abort request received");
- stopping = true;
- if (acceptsock)
- acceptsock->cancel_accept();
- if (securesock)
- securesock->cancel_accept();
- if (rowServiceSock)
- rowServiceSock->cancel_accept();
- reply.append((unsigned)RFEnoerror);
- }
- void cmdExec(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr cmdLine;
- msg.read(cmdLine);
- // NB: legacy remoteExec used to simply pass error code and buffer back to caller.
- VStringBuffer errMsg("Remote command execution no longer supported. Trying to execute cmdline=%s", cmdLine.get());
- WARNLOG("%s", errMsg.str());
- size32_t outSz = errMsg.length()+1; // reply with null terminated string
- // reply with error code -1
- reply.append((unsigned)-1).append((unsigned)0).append(outSz).append(outSz, errMsg.str());
- }
- void cmdSetTrace(MemoryBuffer &msg, MemoryBuffer &reply)
- {
- byte flags;
- msg.read(flags);
- int retcode=-1;
- if (flags!=255) // escape
- {
- retcode = traceFlags;
- traceFlags = flags;
- }
- reply.append(retcode);
- }
- void cmdGetInfo(MemoryBuffer &msg, MemoryBuffer &reply)
- {
- unsigned level=1;
- if (msg.remaining() >= sizeof(unsigned))
- msg.read(level);
- StringBuffer retstr;
- getInfo(retstr, level);
- reply.append(RFEnoerror).append(retstr.str());
- }
- void cmdFirewall(MemoryBuffer &msg, MemoryBuffer &reply)
- {
- // TBD
- StringBuffer retstr;
- getInfo(retstr);
- reply.append(RFEnoerror).append(retstr.str());
- }
- void cmdExtractBlobElements(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client)
- {
- StringAttr prefix;
- StringAttr filename;
- msg.read(prefix).read(filename);
- RemoteFilename rfn;
- rfn.setLocalPath(filename);
- ExtractedBlobArray extracted;
- extractBlobElements(prefix, rfn, extracted);
- unsigned n = extracted.ordinality();
- reply.append((unsigned)RFEnoerror).append(n);
- for (unsigned i=0;i<n;i++)
- extracted.item(i).serialize(reply);
- }
- void cmdStreamGeneral(MemoryBuffer &msg, MemoryBuffer &reply, CRemoteClientHandler &client, CClientStats &stats)
- {
- size32_t jsonSz;
- msg.read(jsonSz);
- Owned<IPropertyTree> requestTree = createPTreeFromJSONString(jsonSz, (const char *)msg.readDirect(jsonSz));
- cmdStreamCommon(requestTree, msg, reply, client, stats);
- }
- /* Notes on protocol:
- *
- * A JSON request with these top-level fields:
- * "format" - the format of the reply. Supported formats = "binary", "xml", "json"
- * "handle" - the handle of for a file session that was previously open (for continuation)
- * "commCompression" - compression format of the communication protocol. Supports "LZ4", "LZW", "FLZ" (TBD: "ZLIB")
- * "replyLimit" - Number of K to limit each reply size to. (default 1024)
- * "node" - contains all 'activity' properties below:
- *
- * For a secured dafilesrv (streaming protocol), requests will only be accepted if the meta blob ("metaInfo") has a matching signature.
- * The request must specify "filePart" (1 based) to denote the partition # being read from or written to.
- *
- * "filePartCopy" (1 based) defaults to 1
- *
- * "kind" - supported kinds = "diskread", "diskwrite", "indexread", "indexcount" (TBD: "diskcount", "indexwrite", "disklookup")
- * NB: disk vs index will be auto detected if "kind" is absent.
- *
- * "action" - supported actions = "count" (used if "kind" is auto-detected to specify count should be performed instead of read)
- *
- * "keyFilter" - filter the results by this expression (See: HPCC-18474 for more details).
- *
- * "chooseN" - maximum # of results to return
- *
- * "compressed" - specifies whether input file is compressed. NB: not relevant to "index" types. Default = false. Auto-detected.
- *
- * "input" - specifies layout on disk of the file being read.
- *
- * "output" - where relavant, specifies the output format to be returned
- *
- * "fileName" is only used for unsecured non signed connections (normally forbidden), and specifies the fully qualified path to a physical file.
- *
- */
- void cmdStreamCommon(IPropertyTree *requestTree, MemoryBuffer &rest, MemoryBuffer &reply, CRemoteClientHandler &client, CClientStats &stats)
- {
- /* Example JSON request:
- *
- * {
- * "format" : "binary",
- * "handle" : "1234",
- * "replyLimit" : "64",
- * "commCompression" : "LZ4",
- * "node" : {
- * "metaInfo" : "",
- * "filePart" : 2,
- * "filePartCopy" : 1,
- * "kind" : "diskread",
- * "fileName": "examplefilename",
- * "keyFilter" : "f1='1 '",
- * "chooseN" : 5,
- * "compressed" : "false"
- * "input" : {
- * "f1" : "string5",
- * "f2" : "string5"
- * },
- * "output" : {
- * "f2" : "string",
- * "f1" : "real"
- * }
- * }
- * }
- * OR
- * {
- * "format" : "binary",
- * "handle" : "1234",
- * "replyLimit" : "64",
- * "commCompression" : "LZ4",
- * "node" : {
- * "kind" : "diskread",
- * "fileName": "examplefilename",
- * "keyFilter" : "f1='1 '",
- * "chooseN" : 5,
- * "compressed" : "false"
- * "input" : {
- * "f1" : "string5",
- * "f2" : "string5"
- * },
- * "output" : {
- * "f2" : "string",
- * "f1" : "real"
- * }
- * }
- * }
- * OR
- * {
- * "format" : "xml",
- * "handle" : "1234",
- * "replyLimit" : "64",
- * "node" : {
- * "kind" : "diskread",
- * "fileName": "examplefilename",
- * "keyFilter" : "f1='1 '",
- * "chooseN" : 5,
- * "compressed" : "false"
- * "input" : {
- * "f1" : "string5",
- * "f2" : "string5"
- * },
- * "output" : {
- * "f2" : "string",
- * "f1" : "real"
- * }
- * }
- * }
- * OR
- * {
- * "format" : "xml",
- * "handle" : "1234",
- * "node" : {
- * "kind" : "indexread",
- * "fileName": "examplefilename",
- * "keyFilter" : "f1='1 '",
- * "input" : {
- * "f1" : "string5",
- * "f2" : "string5"
- * },
- * "output" : {
- * "f2" : "string",
- * "f1" : "real"
- * }
- * }
- * OR
- * {
- * "format" : "xml",
- * "node" : {
- * "kind" : "xmlread",
- * "fileName": "examplefilename",
- * "keyFilter" : "f1='1 '",
- * "input" : {
- * "f1" : "string5",
- * "f2" : "string5"
- * },
- * "output" : {
- * "f2" : "string",
- * "f1" : "real"
- * }
- * "ActivityOptions" : { // usually not required, options here may override file meta info.
- * "rowTag" : "/Dataset/OtherRow"
- * }
- * }
- * OR
- * {
- * "format" : "xml",
- * "node" : {
- * "kind" : "csvread",
- * "fileName": "examplefilename",
- * "keyFilter" : "f1='1 '",
- * "input" : {
- * "f1" : "string5",
- * "f2" : "string5"
- * },
- * "output" : {
- * "f2" : "string",
- * "f1" : "real"
- * }
- * "ActivityOptions" : { // usually not required, options here may override file meta info.
- * "csvQuote" : "\"",
- * "csvSeparate" : ","
- * "csvTerminate" : "\\n,\\r\\n",
- * }
- * }
- * OR
- * {
- * "format" : "xml",
- * "node" : {
- * "action" : "count", // if present performs count with/without filter and returns count
- * "fileName": "examplefilename", // can be either index or flat file
- * "keyFilter" : "f1='1 '",
- * "input" : {
- * "f1" : "string5",
- * "f2" : "string5"
- * },
- * }
- * }
- * OR
- * {
- * "format" : "binary",
- * "handle" : "1234",
- * "replyLimit" : "64",
- * "commCompression" : "LZ4",
- * "node" : {
- * "kind" : "diskwrite",
- * "fileName": "examplefilename",
- * "compressed" : "false" (or "LZ4", "FLZ", "LZW")
- * "input" : {
- * "f1" : "string5",
- * "f2" : "string5"
- * }
- * }
- * }
- * OR
- * {
- * "format" : "binary",
- * "handle" : "1234",
- * "replyLimit" : "64",
- * "node" : {
- * "kind" : "indexwrite",
- * "fileName": "examplefilename",
- * "input" : {
- * "f1" : "string5",
- * "f2" : "string5"
- * }
- * }
- * }
- *
- */
- int cursorHandle = requestTree->getPropInt("handle");
- OutputFormat outputFormat = outFmt_Xml;
- Owned<ICompressor> compressor;
- Owned<IExpander> expander;
- Owned<CRemoteRequest> remoteRequest;
- Owned<IRemoteActivity> outputActivity;
- OpenFileInfo fileInfo;
- if (!cursorHandle)
- {
- const char *outputFmtStr = requestTree->queryProp("format");
- if (nullptr == outputFmtStr)
- outputFormat = outFmt_Xml; // default
- else if (strieq("xml", outputFmtStr))
- outputFormat = outFmt_Xml;
- else if (strieq("json", outputFmtStr))
- outputFormat = outFmt_Json;
- else if (strieq("binary", outputFmtStr))
- outputFormat = outFmt_Binary;
- else
- throw MakeStringException(0, "Unrecognised output format: %s", outputFmtStr);
- /* pre-version 2.4, "outputCompression" denoted data was compressed in communication protocol and only applied to reply row data
- * Since 2.5 "commCompression" replaces "outputCompression", and applies to both incoming row data (write) and outgoing row data (read).
- * But "outputCompression" is checked for backward compatibility.
- */
- if (requestTree->hasProp("outputCompression") || requestTree->hasProp("commCompression"))
- {
- const char *commCompressionType = requestTree->queryProp("commCompression");
- if (isEmptyString(commCompressionType))
- commCompressionType = requestTree->queryProp("outputCompression");
- if (isEmptyString(commCompressionType))
- {
- compressor.setown(queryDefaultCompressHandler()->getCompressor());
- expander.setown(queryDefaultCompressHandler()->getExpander());
- }
- else if (outFmt_Binary == outputFormat)
- {
- compressor.setown(getCompressor(commCompressionType));
- expander.setown(getExpander(commCompressionType));
- if (!compressor)
- WARNLOG("Unknown compressor type specified: %s", commCompressionType);
- }
- else
- WARNLOG("Communication protocol compression not supported for format: %s", outputFmtStr);
- }
- /* NB: unless client call is on dedicated service, allow non-authorized requests through, e.g. from engines talking to unsecured port
- * In a secure setup, this service will be configured on a dedicated port, and the std. insecure dafilesrv will be unreachable.
- */
- bool authorizedOnly = rowServiceSock && client.isRowServiceClient();
- // In future this may be passed the request and build a chain of activities and return sink.
- outputActivity.setown(createOutputActivity(*requestTree, authorizedOnly, keyPairInfo));
- {
- CriticalBlock block(sect);
- cursorHandle = getNextHandle();
- }
- remoteRequest.setown(new CRemoteRequest(cursorHandle, outputFormat, compressor, expander, outputActivity));
- StringBuffer requestStr("jsonrequest:");
- outputActivity->getInfoStr(requestStr);
- Owned<StringAttrItem> name = new StringAttrItem(requestStr);
- CriticalBlock block(sect);
- client.previdx = client.openFiles.ordinality();
- client.openFiles.append(OpenFileInfo(cursorHandle, remoteRequest, name));
- }
- else if (!lookupFileIOHandle(cursorHandle, fileInfo))
- cursorHandle = 0; // challenge response ..
- else // known handle, continuation
- {
- remoteRequest.set(fileInfo.remoteRequest);
- outputFormat = fileInfo.remoteRequest->queryFormat();
- }
- if (cursorHandle)
- remoteRequest->process(requestTree, rest, reply, stats);
- else
- {
- const char *outputFmtStr = requestTree->queryProp("format");
- if (nullptr == outputFmtStr)
- outputFormat = outFmt_Xml; // default
- else if (strieq("xml", outputFmtStr))
- outputFormat = outFmt_Xml;
- else if (strieq("json", outputFmtStr))
- outputFormat = outFmt_Json;
- else if (strieq("binary", outputFmtStr))
- outputFormat = outFmt_Binary;
- else
- throw MakeStringException(0, "Unrecognised output format: %s", outputFmtStr);
- if (outFmt_Binary == outputFormat)
- reply.append(cursorHandle);
- else // outFmt_Xml || outFmt_Json
- {
- Owned<IXmlWriterExt> responseWriter = createIXmlWriterExt(0, 0, nullptr, outFmt_Xml == outputFormat ? WTStandard : WTJSONObject);
- responseWriter->outputBeginNested("Response", true);
- if (outFmt_Xml == outputFormat)
- responseWriter->outputCString("urn:hpcc:dfs", "@xmlns:dfs");
- responseWriter->outputUInt(cursorHandle, sizeof(cursorHandle), "handle");
- responseWriter->outputEndNested("Response");
- responseWriter->finalize();
- reply.append(responseWriter->length(), responseWriter->str());
- }
- }
- }
- void cmdStreamReadCommon(MemoryBuffer & msg, MemoryBuffer & reply, CRemoteClientHandler &client, CClientStats &stats)
- {
- size32_t jsonSz = msg.remaining();
- Owned<IPropertyTree> requestTree = createPTreeFromJSONString(jsonSz, (const char *)msg.readDirect(jsonSz));
- cmdStreamCommon(requestTree, msg, reply, client, stats);
- }
- // NB: JSON header to message, for some requests (e.g. write), there will be trailing raw data (e.g. row data)
- void cmdStreamReadStd(MemoryBuffer & msg, MemoryBuffer & reply, CRemoteClientHandler &client, CClientStats &stats)
- {
- reply.append(RFEnoerror); // gets patched if there is a follow on error
- cmdStreamReadCommon(msg, reply, client, stats);
- }
- void cmdStreamReadJSON(MemoryBuffer & msg, MemoryBuffer & reply, CRemoteClientHandler &client, CClientStats &stats)
- {
- /* NB: exactly the same handling as cmdStreamReadStd(RFCStreamRead) for now,
- * may want to differentiate later
- * i.e. return format is { len[unsigned4-bigendian], errorcode[unsigned4-bigendian], result } - where result format depends on request output type.
- * errorcode = 0 means no error
- */
- reply.append(RFEnoerror); // gets patched if there is a follow on error
- cmdStreamReadCommon(msg, reply, client, stats);
- }
- void cmdStreamReadTestSocket(MemoryBuffer & msg, MemoryBuffer & reply, CRemoteClientHandler &client, CClientStats &stats)
- {
- reply.append('J');
- cmdStreamReadCommon(msg, reply, client, stats);
- }
- // legacy version
- void cmdSetThrottle(MemoryBuffer & msg, MemoryBuffer & reply)
- {
- unsigned limit, delayMs, cpuThreshold;
- msg.read(limit);
- msg.read(delayMs);
- msg.read(cpuThreshold);
- stdCmdThrottler.configure(limit, delayMs, cpuThreshold, (unsigned)-1);
- reply.append((unsigned)RFEnoerror);
- }
- void cmdSetThrottle2(MemoryBuffer & msg, MemoryBuffer & reply)
- {
- unsigned throttleClass, limit, delayMs, cpuThreshold, queueLimit;
- msg.read(throttleClass);
- msg.read(limit);
- msg.read(delayMs);
- msg.read(cpuThreshold);
- msg.read(queueLimit);
- setThrottle((ThrottleClass)throttleClass, limit, delayMs, cpuThreshold, queueLimit);
- reply.append((unsigned)RFEnoerror);
- }
- void formatException(MemoryBuffer &reply, IException *e, RemoteFileCommandType cmd, bool testSocketFlag, unsigned _dfsErrorCode, CRemoteClientHandler *client)
- {
- unsigned dfsErrorCode = _dfsErrorCode;
- if (!dfsErrorCode)
- {
- if (e)
- dfsErrorCode = (QUERYINTERFACE(e, IDAFS_Exception)) ? e->errorCode() : RFSERR_InternalError;
- else
- dfsErrorCode = RFSERR_InternalError;
- }
- VStringBuffer errMsg("ERROR: cmd=%s, error=%s", getRFCText(cmd), getRFSERRText(dfsErrorCode));
- if (e)
- {
- errMsg.appendf(" (%u, ", e->errorCode());
- unsigned len = errMsg.length();
- e->errorMessage(errMsg);
- if (len == errMsg.length())
- errMsg.setLength(len-2); // strip off ", " if no message in exception
- errMsg.append(")");
- }
- if (testSocketFlag)
- reply.append('-');
- else
- reply.append(dfsErrorCode);
- reply.append(errMsg.str());
- if (client && cmd!=RFCunlock)
- {
- const char *peer = client->queryPeerName();
- if (peer)
- {
- VStringBuffer err("%s. Client: %s", errMsg.str(), peer);
- PROGLOG("%s", err.str());
- }
- client->logPrevHandle();
- }
- }
- void throttleCommand(RemoteFileCommandType cmd, MemoryBuffer &msg, CRemoteClientHandler *client)
- {
- switch (cmd)
- {
- case RFCexec:
- case RFCgetcrc:
- case RFCcopy:
- case RFCappend:
- case RFCtreecopy:
- case RFCtreecopytmp:
- case RFCremove:
- case RFCcopysection:
- slowCmdThrottler.addCommand(cmd, msg, client);
- return;
- case RFCcloseIO:
- case RFCopenIO:
- case RFCread:
- case RFCsize:
- case RFCwrite:
- case RFCexists:
- case RFCrename:
- case RFCgetver:
- case RFCisfile:
- case RFCisdirectory:
- case RFCisreadonly:
- case RFCsetreadonly:
- case RFCsetfileperms:
- case RFCreadfilteredindex:
- case RFCreadfilteredindexcount:
- case RFCreadfilteredindexblob:
- case RFCgettime:
- case RFCsettime:
- case RFCcreatedir:
- case RFCgetdir:
- case RFCmonitordir:
- case RFCstop:
- case RFCextractblobelements:
- case RFCredeploy:
- case RFCmove:
- case RFCsetsize:
- case RFCsettrace:
- case RFCgetinfo:
- case RFCfirewall:
- case RFCStreamRead:
- case RFCStreamReadTestSocket:
- case RFCStreamReadJSON:
- stdCmdThrottler.addCommand(cmd, msg, client);
- return;
- // NB: The following commands are still bound by the the thread pool
- case RFCsetthrottle: // legacy version
- case RFCsetthrottle2:
- default:
- {
- client->processCommand(cmd, msg, NULL);
- break;
- }
- }
- }
- void checkAuthorizedStreamCommand(CRemoteClientHandler &client)
- {
- if (!rowServiceOnStdPort && !client.isRowServiceClient())
- throw createDafsException(DAFSERR_cmdstream_unauthorized, "Unauthorized command");
- }
- bool processCommand(RemoteFileCommandType cmd, MemoryBuffer & msg, MemoryBuffer & reply, CRemoteClientHandler *client, CThrottler *throttler)
- {
- Owned<CClientStats> stats = clientStatsTable.getClientReference(cmd, client->queryPeerName());
- bool testSocketFlag = false;
- unsigned posOfErr = reply.length();
- try
- {
- switch(cmd)
- {
- MAPCOMMANDSTATS(RFCread, cmdRead, *stats);
- MAPCOMMANDSTATS(RFCwrite, cmdWrite, *stats);
- MAPCOMMANDCLIENTSTATS(RFCappend, cmdAppend, *client, *stats);
- MAPCOMMAND(RFCcloseIO, cmdCloseFileIO);
- MAPCOMMANDCLIENT(RFCopenIO, cmdOpenFileIO, *client);
- MAPCOMMAND(RFCsize, cmdSize);
- MAPCOMMANDCLIENT(RFCexists, cmdExists, *client);
- MAPCOMMANDCLIENT(RFCremove, cmdRemove, *client);
- MAPCOMMANDCLIENT(RFCrename, cmdRename, *client);
- MAPCOMMAND(RFCgetver, cmdGetVer);
- MAPCOMMANDCLIENT(RFCisfile, cmdIsFile, *client);
- MAPCOMMANDCLIENT(RFCisdirectory, cmdIsDir, *client);
- MAPCOMMANDCLIENT(RFCisreadonly, cmdIsReadOnly, *client);
- MAPCOMMANDCLIENT(RFCsetreadonly, cmdSetReadOnly, *client);
- MAPCOMMANDCLIENT(RFCsetfileperms, cmdSetFilePerms, *client);
- MAPCOMMANDCLIENT(RFCgettime, cmdGetTime, *client);
- MAPCOMMANDCLIENT(RFCsettime, cmdSetTime, *client);
- MAPCOMMANDCLIENT(RFCcreatedir, cmdCreateDir, *client);
- MAPCOMMANDCLIENT(RFCgetdir, cmdGetDir, *client);
- MAPCOMMANDCLIENT(RFCmonitordir, cmdMonitorDir, *client);
- MAPCOMMAND(RFCstop, cmdStop);
- MAPCOMMANDCLIENT(RFCexec, cmdExec, *client);
- MAPCOMMANDCLIENT(RFCextractblobelements, cmdExtractBlobElements, *client);
- MAPCOMMANDCLIENT(RFCgetcrc, cmdGetCRC, *client);
- MAPCOMMANDCLIENT(RFCmove, cmdMove, *client);
- MAPCOMMANDCLIENT(RFCcopy, cmdCopy, *client);
- MAPCOMMAND(RFCsetsize, cmdSetSize);
- MAPCOMMAND(RFCsettrace, cmdSetTrace);
- MAPCOMMAND(RFCgetinfo, cmdGetInfo);
- MAPCOMMAND(RFCfirewall, cmdFirewall);
- MAPCOMMANDCLIENT(RFCcopysection, cmdCopySection, *client);
- MAPCOMMANDCLIENTTHROTTLE(RFCtreecopy, cmdTreeCopy, *client, &slowCmdThrottler);
- MAPCOMMANDCLIENTTHROTTLE(RFCtreecopytmp, cmdTreeCopyTmp, *client, &slowCmdThrottler);
- MAPCOMMAND(RFCsetthrottle, cmdSetThrottle); // legacy version
- MAPCOMMAND(RFCsetthrottle2, cmdSetThrottle2);
- // row service commands
- case RFCStreamGeneral:
- {
- checkAuthorizedStreamCommand(*client);
- reply.append(RFEnoerror); // gets patched if there is a follow on error
- cmdStreamGeneral(msg, reply, *client, *stats);
- break;
- }
- case RFCStreamRead:
- {
- checkAuthorizedStreamCommand(*client);
- cmdStreamReadStd(msg, reply, *client, *stats);
- break;
- }
- case RFCStreamReadJSON:
- {
- checkAuthorizedStreamCommand(*client);
- cmdStreamReadJSON(msg, reply, *client, *stats);
- break;
- }
- case RFCStreamReadTestSocket:
- {
- testSocketFlag = true;
- checkAuthorizedStreamCommand(*client);
- cmdStreamReadTestSocket(msg, reply, *client, *stats);
- break;
- }
- default:
- formatException(reply, nullptr, cmd, false, RFSERR_InvalidCommand, client);
- break;
- }
- }
- catch (IException *e)
- {
- checkOutOfHandles(e);
- reply.setWritePos(posOfErr);
- formatException(reply, e, cmd, testSocketFlag, 0, client);
- e->Release();
- }
- return testSocketFlag;
- }
- IPooledThread *createCommandProcessor()
- {
- return new cCommandProcessor();
- }
- void checkOutOfHandles(IException *exception)
- {
- if (EMFILE == exception->errorCode())
- handleTracer.traceIfReady();
- }
- virtual void run(DAFSConnectCfg _connectMethod, const SocketEndpoint &listenep, unsigned sslPort, const SocketEndpoint *rowServiceEp, bool _rowServiceSSL, bool _rowServiceOnStdPort) override
- {
- SocketEndpoint sslep(listenep);
- if (sslPort)
- sslep.port = sslPort;
- else
- sslep.port = securitySettings.daFileSrvSSLPort;
- Owned<ISocket> acceptSock, secureSock, rowServiceSock;
- if (_connectMethod != SSLOnly)
- {
- if (listenep.port == 0)
- throw createDafsException(DAFSERR_serverinit_failed, "dafilesrv port not specified");
- if (listenep.isNull())
- acceptSock.setown(ISocket::create(listenep.port));
- else
- {
- StringBuffer ips;
- listenep.getIpText(ips);
- acceptSock.setown(ISocket::create_ip(listenep.port,ips.str()));
- }
- }
- if (_connectMethod == SSLOnly || _connectMethod == SSLFirst || _connectMethod == UnsecureFirst)
- {
- if (sslep.port == 0)
- throw createDafsException(DAFSERR_serverinit_failed, "Secure dafilesrv port not specified");
- if (_connectMethod == UnsecureFirst)
- {
- // don't fail, but warn - this allows for fast SSL client rejections
- if (!securitySettings.certificate)
- WARNLOG("SSL Certificate information not found in environment.conf, cannot accept SSL connections");
- else if ( !checkFileExists(securitySettings.certificate) )
- {
- WARNLOG("SSL Certificate File not found in environment.conf, cannot accept SSL connections");
- securitySettings.certificate = nullptr;
- }
- if (!securitySettings.privateKey)
- WARNLOG("SSL Key information not found in environment.conf, cannot accept SSL connections");
- else if ( !checkFileExists(securitySettings.privateKey) )
- {
- WARNLOG("SSL Key File not found in environment.conf, cannot accept SSL connections");
- securitySettings.privateKey = nullptr;
- }
- }
- else
- validateSSLSetup();
- if (sslep.isNull())
- secureSock.setown(ISocket::create(sslep.port));
- else
- {
- StringBuffer ips;
- sslep.getIpText(ips);
- secureSock.setown(ISocket::create_ip(sslep.port,ips.str()));
- }
- }
- if (rowServiceEp)
- {
- rowServiceSSL = _rowServiceSSL;
- rowServiceOnStdPort = _rowServiceOnStdPort;
- if (rowServiceEp->isNull())
- rowServiceSock.setown(ISocket::create(rowServiceEp->port));
- else
- {
- StringBuffer ips;
- rowServiceEp->getIpText(ips);
- rowServiceSock.setown(ISocket::create_ip(rowServiceEp->port, ips.str()));
- }
- #ifdef _USE_OPENSSL
- if (rowServiceSSL)
- validateSSLSetup();
- #else
- rowServiceSSL = false;
- #endif
- }
- run(_connectMethod, acceptSock.getClear(), secureSock.getClear(), rowServiceSock.getClear());
- }
- virtual void run(DAFSConnectCfg _connectMethod, ISocket *_acceptSock, ISocket *_secureSock, ISocket *_rowServiceSock) override
- {
- acceptsock.setown(_acceptSock);
- securesock.setown(_secureSock);
- rowServiceSock.setown(_rowServiceSock);
- if (_connectMethod != SSLOnly)
- {
- if (!acceptsock)
- throw createDafsException(DAFSERR_serverinit_failed, "Invalid non-secure socket");
- }
- if (_connectMethod == SSLOnly || _connectMethod == SSLFirst || _connectMethod == UnsecureFirst)
- {
- if (!securesock)
- throw createDafsException(DAFSERR_serverinit_failed, "Invalid secure socket");
- }
- selecthandler->start();
- Owned<IException> exception;
- for (;;)
- {
- Owned<ISocket> sock;
- Owned<ISocket> sockSSL;
- Owned<ISocket> acceptedRSSock;
- bool sockavail = false;
- bool securesockavail = false;
- bool rowServiceSockAvail = false;
- if (_connectMethod == SSLNone && (nullptr == rowServiceSock.get()))
- sockavail = acceptsock->wait_read(1000*60*1)!=0;
- else if (_connectMethod == SSLOnly && (nullptr == rowServiceSock.get()))
- securesockavail = securesock->wait_read(1000*60*1)!=0;
- else
- {
- UnsignedArray readSocks;
- UnsignedArray waitingSocks;
- if (acceptsock)
- readSocks.append(acceptsock->OShandle());
- if (securesock)
- readSocks.append(securesock->OShandle());
- if (rowServiceSock)
- readSocks.append(rowServiceSock->OShandle());
- int numReady = wait_read_multiple(readSocks, 1000*60*1, waitingSocks);
- if (numReady > 0)
- {
- for (int idx = 0; idx < numReady; idx++)
- {
- unsigned waitingSock = waitingSocks.item(idx);
- if (acceptsock && (waitingSock == acceptsock->OShandle()))
- sockavail = true;
- else if (securesock && (waitingSock == securesock->OShandle()))
- securesockavail = true;
- else if (rowServiceSock && (waitingSock == rowServiceSock->OShandle()))
- rowServiceSockAvail = true;
- }
- }
- }
- #if 0
- if (!sockavail && !securesockavail && !rowServiceSockAvail)
- {
- JSocketStatistics stats;
- getSocketStatistics(stats);
- StringBuffer s;
- getSocketStatisticsString(stats,s);
- PROGLOG( "Socket statistics : \n%s\n",s.str());
- }
- #endif
- if (stopping)
- break;
- if (sockavail || securesockavail || rowServiceSockAvail)
- {
- if (sockavail)
- {
- try
- {
- sock.setown(acceptsock->accept(true));
- if (!sock||stopping)
- break;
- }
- catch (IException *e)
- {
- exception.setown(e);
- }
- if (exception)
- {
- EXCLOG(exception, "CRemoteFileServer");
- checkOutOfHandles(exception);
- exception.clear();
- sockavail = false;
- }
- }
- if (securesockavail)
- {
- Owned<ISecureSocket> ssock;
- try
- {
- sockSSL.setown(securesock->accept(true));
- if (!sockSSL||stopping)
- break;
- if ( (_connectMethod == UnsecureFirst) && (!securitySettings.certificate || !securitySettings.privateKey) )
- {
- // for client secure_connect() to fail quickly ...
- cleanupDaFsSocket(sockSSL);
- sockSSL.clear();
- securesockavail = false;
- }
- else
- {
- ssock.setown(createSecureSocket(sockSSL.getClear(), ServerSocket));
- int status = ssock->secure_accept();
- if (status < 0)
- throw createDafsException(DAFSERR_serveraccept_failed,"Failure to establish secure connection");
- sockSSL.setown(ssock.getLink());
- }
- }
- catch (IException *e)
- {
- exception.setown(e);
- }
- if (exception)
- {
- EXCLOG(exception, "CRemoteFileServer (secure)");
- cleanupDaFsSocket(sockSSL);
- sockSSL.clear();
- cleanupDaFsSocket(ssock);
- ssock.clear();
- checkOutOfHandles(exception);
- exception.clear();
- securesockavail = false;
- }
- }
- if (rowServiceSockAvail)
- {
- Owned<ISecureSocket> ssock;
- try
- {
- acceptedRSSock.setown(rowServiceSock->accept(true));
- if (!acceptedRSSock||stopping)
- break;
- if (rowServiceSSL) // NB: will be disabled if !_USE_OPENSLL
- {
- ssock.setown(createSecureSocket(acceptedRSSock.getClear(), ServerSocket));
- int status = ssock->secure_accept();
- if (status < 0)
- throw createDafsException(DAFSERR_serveraccept_failed,"Failure to establish SSL row service connection");
- acceptedRSSock.setown(ssock.getLink());
- }
- }
- catch (IException *e)
- {
- exception.setown(e);
- }
- if (exception)
- {
- EXCLOG(exception, "CRemoteFileServer (row service)");
- cleanupDaFsSocket(acceptedRSSock);
- acceptedRSSock.clear();
- cleanupDaFsSocket(ssock);
- ssock.clear();
- checkOutOfHandles(exception);
- exception.clear();
- rowServiceSockAvail = false;
- }
- }
- #ifdef _DEBUG
- SocketEndpoint eps;
- StringBuffer peerURL;
- #endif
- if (sockavail)
- {
- #ifdef _DEBUG
- sock->getPeerEndpoint(eps);
- eps.getUrlStr(peerURL);
- PROGLOG("Server accepting from %s", peerURL.str());
- #endif
- runClient(sock.getClear(), false);
- }
- if (securesockavail)
- {
- #ifdef _DEBUG
- sockSSL->getPeerEndpoint(eps);
- eps.getUrlStr(peerURL.clear());
- PROGLOG("Server accepting SECURE from %s", peerURL.str());
- #endif
- runClient(sockSSL.getClear(), false);
- }
- if (rowServiceSockAvail)
- {
- #ifdef _DEBUG
- acceptedRSSock->getPeerEndpoint(eps);
- eps.getUrlStr(peerURL.clear());
- PROGLOG("Server accepting row service socket from %s", peerURL.str());
- #endif
- runClient(acceptedRSSock.getClear(), true);
- }
- }
- else
- checkTimeout();
- }
- if (TF_TRACE_CLIENT_STATS)
- PROGLOG("CRemoteFileServer:run exiting");
- selecthandler->stop(true);
- }
- void processUnauthenticatedCommand(RemoteFileCommandType cmd, ISocket *socket, MemoryBuffer &msg)
- {
- // these are unauthenticated commands
- if (cmd != RFCgetver)
- cmd = RFCinvalid;
- MemoryBuffer reply;
- bool testSocketFlag = processCommand(cmd, msg, initSendBuffer(reply), NULL, NULL);
- sendDaFsBuffer(socket, reply, testSocketFlag);
- }
- void runClient(ISocket *sock, bool rowService) // rowService used to distinguish client calls
- {
- cCommandProcessor::cCommandProcessorParams params;
- params.client = new CRemoteClientHandler(this, sock, globallasttick, rowService);
- {
- CriticalBlock block(sect);
- clients.append(*LINK(params.client));
- }
- // NB: This could be blocked, by thread pool limit
- threads->start(¶ms);
- }
- void stop()
- {
- // stop accept loop
- if (TF_TRACE_CLIENT_STATS)
- PROGLOG("CRemoteFileServer::stop");
- if (acceptsock)
- acceptsock->cancel_accept();
- if (securesock)
- securesock->cancel_accept();
- threads->stopAll();
- threads->joinAll(true,60*1000);
- }
- bool notify(CRemoteClientHandler *_client, MemoryBuffer &msg)
- {
- Linked<CRemoteClientHandler> client;
- client.set(_client);
- if (TF_TRACE_FULL)
- PROGLOG("notify %d", msg.length());
- if (msg.length())
- {
- if (TF_TRACE_FULL)
- PROGLOG("notify CRemoteClientHandler(%p), msg length=%u", _client, msg.length());
- cCommandProcessor::cCommandProcessorParams params;
- params.client = client.getClear();
- params.msg.swapWith(msg);
- /* This can block because the thread pool is full and therefore block the selecthandler
- * This is akin to the main server blocking post accept() for the same reason.
- */
- threads->start(¶ms);
- }
- else
- onCloseSocket(client,3); // removes owned handles
- return false;
- }
- void addClient(CRemoteClientHandler *client)
- {
- if (client&&client->socket)
- selecthandler->add(client->socket,SELECTMODE_READ,client);
- }
- void checkTimeout()
- {
- if (msTick()-clientcounttick>1000*60*60)
- {
- CriticalBlock block(ClientCountSect);
- if (TF_TRACE_CLIENT_STATS && (ClientCount || MaxClientCount))
- PROGLOG("Client count = %d, max = %d", ClientCount, MaxClientCount);
- clientcounttick = msTick();
- MaxClientCount = ClientCount;
- if (closedclients)
- {
- if (TF_TRACE_CLIENT_STATS)
- PROGLOG("Closed client count = %d",closedclients);
- closedclients = 0;
- }
- }
- CriticalBlock block(sect);
- ForEachItemInRev(i,clients)
- {
- CRemoteClientHandler &client = clients.item(i);
- if (client.timedOut())
- {
- StringBuffer s;
- bool ok = client.getInfo(s); // will spot duff sockets
- if (ok&&(client.openFiles.ordinality()!=0))
- {
- if (TF_TRACE_CLIENT_CONN && client.inactiveTimedOut())
- WARNLOG("Inactive %s",s.str());
- }
- else
- {
- #ifndef _DEBUG
- if (TF_TRACE_CLIENT_CONN)
- #endif
- PROGLOG("Timing out %s",s.str());
- closedclients++;
- onCloseSocket(&client,4); // removes owned handles
- }
- }
- }
- }
- void getInfo(StringBuffer &info, unsigned level=1)
- {
- {
- CriticalBlock block(ClientCountSect);
- info.append(DAFILESRV_VERSIONSTRING).append('\n');
- info.appendf("Client count = %d\n",ClientCount);
- info.appendf("Max client count = %d",MaxClientCount);
- }
- CriticalBlock block(sect);
- ForEachItemIn(i,clients)
- {
- info.newline().append(i).append(": ");
- clients.item(i).getInfo(info);
- }
- info.newline().appendf("Running threads: %u", threadRunningCount());
- info.newline();
- stdCmdThrottler.getInfo(info);
- info.newline();
- slowCmdThrottler.getInfo(info);
- clientStatsTable.getInfo(info, level);
- }
- unsigned threadRunningCount()
- {
- if (!threads)
- return 0;
- return threads->runningCount();
- }
- unsigned idleTime()
- {
- unsigned t = (unsigned)atomic_read(&globallasttick);
- return msTick()-t;
- }
- void setThrottle(ThrottleClass throttleClass, unsigned limit, unsigned delayMs, unsigned cpuThreshold, unsigned queueLimit)
- {
- switch (throttleClass)
- {
- case ThrottleStd:
- stdCmdThrottler.configure(limit, delayMs, cpuThreshold, queueLimit);
- break;
- case ThrottleSlow:
- slowCmdThrottler.configure(limit, delayMs, cpuThreshold, queueLimit);
- break;
- default:
- {
- StringBuffer availableClasses("{ ");
- for (unsigned c=0; c<ThrottleClassMax; c++)
- {
- availableClasses.append(c).append(" = ").append(getThrottleClassText((ThrottleClass)c));
- if (c+1<ThrottleClassMax)
- availableClasses.append(", ");
- }
- availableClasses.append(" }");
- throw MakeStringException(0, "Unknown throttle class: %u, available classes are: %s", (unsigned)throttleClass, availableClasses.str());
- }
- }
- }
- StringBuffer &getStats(StringBuffer &stats, bool reset)
- {
- CriticalBlock block(sect);
- stdCmdThrottler.getStats(stats, reset).newline();
- slowCmdThrottler.getStats(stats, reset);
- if (reset)
- clientStatsTable.reset();
- return stats;
- }
- };
- IRemoteFileServer * createRemoteFileServer(unsigned maxThreads, unsigned maxThreadsDelayMs, unsigned maxAsyncCopy, IPropertyTree *keyPairInfo)
- {
- return new CRemoteFileServer(maxThreads, maxThreadsDelayMs, maxAsyncCopy, keyPairInfo);
- }
- int setDaliServerTrace(byte flags)
- {
- byte ret = traceFlags;
- traceFlags = flags;
- return ret;
- }
- #ifdef _USE_CPPUNIT
- #include "unittests.hpp"
- #include "rmtfile.hpp"
- /* MP_START_PORT -> MP_END_PORT is the MP reserved dynamic port range, and is used here for convenience.
- * MP_START_PORT is used as starting point to find an available port for the temporary dafilesrv service in these unittests.
- * All (MP) components using this range always check and find an unused port.
- */
- static unsigned serverPort = MP_START_PORT;
- static StringBuffer basePath;
- static Owned<CSimpleInterface> serverThread;
- class RemoteFileSlowTest : public CppUnit::TestFixture
- {
- CPPUNIT_TEST_SUITE(RemoteFileSlowTest);
- CPPUNIT_TEST(testRemoteFilename);
- CPPUNIT_TEST(testStartServer);
- CPPUNIT_TEST(testBasicFunctionality);
- CPPUNIT_TEST(testCopy);
- CPPUNIT_TEST(testOther);
- CPPUNIT_TEST(testConfiguration);
- CPPUNIT_TEST(testDirectoryMonitoring);
- CPPUNIT_TEST(testFinish);
- CPPUNIT_TEST_SUITE_END();
- size32_t testLen = 1024;
- protected:
- void testRemoteFilename()
- {
- const char *rfns = "//1.2.3.4/dir1/file1|//1.2.3.4:7100/dir1/file1,"
- "//1.2.3.4:7100/dir1/file1|//1.2.3.4:7100/dir1/file1,"
- "//1.2.3.4/c$/dir1/file1|//1.2.3.4:7100/c$/dir1/file1,"
- "//1.2.3.4:7100/c$/dir1/file1|//1.2.3.4:7100/c$/dir1/file1,"
- "//1.2.3.4:7100/d$/dir1/file1|//1.2.3.4:7100/d$/dir1/file1";
- StringArray tests;
- tests.appendList(rfns, ",");
- ForEachItemIn(i, tests)
- {
- StringArray inOut;
- const char *pair = tests.item(i);
- inOut.appendList(pair, "|");
- const char *rfn = inOut.item(0);
- const char *expected = inOut.item(1);
- Owned<IFile> iFile = createIFile(rfn);
- const char *res = iFile->queryFilename();
- if (!streq(expected, res))
- {
- StringBuffer errMsg("testRemoteFilename MISMATCH");
- errMsg.newline().append("Expected: ").append(expected);
- errMsg.newline().append("Got: ").append(res);
- PROGLOG("%s", errMsg.str());
- CPPUNIT_ASSERT_MESSAGE(errMsg.str(), 0);
- }
- else
- PROGLOG("MATCH: %s", res);
- }
- }
- void testStartServer()
- {
- Owned<ISocket> socket;
- unsigned endPort = MP_END_PORT;
- while (1)
- {
- try
- {
- socket.setown(ISocket::create(serverPort));
- break;
- }
- catch (IJSOCK_Exception *e)
- {
- if (e->errorCode() != JSOCKERR_port_in_use)
- {
- StringBuffer eStr;
- e->errorMessage(eStr);
- e->Release();
- CPPUNIT_ASSERT_MESSAGE(eStr.str(), 0);
- }
- else if (serverPort == endPort)
- {
- e->Release();
- CPPUNIT_ASSERT_MESSAGE("Could not find a free port to use for remote file server", 0);
- }
- }
- ++serverPort;
- }
- basePath.append("//");
- SocketEndpoint ep(serverPort);
- ep.getUrlStr(basePath);
- char cpath[_MAX_DIR];
- if (!GetCurrentDirectory(_MAX_DIR, cpath))
- CPPUNIT_ASSERT_MESSAGE("Current directory path too big", 0);
- else
- basePath.append(cpath);
- addPathSepChar(basePath);
- PROGLOG("basePath = %s", basePath.str());
- class CServerThread : public CSimpleInterface, implements IThreaded
- {
- CThreaded threaded;
- Owned<CRemoteFileServer> server;
- Linked<ISocket> socket;
- public:
- CServerThread(CRemoteFileServer *_server, ISocket *_socket) : server(_server), socket(_socket), threaded("CServerThread")
- {
- threaded.init(this);
- }
- ~CServerThread()
- {
- threaded.join();
- }
- // IThreaded
- virtual void threadmain() override
- {
- DAFSConnectCfg sslCfg = SSLNone;
- server->run(sslCfg, socket, nullptr, nullptr);
- }
- };
- Owned<IRemoteFileServer> server = createRemoteFileServer();
- serverThread.setown(new CServerThread(QUERYINTERFACE(server.getClear(), CRemoteFileServer), socket.getClear()));
- }
- void testBasicFunctionality()
- {
- VStringBuffer filePath("%s%s", basePath.str(), "file1");
- // create file
- Owned<IFile> iFile = createIFile(filePath);
- CPPUNIT_ASSERT(iFile);
- Owned<IFileIO> iFileIO = iFile->open(IFOcreate);
- CPPUNIT_ASSERT(iFileIO);
- // write out 1k of random data and crc
- MemoryBuffer mb;
- char *buf = (char *)mb.reserveTruncate(testLen);
- for (unsigned b=0; b<1024; b++)
- buf[b] = getRandom()%256;
- CRC32 crc;
- crc.tally(testLen, buf);
- unsigned writeCrc = crc.get();
- size32_t sz = iFileIO->write(0, testLen, buf);
- CPPUNIT_ASSERT(sz == testLen);
- // close file
- iFileIO.clear();
- // validate remote crc
- CPPUNIT_ASSERT(writeCrc == iFile->getCRC());
- // exists
- CPPUNIT_ASSERT(iFile->exists());
- // validate size
- CPPUNIT_ASSERT(iFile->size() == testLen);
- // read back and validate read data's crc against written
- iFileIO.setown(iFile->open(IFOread));
- CPPUNIT_ASSERT(iFileIO);
- sz = iFileIO->read(0, testLen, buf);
- iFileIO.clear();
- CPPUNIT_ASSERT(sz == testLen);
- crc.reset();
- crc.tally(testLen, buf);
- CPPUNIT_ASSERT(writeCrc == crc.get());
- }
- void testCopy()
- {
- VStringBuffer filePath("%s%s", basePath.str(), "file1");
- Owned<IFile> iFile = createIFile(filePath);
- // test file copy
- VStringBuffer filePathCopy("%s%s", basePath.str(), "file1copy");
- Owned<IFile> iFile1Copy = createIFile(filePathCopy);
- iFile->copyTo(iFile1Copy);
- // read back copy and validate read data's crc against written
- Owned<IFileIO> iFileIO = iFile1Copy->open(IFOreadwrite); // open read/write for appendFile in next step.
- CPPUNIT_ASSERT(iFileIO);
- MemoryBuffer mb;
- char *buf = (char *)mb.reserveTruncate(testLen);
- size32_t sz = iFileIO->read(0, testLen, buf);
- CPPUNIT_ASSERT(sz == testLen);
- CRC32 crc;
- crc.tally(testLen, buf);
- CPPUNIT_ASSERT(iFile->getCRC() == crc.get());
- // check appendFile functionality. NB after this "file1copy" should be 2*testLen
- CPPUNIT_ASSERT(testLen == iFileIO->appendFile(iFile));
- iFileIO.clear();
- // validate new size
- CPPUNIT_ASSERT(iFile1Copy->size() == 2 * testLen);
- // setSize test, truncate copy to original size
- iFileIO.setown(iFile1Copy->open(IFOreadwrite));
- iFileIO->setSize(testLen);
- // validate new size
- CPPUNIT_ASSERT(iFile1Copy->size() == testLen);
- }
- void testOther()
- {
- VStringBuffer filePath("%s%s", basePath.str(), "file1");
- Owned<IFile> iFile = createIFile(filePath);
- // rename
- iFile->rename("file2");
- // create a directory
- VStringBuffer subDirPath("%s%s", basePath.str(), "subdir1");
- Owned<IFile> subDirIFile = createIFile(subDirPath);
- subDirIFile->createDirectory();
- // check isDirectory result
- CPPUNIT_ASSERT(subDirIFile->isDirectory()==fileBool::foundYes);
- // move previous created and renamed file into new sub-directory
- // ensure not present before move
- VStringBuffer subDirFilePath("%s/%s", subDirPath.str(), "file2");
- Owned<IFile> iFile2 = createIFile(subDirFilePath);
- iFile2->remove();
- iFile->move(subDirFilePath);
- // open sub-directory file2 explicitly
- RemoteFilename rfn;
- rfn.setRemotePath(subDirPath.str());
- Owned<IFile> dir = createIFile(rfn);
- Owned<IDirectoryIterator> diriter = dir->directoryFiles("file2");
- if (!diriter->first())
- {
- CPPUNIT_ASSERT_MESSAGE("Error, file2 diriter->first() is null", 0);
- }
- Linked<IFile> iFile3 = &diriter->query();
- diriter.clear();
- dir.clear();
- OwnedIFileIO iFile3IO = iFile3->openShared(IFOread, IFSHfull);
- if (!iFile3IO)
- {
- CPPUNIT_ASSERT_MESSAGE("Error, file2 openShared() failed", 0);
- }
- iFile3IO->close();
- // count sub-directory files with a wildcard
- unsigned count=0;
- Owned<IDirectoryIterator> iter = subDirIFile->directoryFiles("*2");
- ForEach(*iter)
- ++count;
- CPPUNIT_ASSERT(1 == count);
- // check isFile result
- CPPUNIT_ASSERT(iFile2->isFile()==fileBool::foundYes);
- // validate isReadOnly before after setting
- CPPUNIT_ASSERT(iFile2->isReadOnly()==fileBool::foundNo);
- iFile2->setReadOnly(true);
- CPPUNIT_ASSERT(iFile2->isReadOnly()==fileBool::foundYes);
- // get/set Time and validate result
- CDateTime createTime, modifiedTime, accessedTime;
- CPPUNIT_ASSERT(subDirIFile->getTime(&createTime, &modifiedTime, &accessedTime));
- CDateTime newModifiedTime = modifiedTime;
- newModifiedTime.adjustTime(-86400); // -1 day
- CPPUNIT_ASSERT(subDirIFile->setTime(&createTime, &newModifiedTime, &accessedTime));
- CPPUNIT_ASSERT(subDirIFile->getTime(&createTime, &modifiedTime, &accessedTime));
- CPPUNIT_ASSERT(modifiedTime == newModifiedTime);
- // test set file permissions
- try
- {
- iFile2->setFilePermissions(0777);
- }
- catch (...)
- {
- CPPUNIT_ASSERT_MESSAGE("iFile2->setFilePermissions() exception", 0);
- }
- }
- void testConfiguration()
- {
- SocketEndpoint ep(serverPort); // test trace open connections
- CPPUNIT_ASSERT(setDafileSvrTraceFlags(ep, 0x08));
- StringBuffer infoStr;
- CPPUNIT_ASSERT(RFEnoerror == getDafileSvrInfo(ep, 10, infoStr));
- CPPUNIT_ASSERT(RFEnoerror == setDafileSvrThrottleLimit(ep, ThrottleStd, DEFAULT_STDCMD_PARALLELREQUESTLIMIT+1, DEFAULT_STDCMD_THROTTLEDELAYMS+1, DEFAULT_STDCMD_THROTTLECPULIMIT+1, DEFAULT_STDCMD_THROTTLEQUEUELIMIT+1));
- }
- void testDirectoryMonitoring()
- {
- VStringBuffer subDirPath("%s%s", basePath.str(), "subdir1");
- Owned<IFile> subDirIFile = createIFile(subDirPath);
- subDirIFile->createDirectory();
- VStringBuffer filePath("%s/%s", subDirPath.str(), "file1");
- class CDelayedFileCreate : implements IThreaded
- {
- CThreaded threaded;
- StringAttr filePath;
- Semaphore doneSem;
- public:
- CDelayedFileCreate(const char *_filePath) : filePath(_filePath), threaded("CDelayedFileCreate")
- {
- threaded.init(this);
- }
- ~CDelayedFileCreate()
- {
- stop();
- }
- void stop()
- {
- doneSem.signal();
- threaded.join();
- }
- // IThreaded impl.
- virtual void threadmain() override
- {
- MilliSleep(1000); // give monitorDirectory a chance to be monitoring
- // create file
- Owned<IFile> iFile = createIFile(filePath);
- CPPUNIT_ASSERT(iFile);
- Owned<IFileIO> iFileIO = iFile->open(IFOcreate);
- CPPUNIT_ASSERT(iFileIO);
- iFileIO.clear();
- doneSem.wait(60 * 1000);
- CPPUNIT_ASSERT(iFile->remove());
- }
- } delayedFileCreate(filePath);
- Owned<IDirectoryDifferenceIterator> iter = subDirIFile->monitorDirectory(nullptr, nullptr, false, false, 2000, 60 * 1000);
- ForEach(*iter)
- {
- StringBuffer fname;
- iter->getName(fname);
- PROGLOG("fname = %s", fname.str());
- }
- delayedFileCreate.stop();
- }
- void testFinish()
- {
- // clearup
- VStringBuffer filePathCopy("%s%s", basePath.str(), "file1copy");
- Owned<IFile> iFile1Copy = createIFile(filePathCopy);
- CPPUNIT_ASSERT(iFile1Copy->remove());
- VStringBuffer subDirPath("%s%s", basePath.str(), "subdir1");
- VStringBuffer subDirFilePath("%s/%s", subDirPath.str(), "file2");
- Owned<IFile> iFile2 = createIFile(subDirFilePath);
- CPPUNIT_ASSERT(iFile2->remove());
- Owned<IFile> subDirIFile = createIFile(subDirPath);
- CPPUNIT_ASSERT(subDirIFile->remove());
- SocketEndpoint ep(serverPort);
- Owned<ISocket> sock = ISocket::connect_timeout(ep, 60 * 1000);
- CPPUNIT_ASSERT(RFEnoerror == stopRemoteServer(sock));
- serverThread.clear();
- }
- };
- CPPUNIT_TEST_SUITE_REGISTRATION( RemoteFileSlowTest );
- CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RemoteFileSlowTest, "RemoteFileSlowTests" );
- #endif // _USE_CPPUNIT
|