repl5_replica.c 140 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  5. * All rights reserved.
  6. *
  7. * License: GPL (version 3 or any later version).
  8. * See LICENSE for details.
  9. * END COPYRIGHT BLOCK **/
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. /* repl5_replica.c */
  14. #include "slapi-plugin.h"
  15. #include "repl5.h"
  16. #include "repl_shared.h"
  17. #include "csnpl.h"
  18. #include "cl5_api.h"
  19. #include "slap.h"
  20. #define RUV_SAVE_INTERVAL (30 * 1000) /* 30 seconds */
  21. #define REPLICA_RDN "cn=replica"
  22. /*
  23. * A replica is a locally-held copy of a portion of the DIT.
  24. */
  25. struct replica
  26. {
  27. Slapi_DN *repl_root; /* top of the replicated are */
  28. char *repl_name; /* unique replica name */
  29. PRBool new_name; /* new name was generated - need to be saved */
  30. ReplicaUpdateDNList updatedn_list; /* list of dns with which a supplier should bind
  31. to update this replica */
  32. Slapi_ValueSet *updatedn_groups; /* set of groups whose memebers are
  33. * allowed to update replica */
  34. ReplicaUpdateDNList groupdn_list; /* exploded listof dns from update group */
  35. PRUint32 updatedn_group_last_check;
  36. int updatedn_group_check_interval;
  37. ReplicaType repl_type; /* is this replica read-only ? */
  38. ReplicaId repl_rid; /* replicaID */
  39. Object *repl_ruv; /* replica update vector */
  40. PRBool repl_ruv_dirty; /* Dirty flag for ruv */
  41. CSNPL *min_csn_pl; /* Pending list for minimal CSN */
  42. void *csn_pl_reg_id; /* registration assignment for csn callbacks */
  43. unsigned long repl_state_flags; /* state flags */
  44. PRUint32 repl_flags; /* persistent, externally visible flags */
  45. PRMonitor *repl_lock; /* protects entire structure */
  46. Slapi_Eq_Context repl_eqcxt_rs; /* context to cancel event that saves ruv */
  47. Slapi_Eq_Context repl_eqcxt_tr; /* context to cancel event that reaps tombstones */
  48. Object *repl_csngen; /* CSN generator for this replica */
  49. PRBool repl_csn_assigned; /* Flag set when new csn is assigned. */
  50. PRUint32 repl_purge_delay; /* When purgeable, CSNs are held on to for this many extra seconds */
  51. PRBool tombstone_reap_stop; /* TRUE when the tombstone reaper should stop */
  52. PRBool tombstone_reap_active; /* TRUE when the tombstone reaper is running */
  53. long tombstone_reap_interval; /* Time in seconds between tombstone reaping */
  54. Slapi_ValueSet *repl_referral; /* A list of administrator provided referral URLs */
  55. PRBool state_update_inprogress; /* replica state is being updated */
  56. PRLock *agmt_lock; /* protects agreement creation, start and stop */
  57. char *locking_purl; /* supplier who has exclusive access */
  58. uint64_t locking_conn; /* The supplier's connection id */
  59. Slapi_Counter *protocol_timeout; /* protocol shutdown timeout */
  60. Slapi_Counter *backoff_min; /* backoff retry minimum */
  61. Slapi_Counter *backoff_max; /* backoff retry maximum */
  62. Slapi_Counter *precise_purging; /* Enable precise tombstone purging */
  63. PRUint64 agmt_count; /* Number of agmts */
  64. Slapi_Counter *release_timeout; /* The amount of time to wait before releasing active replica */
  65. PRUint64 abort_session; /* Abort the current replica session */
  66. };
  67. typedef struct reap_callback_data
  68. {
  69. int rc;
  70. unsigned long num_entries;
  71. unsigned long num_purged_entries;
  72. CSN *purge_csn;
  73. PRBool *tombstone_reap_stop;
  74. } reap_callback_data;
  75. /* Forward declarations of helper functions*/
  76. static Slapi_Entry *_replica_get_config_entry(const Slapi_DN *root, const char **attrs);
  77. static int _replica_check_validity(const Replica *r);
  78. static int _replica_init_from_config(Replica *r, Slapi_Entry *e, char *errortext);
  79. static int _replica_update_entry(Replica *r, Slapi_Entry *e, char *errortext);
  80. static int _replica_configure_ruv(Replica *r, PRBool isLocked);
  81. static char *_replica_get_config_dn(const Slapi_DN *root);
  82. static char *_replica_type_as_string(const Replica *r);
  83. /* DBDB, I think this is probably bogus : */
  84. static int replica_create_ruv_tombstone(Replica *r);
  85. static void assign_csn_callback(const CSN *csn, void *data);
  86. static void abort_csn_callback(const CSN *csn, void *data);
  87. static void eq_cb_reap_tombstones(time_t when, void *arg);
  88. static CSN *_replica_get_purge_csn_nolock(const Replica *r);
  89. static void replica_get_referrals_nolock(const Replica *r, char ***referrals);
  90. static int replica_log_ruv_elements_nolock(const Replica *r);
  91. static void replica_replace_ruv_tombstone(Replica *r);
  92. static void start_agreements_for_replica(Replica *r, PRBool start);
  93. static void _delete_tombstone(const char *tombstone_dn, const char *uniqueid, int ext_op_flags);
  94. static void replica_strip_cleaned_rids(Replica *r);
  95. static void
  96. replica_lock(PRMonitor *lock)
  97. {
  98. PR_EnterMonitor(lock);
  99. }
  100. static void
  101. replica_unlock(PRMonitor *lock)
  102. {
  103. PR_ExitMonitor(lock);
  104. }
  105. /*
  106. * Allocates new replica and reads its state and state of its component from
  107. * various parts of the DIT.
  108. */
  109. Replica *
  110. replica_new(const Slapi_DN *root)
  111. {
  112. Replica *r = NULL;
  113. Slapi_Entry *e = NULL;
  114. char errorbuf[SLAPI_DSE_RETURNTEXT_SIZE];
  115. PR_ASSERT(root);
  116. /* check if there is a replica associated with the tree */
  117. e = _replica_get_config_entry(root, NULL);
  118. if (e) {
  119. errorbuf[0] = '\0';
  120. r = replica_new_from_entry(e, errorbuf,
  121. PR_FALSE /* not a newly added entry */);
  122. if (NULL == r) {
  123. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_new - "
  124. "Unable to configure replica %s: %s\n",
  125. slapi_sdn_get_dn(root), errorbuf);
  126. }
  127. slapi_entry_free(e);
  128. }
  129. return r;
  130. }
  131. /* constructs the replica object from the newly added entry */
  132. Replica *
  133. replica_new_from_entry(Slapi_Entry *e, char *errortext, PRBool is_add_operation)
  134. {
  135. int rc = 0;
  136. Replica *r;
  137. if (e == NULL) {
  138. if (NULL != errortext) {
  139. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "NULL entry");
  140. }
  141. return NULL;
  142. }
  143. r = (Replica *)slapi_ch_calloc(1, sizeof(Replica));
  144. if (!r) {
  145. if (NULL != errortext) {
  146. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "Out of memory");
  147. }
  148. rc = -1;
  149. goto done;
  150. }
  151. if ((r->repl_lock = PR_NewMonitor()) == NULL) {
  152. if (NULL != errortext) {
  153. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to create replica lock");
  154. }
  155. rc = -1;
  156. goto done;
  157. }
  158. if ((r->agmt_lock = PR_NewLock()) == NULL) {
  159. if (NULL != errortext) {
  160. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to create replica lock");
  161. }
  162. rc = -1;
  163. goto done;
  164. }
  165. /* init the slapi_counter/atomic settings */
  166. r->protocol_timeout = slapi_counter_new();
  167. r->release_timeout = slapi_counter_new();
  168. r->backoff_min = slapi_counter_new();
  169. r->backoff_max = slapi_counter_new();
  170. r->precise_purging = slapi_counter_new();
  171. /* read parameters from the replica config entry */
  172. rc = _replica_init_from_config(r, e, errortext);
  173. if (rc != 0) {
  174. goto done;
  175. }
  176. /* configure ruv */
  177. rc = _replica_configure_ruv(r, PR_FALSE);
  178. if (rc != 0) {
  179. goto done;
  180. }
  181. /* If smallest csn exists in RUV for our local replica, it's ok to begin iteration */
  182. PR_ASSERT(object_get_data(r->repl_ruv));
  183. if (is_add_operation) {
  184. /*
  185. * This is called by an ldap add operation.
  186. * Update the entry to contain information generated
  187. * during replica initialization
  188. */
  189. rc = _replica_update_entry(r, e, errortext);
  190. } else {
  191. /*
  192. * Entry is already in dse.ldif - update it on the disk
  193. * (done by the update state event scheduled below)
  194. */
  195. }
  196. if (rc != 0)
  197. goto done;
  198. /* ONREPL - the state update can occur before the entry is added to the DIT.
  199. In that case the updated would fail but nothing bad would happen. The next
  200. scheduled update would save the state */
  201. r->repl_eqcxt_rs = slapi_eq_repeat(replica_update_state, r->repl_name,
  202. slapi_current_utc_time() + START_UPDATE_DELAY, RUV_SAVE_INTERVAL);
  203. if (r->tombstone_reap_interval > 0) {
  204. /*
  205. * Reap Tombstone should be started some time after the plugin started.
  206. * This will allow the server to fully start before consuming resources.
  207. */
  208. r->repl_eqcxt_tr = slapi_eq_repeat(eq_cb_reap_tombstones, r->repl_name,
  209. slapi_current_utc_time() + r->tombstone_reap_interval,
  210. 1000 * r->tombstone_reap_interval);
  211. }
  212. replica_check_for_tasks(r, e);
  213. done:
  214. if (rc != 0 && r) {
  215. replica_destroy((void **)&r);
  216. }
  217. return r;
  218. }
  219. void
  220. replica_flush(Replica *r)
  221. {
  222. PR_ASSERT(NULL != r);
  223. if (NULL != r) {
  224. replica_lock(r->repl_lock);
  225. /* Make sure we dump the CSNGen state */
  226. r->repl_csn_assigned = PR_TRUE;
  227. replica_unlock(r->repl_lock);
  228. /* This function take the Lock Inside */
  229. /* And also write the RUV */
  230. replica_update_state((time_t)0, r->repl_name);
  231. }
  232. }
  233. void
  234. replica_set_csn_assigned(Replica *r)
  235. {
  236. replica_lock(r->repl_lock);
  237. r->repl_csn_assigned = PR_TRUE;
  238. replica_unlock(r->repl_lock);
  239. }
  240. /*
  241. * Deallocate a replica. arg should point to the address of a
  242. * pointer that points to a replica structure.
  243. */
  244. void
  245. replica_destroy(void **arg)
  246. {
  247. Replica *r;
  248. if (arg == NULL)
  249. return;
  250. r = *((Replica **)arg);
  251. PR_ASSERT(r);
  252. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "replica_destroy\n");
  253. /*
  254. * The function will not be called unless the refcnt of its
  255. * wrapper object is 0. Hopefully this refcnt could sync up
  256. * this destruction and the events such as tombstone reap
  257. * and ruv updates.
  258. */
  259. if (r->repl_eqcxt_rs) {
  260. slapi_eq_cancel(r->repl_eqcxt_rs);
  261. r->repl_eqcxt_rs = NULL;
  262. }
  263. if (r->repl_eqcxt_tr) {
  264. slapi_eq_cancel(r->repl_eqcxt_tr);
  265. r->repl_eqcxt_tr = NULL;
  266. }
  267. if (r->repl_root) {
  268. slapi_sdn_free(&r->repl_root);
  269. }
  270. slapi_ch_free_string(&r->locking_purl);
  271. if (r->updatedn_list) {
  272. replica_updatedn_list_free(r->updatedn_list);
  273. r->updatedn_list = NULL;
  274. }
  275. if (r->groupdn_list) {
  276. replica_updatedn_list_free(r->groupdn_list);
  277. r->groupdn_list = NULL;
  278. }
  279. if (r->updatedn_groups) {
  280. slapi_valueset_free(r->updatedn_groups);
  281. }
  282. /* slapi_ch_free accepts NULL pointer */
  283. slapi_ch_free((void **)&r->repl_name);
  284. if (r->repl_lock) {
  285. PR_DestroyMonitor(r->repl_lock);
  286. r->repl_lock = NULL;
  287. }
  288. if (r->agmt_lock) {
  289. PR_DestroyLock(r->agmt_lock);
  290. r->agmt_lock = NULL;
  291. }
  292. if (NULL != r->repl_ruv) {
  293. object_release(r->repl_ruv);
  294. }
  295. if (NULL != r->repl_csngen) {
  296. if (r->csn_pl_reg_id) {
  297. csngen_unregister_callbacks((CSNGen *)object_get_data(r->repl_csngen), r->csn_pl_reg_id);
  298. }
  299. object_release(r->repl_csngen);
  300. }
  301. if (NULL != r->repl_referral) {
  302. slapi_valueset_free(r->repl_referral);
  303. }
  304. if (NULL != r->min_csn_pl) {
  305. csnplFree(&r->min_csn_pl);
  306. ;
  307. }
  308. slapi_counter_destroy(&r->protocol_timeout);
  309. slapi_counter_destroy(&r->release_timeout);
  310. slapi_counter_destroy(&r->backoff_min);
  311. slapi_counter_destroy(&r->backoff_max);
  312. slapi_counter_destroy(&r->precise_purging);
  313. slapi_ch_free((void **)arg);
  314. }
  315. #define KEEP_ALIVE_ATTR "keepalivetimestamp"
  316. #define KEEP_ALIVE_ENTRY "repl keep alive"
  317. #define KEEP_ALIVE_DN_FORMAT "cn=%s %d,%s"
  318. static int
  319. replica_subentry_create(Slapi_DN *repl_root, ReplicaId rid)
  320. {
  321. char *entry_string = NULL;
  322. Slapi_Entry *e = NULL;
  323. Slapi_PBlock *pb = NULL;
  324. int return_value;
  325. int rc = 0;
  326. entry_string = slapi_ch_smprintf("dn: cn=%s %d,%s\nobjectclass: top\nobjectclass: ldapsubentry\nobjectclass: extensibleObject\ncn: %s %d",
  327. KEEP_ALIVE_ENTRY, rid, slapi_sdn_get_dn(repl_root), KEEP_ALIVE_ENTRY, rid);
  328. if (entry_string == NULL) {
  329. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  330. "replica_subentry_create - Failed in slapi_ch_smprintf\n");
  331. rc = -1;
  332. goto done;
  333. }
  334. slapi_log_err(SLAPI_LOG_INFO, repl_plugin_name,
  335. "replica_subentry_create - add %s\n", entry_string);
  336. e = slapi_str2entry(entry_string, 0);
  337. /* create the entry */
  338. pb = slapi_pblock_new();
  339. slapi_add_entry_internal_set_pb(pb, e, NULL, /* controls */
  340. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0 /* flags */);
  341. slapi_add_internal_pb(pb);
  342. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
  343. if (return_value != LDAP_SUCCESS &&
  344. return_value != LDAP_ALREADY_EXISTS &&
  345. return_value != LDAP_REFERRAL /* CONSUMER */) {
  346. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_subentry_create - Unable to "
  347. "create replication keep alive entry %s: error %d - %s\n",
  348. slapi_entry_get_dn_const(e),
  349. return_value, ldap_err2string(return_value));
  350. rc = -1;
  351. goto done;
  352. }
  353. done:
  354. slapi_pblock_destroy(pb);
  355. slapi_ch_free_string(&entry_string);
  356. return rc;
  357. }
  358. int
  359. replica_subentry_check(Slapi_DN *repl_root, ReplicaId rid)
  360. {
  361. Slapi_PBlock *pb;
  362. char *filter = NULL;
  363. Slapi_Entry **entries = NULL;
  364. int res;
  365. int rc = 0;
  366. pb = slapi_pblock_new();
  367. filter = slapi_ch_smprintf("(&(objectclass=ldapsubentry)(cn=%s %d))", KEEP_ALIVE_ENTRY, rid);
  368. slapi_search_internal_set_pb(pb, slapi_sdn_get_dn(repl_root), LDAP_SCOPE_ONELEVEL,
  369. filter, NULL, 0, NULL, NULL,
  370. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  371. slapi_search_internal_pb(pb);
  372. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  373. if (res == LDAP_SUCCESS) {
  374. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  375. if (entries && (entries[0] == NULL)) {
  376. slapi_log_err(SLAPI_LOG_NOTICE, repl_plugin_name,
  377. "replica_subentry_check - Need to create replication keep alive entry <cn=%s %d,%s>\n", KEEP_ALIVE_ENTRY, rid, slapi_sdn_get_dn(repl_root));
  378. rc = replica_subentry_create(repl_root, rid);
  379. } else {
  380. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  381. "replica_subentry_check - replication keep alive entry <cn=%s %d,%s> already exists\n", KEEP_ALIVE_ENTRY, rid, slapi_sdn_get_dn(repl_root));
  382. rc = 0;
  383. }
  384. } else {
  385. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  386. "replica_subentry_check - Error accessing replication keep alive entry <cn=%s %d,%s> res=%d\n",
  387. KEEP_ALIVE_ENTRY, rid, slapi_sdn_get_dn(repl_root), res);
  388. /* The status of the entry is not clear, do not attempt to create it */
  389. rc = 1;
  390. }
  391. slapi_free_search_results_internal(pb);
  392. slapi_pblock_destroy(pb);
  393. slapi_ch_free_string(&filter);
  394. return rc;
  395. }
  396. int
  397. replica_subentry_update(Slapi_DN *repl_root, ReplicaId rid)
  398. {
  399. int ldrc;
  400. int rc = LDAP_SUCCESS; /* Optimistic default */
  401. LDAPMod *mods[2];
  402. LDAPMod mod;
  403. struct berval *vals[2];
  404. char buf[SLAPI_TIMESTAMP_BUFSIZE];
  405. struct berval val;
  406. Slapi_PBlock *modpb = NULL;
  407. char *dn;
  408. replica_subentry_check(repl_root, rid);
  409. slapi_timestamp_utc_hr(buf, SLAPI_TIMESTAMP_BUFSIZE);
  410. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "subentry_update called at %s\n", buf);
  411. val.bv_val = buf;
  412. val.bv_len = strlen(val.bv_val);
  413. vals[0] = &val;
  414. vals[1] = NULL;
  415. mod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  416. mod.mod_type = KEEP_ALIVE_ATTR;
  417. mod.mod_bvalues = vals;
  418. mods[0] = &mod;
  419. mods[1] = NULL;
  420. modpb = slapi_pblock_new();
  421. dn = slapi_ch_smprintf(KEEP_ALIVE_DN_FORMAT, KEEP_ALIVE_ENTRY, rid, slapi_sdn_get_dn(repl_root));
  422. slapi_modify_internal_set_pb(modpb, dn, mods, NULL, NULL,
  423. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  424. slapi_modify_internal_pb(modpb);
  425. slapi_pblock_get(modpb, SLAPI_PLUGIN_INTOP_RESULT, &ldrc);
  426. if (ldrc != LDAP_SUCCESS) {
  427. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  428. "Failure (%d) to update replication keep alive entry \"%s: %s\"\n", ldrc, KEEP_ALIVE_ATTR, buf);
  429. rc = ldrc;
  430. } else {
  431. slapi_log_err(SLAPI_LOG_PLUGIN, repl_plugin_name,
  432. "Successful update of replication keep alive entry \"%s: %s\"\n", KEEP_ALIVE_ATTR, buf);
  433. }
  434. slapi_pblock_destroy(modpb);
  435. slapi_ch_free_string(&dn);
  436. return rc;
  437. }
  438. /*
  439. * Attempt to obtain exclusive access to replica (advisory only)
  440. *
  441. * Returns PR_TRUE if exclusive access was granted,
  442. * PR_FALSE otherwise
  443. * The parameter isInc tells whether or not the replica is being
  444. * locked for an incremental update session - if the replica is
  445. * successfully locked, this value is used - if the replica is already
  446. * in use, this value will be set to TRUE or FALSE, depending on what
  447. * type of update session has the replica in use currently
  448. * locking_purl is the supplier who is attempting to acquire access
  449. * current_purl is the supplier who already has access, if any
  450. */
  451. PRBool
  452. replica_get_exclusive_access(Replica *r, PRBool *isInc, PRUint64 connid, int opid, const char *locking_purl, char **current_purl)
  453. {
  454. PRBool rval = PR_TRUE;
  455. PR_ASSERT(r);
  456. replica_lock(r->repl_lock);
  457. if (r->repl_state_flags & REPLICA_IN_USE) {
  458. if (isInc)
  459. *isInc = (r->repl_state_flags & REPLICA_INCREMENTAL_IN_PROGRESS);
  460. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  461. "replica_get_exclusive_access - "
  462. "conn=%" PRIu64 " op=%d repl=\"%s\": "
  463. "Replica in use locking_purl=%s\n",
  464. connid, opid,
  465. slapi_sdn_get_dn(r->repl_root),
  466. r->locking_purl ? r->locking_purl : "unknown");
  467. rval = PR_FALSE;
  468. if (!(r->repl_state_flags & REPLICA_TOTAL_IN_PROGRESS)) {
  469. /* inc update */
  470. if (r->locking_purl && r->locking_conn == connid) {
  471. /* This is the same supplier connection, reset the replica
  472. * purl, and return success */
  473. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  474. "replica_get_exclusive_access - "
  475. "This is a second acquire attempt from the same replica connection "
  476. " - return success instead of busy\n");
  477. slapi_ch_free_string(&r->locking_purl);
  478. r->locking_purl = slapi_ch_strdup(locking_purl);
  479. rval = PR_TRUE;
  480. goto done;
  481. }
  482. if (replica_get_release_timeout(r)) {
  483. /*
  484. * Abort the current session so other replicas can acquire
  485. * this server.
  486. */
  487. r->abort_session = ABORT_SESSION;
  488. }
  489. }
  490. if (current_purl) {
  491. *current_purl = slapi_ch_strdup(r->locking_purl);
  492. }
  493. } else {
  494. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  495. "replica_get_exclusive_access - "
  496. "conn=%" PRIu64 " op=%d repl=\"%s\": Acquired replica\n",
  497. connid, opid,
  498. slapi_sdn_get_dn(r->repl_root));
  499. r->repl_state_flags |= REPLICA_IN_USE;
  500. r->abort_session = SESSION_ACQUIRED;
  501. if (isInc && *isInc) {
  502. r->repl_state_flags |= REPLICA_INCREMENTAL_IN_PROGRESS;
  503. } else {
  504. /*
  505. * If connid or opid != 0, it's a total update.
  506. * Both set to 0 means we're disabling replication
  507. */
  508. if (connid || opid) {
  509. r->repl_state_flags |= REPLICA_TOTAL_IN_PROGRESS;
  510. }
  511. }
  512. slapi_ch_free_string(&r->locking_purl);
  513. r->locking_purl = slapi_ch_strdup(locking_purl);
  514. r->locking_conn = connid;
  515. }
  516. done:
  517. replica_unlock(r->repl_lock);
  518. return rval;
  519. }
  520. /*
  521. * Relinquish exclusive access to the replica
  522. */
  523. void
  524. replica_relinquish_exclusive_access(Replica *r, PRUint64 connid, int opid)
  525. {
  526. PRBool isInc;
  527. PR_ASSERT(r);
  528. replica_lock(r->repl_lock);
  529. isInc = (r->repl_state_flags & REPLICA_INCREMENTAL_IN_PROGRESS);
  530. /* check to see if the replica is in use and log a warning if not */
  531. if (!(r->repl_state_flags & REPLICA_IN_USE)) {
  532. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  533. "replica_relinquish_exclusive_access - "
  534. "conn=%" PRIu64 " op=%d repl=\"%s\": "
  535. "Replica not in use\n",
  536. connid, opid, slapi_sdn_get_dn(r->repl_root));
  537. } else {
  538. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  539. "replica_relinquish_exclusive_access - "
  540. "conn=%" PRIu64 " op=%d repl=\"%s\": "
  541. "Released replica held by locking_purl=%s\n",
  542. connid, opid,
  543. slapi_sdn_get_dn(r->repl_root), r->locking_purl);
  544. slapi_ch_free_string(&r->locking_purl);
  545. r->repl_state_flags &= ~(REPLICA_IN_USE);
  546. if (isInc)
  547. r->repl_state_flags &= ~(REPLICA_INCREMENTAL_IN_PROGRESS);
  548. else
  549. r->repl_state_flags &= ~(REPLICA_TOTAL_IN_PROGRESS);
  550. }
  551. replica_unlock(r->repl_lock);
  552. }
  553. /*
  554. * Returns root of the replicated area
  555. */
  556. PRBool
  557. replica_get_tombstone_reap_active(const Replica *r)
  558. {
  559. PR_ASSERT(r);
  560. return (r->tombstone_reap_active);
  561. }
  562. /*
  563. * Returns root of the replicated area
  564. */
  565. const Slapi_DN *
  566. replica_get_root(const Replica *r) /* ONREPL - should we return copy instead? */
  567. {
  568. PR_ASSERT(r);
  569. /* replica root never changes so we don't have to lock */
  570. return (r->repl_root);
  571. }
  572. /*
  573. * Returns normalized dn of the root of the replicated area
  574. */
  575. const char *
  576. replica_get_name(const Replica *r) /* ONREPL - should we return copy instead? */
  577. {
  578. PR_ASSERT(r);
  579. /* replica name never changes so we don't have to lock */
  580. return (r->repl_name);
  581. }
  582. /*
  583. * Returns locking_conn of this replica
  584. */
  585. uint64_t
  586. replica_get_locking_conn(const Replica *r)
  587. {
  588. uint64_t connid;
  589. replica_lock(r->repl_lock);
  590. connid = r->locking_conn;
  591. replica_unlock(r->repl_lock);
  592. return connid;
  593. }
  594. /*
  595. * Returns replicaid of this replica
  596. */
  597. ReplicaId
  598. replica_get_rid(const Replica *r)
  599. {
  600. ReplicaId rid;
  601. PR_ASSERT(r);
  602. replica_lock(r->repl_lock);
  603. rid = r->repl_rid;
  604. replica_unlock(r->repl_lock);
  605. return rid;
  606. }
  607. /*
  608. * Sets replicaid of this replica - should only be used when also changing the type
  609. */
  610. void
  611. replica_set_rid(Replica *r, ReplicaId rid)
  612. {
  613. PR_ASSERT(r);
  614. replica_lock(r->repl_lock);
  615. r->repl_rid = rid;
  616. replica_unlock(r->repl_lock);
  617. }
  618. /* Returns true if replica was initialized through ORC or import;
  619. * otherwise, false. An uninitialized replica should return
  620. * LDAP_UNWILLING_TO_PERFORM to all client requests
  621. */
  622. PRBool
  623. replica_is_initialized(const Replica *r)
  624. {
  625. PR_ASSERT(r);
  626. return (r->repl_ruv != NULL);
  627. }
  628. /*
  629. * Returns refcounted object that contains RUV. The caller should release the
  630. * object once it is no longer used. To release, call object_release
  631. */
  632. Object *
  633. replica_get_ruv(const Replica *r)
  634. {
  635. Object *ruv = NULL;
  636. PR_ASSERT(r);
  637. replica_lock(r->repl_lock);
  638. PR_ASSERT(r->repl_ruv);
  639. object_acquire(r->repl_ruv);
  640. ruv = r->repl_ruv;
  641. replica_unlock(r->repl_lock);
  642. return ruv;
  643. }
  644. /*
  645. * Sets RUV vector. This function should be called during replica
  646. * (re)initialization. During normal operation, the RUV is read from
  647. * the root of the replicated in the replica_new call
  648. */
  649. void
  650. replica_set_ruv(Replica *r, RUV *ruv)
  651. {
  652. PR_ASSERT(r && ruv);
  653. replica_lock(r->repl_lock);
  654. if (NULL != r->repl_ruv) {
  655. object_release(r->repl_ruv);
  656. }
  657. /* if the local replica is not in the RUV and it is writable - add it
  658. and reinitialize min_csn pending list */
  659. if (r->repl_type == REPLICA_TYPE_UPDATABLE) {
  660. CSN *csn = NULL;
  661. if (r->min_csn_pl)
  662. csnplFree(&r->min_csn_pl);
  663. if (ruv_contains_replica(ruv, r->repl_rid)) {
  664. ruv_get_smallest_csn_for_replica(ruv, r->repl_rid, &csn);
  665. if (csn)
  666. csn_free(&csn);
  667. else
  668. r->min_csn_pl = csnplNew();
  669. /* We need to make sure the local ruv element is the 1st. */
  670. ruv_move_local_supplier_to_first(ruv, r->repl_rid);
  671. } else {
  672. r->min_csn_pl = csnplNew();
  673. /* To be sure that the local is in first */
  674. ruv_add_index_replica(ruv, r->repl_rid, multimaster_get_local_purl(), 1);
  675. }
  676. }
  677. r->repl_ruv = object_new((void *)ruv, (FNFree)ruv_destroy);
  678. r->repl_ruv_dirty = PR_TRUE;
  679. replica_unlock(r->repl_lock);
  680. }
  681. /*
  682. * Update one particular CSN in an RUV. This is meant to be called
  683. * whenever (a) the server has processed a client operation and
  684. * needs to update its CSN, or (b) the server is completing an
  685. * inbound replication session operation, and needs to update its
  686. * local RUV.
  687. */
  688. int
  689. replica_update_ruv(Replica *r, const CSN *updated_csn, const char *replica_purl)
  690. {
  691. char csn_str[CSN_STRSIZE];
  692. int rc = RUV_SUCCESS;
  693. PR_ASSERT(NULL != r);
  694. PR_ASSERT(NULL != updated_csn);
  695. #ifdef DEBUG
  696. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  697. "replica_update_ruv: csn %s\n",
  698. csn_as_string(updated_csn, PR_FALSE, csn_str)); /* XXXggood remove debugging */
  699. #endif
  700. if (NULL == r) {
  701. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_update_ruv - Replica "
  702. "is NULL\n");
  703. rc = RUV_BAD_DATA;
  704. } else if (NULL == updated_csn) {
  705. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_update_ruv - csn "
  706. "is NULL when updating replica %s\n",
  707. slapi_sdn_get_dn(r->repl_root));
  708. rc = RUV_BAD_DATA;
  709. } else {
  710. RUV *ruv;
  711. replica_lock(r->repl_lock);
  712. if (r->repl_ruv != NULL) {
  713. ruv = object_get_data(r->repl_ruv);
  714. if (NULL != ruv) {
  715. ReplicaId rid = csn_get_replicaid(updated_csn);
  716. if (rid == r->repl_rid) {
  717. if (NULL != r->min_csn_pl) {
  718. CSN *min_csn;
  719. PRBool committed;
  720. (void)csnplCommit(r->min_csn_pl, updated_csn);
  721. min_csn = csnplGetMinCSN(r->min_csn_pl, &committed);
  722. if (NULL != min_csn) {
  723. if (committed) {
  724. ruv_set_min_csn(ruv, min_csn, replica_purl);
  725. csnplFree(&r->min_csn_pl);
  726. }
  727. csn_free(&min_csn);
  728. }
  729. }
  730. }
  731. /* Update max csn for local and remote replicas */
  732. rc = ruv_update_ruv(ruv, updated_csn, replica_purl, r, r->repl_rid);
  733. if (RUV_COVERS_CSN == rc) {
  734. slapi_log_err(SLAPI_LOG_REPL,
  735. repl_plugin_name, "replica_update_ruv - RUV "
  736. "for replica %s already covers max_csn = %s\n",
  737. slapi_sdn_get_dn(r->repl_root),
  738. csn_as_string(updated_csn, PR_FALSE, csn_str));
  739. /* RUV is not dirty - no write needed */
  740. } else if (RUV_SUCCESS != rc) {
  741. slapi_log_err(SLAPI_LOG_ERR,
  742. repl_plugin_name, "replica_update_ruv - Unable "
  743. "to update RUV for replica %s, csn = %s\n",
  744. slapi_sdn_get_dn(r->repl_root),
  745. csn_as_string(updated_csn, PR_FALSE, csn_str));
  746. } else {
  747. /* RUV updated - mark as dirty */
  748. r->repl_ruv_dirty = PR_TRUE;
  749. }
  750. } else {
  751. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  752. "replica_update_ruv - Unable to get RUV object for replica "
  753. "%s\n",
  754. slapi_sdn_get_dn(r->repl_root));
  755. rc = RUV_NOTFOUND;
  756. }
  757. } else {
  758. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_update_ruv - "
  759. "Unable to initialize RUV for replica %s\n",
  760. slapi_sdn_get_dn(r->repl_root));
  761. rc = RUV_NOTFOUND;
  762. }
  763. replica_unlock(r->repl_lock);
  764. }
  765. return rc;
  766. }
  767. /*
  768. * Returns refcounted object that contains csn generator. The caller should release the
  769. * object once it is no longer used. To release, call object_release
  770. */
  771. Object *
  772. replica_get_csngen(const Replica *r)
  773. {
  774. Object *csngen;
  775. PR_ASSERT(r);
  776. replica_lock(r->repl_lock);
  777. object_acquire(r->repl_csngen);
  778. csngen = r->repl_csngen;
  779. replica_unlock(r->repl_lock);
  780. return csngen;
  781. }
  782. /*
  783. * Returns the replica type.
  784. */
  785. ReplicaType
  786. replica_get_type(const Replica *r)
  787. {
  788. PR_ASSERT(r);
  789. return r->repl_type;
  790. }
  791. PRUint64
  792. replica_get_protocol_timeout(Replica *r)
  793. {
  794. if (r) {
  795. return slapi_counter_get_value(r->protocol_timeout);
  796. } else {
  797. return 0;
  798. }
  799. }
  800. PRUint64
  801. replica_get_release_timeout(Replica *r)
  802. {
  803. if (r) {
  804. return slapi_counter_get_value(r->release_timeout);
  805. } else {
  806. return 0;
  807. }
  808. }
  809. void
  810. replica_set_release_timeout(Replica *r, PRUint64 limit)
  811. {
  812. if (r) {
  813. slapi_counter_set_value(r->release_timeout, limit);
  814. }
  815. }
  816. void
  817. replica_set_protocol_timeout(Replica *r, PRUint64 timeout)
  818. {
  819. if (r) {
  820. slapi_counter_set_value(r->protocol_timeout, timeout);
  821. }
  822. }
  823. void
  824. replica_set_groupdn_checkinterval(Replica *r, int interval)
  825. {
  826. if (r) {
  827. r->updatedn_group_check_interval = interval;
  828. }
  829. }
  830. /*
  831. * Sets the replica type.
  832. */
  833. void
  834. replica_set_type(Replica *r, ReplicaType type)
  835. {
  836. PR_ASSERT(r);
  837. replica_lock(r->repl_lock);
  838. r->repl_type = type;
  839. replica_unlock(r->repl_lock);
  840. }
  841. static PRBool
  842. valuesets_equal(Slapi_ValueSet *new_dn_groups, Slapi_ValueSet *old_dn_groups)
  843. {
  844. Slapi_Attr *attr = NULL;
  845. Slapi_Value *val = NULL;
  846. int idx = 0;
  847. PRBool rc = PR_TRUE;
  848. if (new_dn_groups == NULL) {
  849. if (old_dn_groups == NULL)
  850. return PR_TRUE;
  851. else
  852. return PR_FALSE;
  853. }
  854. if (old_dn_groups == NULL) {
  855. return PR_FALSE;
  856. }
  857. /* if there is not the same number of value, no need to check the value themselves */
  858. if (new_dn_groups->num != old_dn_groups->num) {
  859. return PR_FALSE;
  860. }
  861. attr = slapi_attr_new();
  862. slapi_attr_init(attr, attr_replicaBindDnGroup);
  863. /* Check that all values in old_dn_groups also exist in new_dn_groups */
  864. for (idx = slapi_valueset_first_value(old_dn_groups, &val);
  865. val && (idx != -1);
  866. idx = slapi_valueset_next_value(old_dn_groups, idx, &val)) {
  867. if (slapi_valueset_find(attr, new_dn_groups, val) == NULL) {
  868. rc = PR_FALSE;
  869. break;
  870. }
  871. }
  872. slapi_attr_free(&attr);
  873. return rc;
  874. }
  875. /*
  876. * Returns true if sdn is the same as updatedn and false otherwise
  877. */
  878. PRBool
  879. replica_is_updatedn(Replica *r, const Slapi_DN *sdn)
  880. {
  881. PRBool result = PR_FALSE;
  882. PR_ASSERT(r);
  883. replica_lock(r->repl_lock);
  884. if ((r->updatedn_list == NULL) && (r->groupdn_list == NULL)) {
  885. if (sdn == NULL) {
  886. result = PR_TRUE;
  887. } else {
  888. result = PR_FALSE;
  889. }
  890. replica_unlock(r->repl_lock);
  891. return result;
  892. }
  893. if (r->updatedn_list) {
  894. result = replica_updatedn_list_ismember(r->updatedn_list, sdn);
  895. if (result == PR_TRUE) {
  896. /* sdn is present in the updatedn_list */
  897. replica_unlock(r->repl_lock);
  898. return result;
  899. }
  900. }
  901. if (r->groupdn_list) {
  902. /* check and rebuild groupdns */
  903. if (r->updatedn_group_check_interval > -1) {
  904. time_t now = slapi_current_utc_time();
  905. if (now - r->updatedn_group_last_check > r->updatedn_group_check_interval) {
  906. Slapi_ValueSet *updatedn_groups_copy = NULL;
  907. ReplicaUpdateDNList groupdn_list = replica_updatedn_list_new(NULL);
  908. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "Authorized replication managers is resync (%ld)\n", now);
  909. updatedn_groups_copy = slapi_valueset_new();
  910. slapi_valueset_set_valueset(updatedn_groups_copy, r->updatedn_groups);
  911. r->updatedn_group_last_check = now; /* Just to be sure no one will try to reload */
  912. /* It can do internal searches, to avoid deadlock release the replica lock
  913. * as we are working on local variables
  914. */
  915. replica_unlock(r->repl_lock);
  916. replica_updatedn_list_group_replace(groupdn_list, updatedn_groups_copy);
  917. replica_lock(r->repl_lock);
  918. if (valuesets_equal(r->updatedn_groups, updatedn_groups_copy)) {
  919. /* the updatedn_groups has not been updated while we release the replica
  920. * this is fine to apply the groupdn_list
  921. */
  922. replica_updatedn_list_delete(r->groupdn_list, NULL);
  923. replica_updatedn_list_free(r->groupdn_list);
  924. r->groupdn_list = groupdn_list;
  925. } else {
  926. /* the unpdatedn_groups has been updated while we released the replica
  927. * groupdn_list in the replica is up to date. Do not replace it
  928. */
  929. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "Authorized replication managers (%s) was updated during a refresh\n", attr_replicaBindDnGroup);
  930. replica_updatedn_list_delete(groupdn_list, NULL);
  931. replica_updatedn_list_free(groupdn_list);
  932. }
  933. slapi_valueset_free(updatedn_groups_copy);
  934. }
  935. }
  936. result = replica_updatedn_list_ismember(r->groupdn_list, sdn);
  937. }
  938. replica_unlock(r->repl_lock);
  939. return result;
  940. }
  941. /*
  942. * Sets updatedn list for this replica
  943. */
  944. void
  945. replica_set_updatedn(Replica *r, const Slapi_ValueSet *vs, int mod_op)
  946. {
  947. PR_ASSERT(r);
  948. replica_lock(r->repl_lock);
  949. if (!r->updatedn_list)
  950. r->updatedn_list = replica_updatedn_list_new(NULL);
  951. if (SLAPI_IS_MOD_DELETE(mod_op) || vs == NULL ||
  952. (0 == slapi_valueset_count(vs))) /* null value also causes list deletion */
  953. replica_updatedn_list_delete(r->updatedn_list, vs);
  954. else if (SLAPI_IS_MOD_REPLACE(mod_op))
  955. replica_updatedn_list_replace(r->updatedn_list, vs);
  956. else if (SLAPI_IS_MOD_ADD(mod_op))
  957. replica_updatedn_list_add(r->updatedn_list, vs);
  958. replica_unlock(r->repl_lock);
  959. }
  960. /*
  961. * Sets updatedn list for this replica
  962. */
  963. void
  964. replica_set_groupdn(Replica *r, const Slapi_ValueSet *vs, int mod_op)
  965. {
  966. PR_ASSERT(r);
  967. replica_lock(r->repl_lock);
  968. if (!r->groupdn_list)
  969. r->groupdn_list = replica_updatedn_list_new(NULL);
  970. if (!r->updatedn_groups)
  971. r->updatedn_groups = slapi_valueset_new();
  972. if (SLAPI_IS_MOD_DELETE(mod_op) || vs == NULL ||
  973. (0 == slapi_valueset_count(vs))) {
  974. /* null value also causes list deletion */
  975. slapi_valueset_free(r->updatedn_groups);
  976. r->updatedn_groups = NULL;
  977. replica_updatedn_list_delete(r->groupdn_list, vs);
  978. } else if (SLAPI_IS_MOD_REPLACE(mod_op)) {
  979. if (r->updatedn_groups) {
  980. slapi_valueset_done(r->updatedn_groups);
  981. } else {
  982. r->updatedn_groups = slapi_valueset_new();
  983. }
  984. slapi_valueset_set_valueset(r->updatedn_groups, vs);
  985. replica_updatedn_list_group_replace(r->groupdn_list, vs);
  986. } else if (SLAPI_IS_MOD_ADD(mod_op)) {
  987. if (!r->updatedn_groups) {
  988. r->updatedn_groups = slapi_valueset_new();
  989. }
  990. slapi_valueset_join_attr_valueset(NULL, r->updatedn_groups, vs);
  991. replica_updatedn_list_add_ext(r->groupdn_list, vs, 1);
  992. }
  993. replica_unlock(r->repl_lock);
  994. }
  995. void
  996. replica_reset_csn_pl(Replica *r)
  997. {
  998. replica_lock(r->repl_lock);
  999. if (NULL != r->min_csn_pl) {
  1000. csnplFree(&r->min_csn_pl);
  1001. }
  1002. r->min_csn_pl = csnplNew();
  1003. replica_unlock(r->repl_lock);
  1004. }
  1005. /* gets current replica generation for this replica */
  1006. char *
  1007. replica_get_generation(const Replica *r)
  1008. {
  1009. int rc = 0;
  1010. char *gen = NULL;
  1011. if (r && r->repl_ruv) {
  1012. replica_lock(r->repl_lock);
  1013. if (rc == 0)
  1014. gen = ruv_get_replica_generation((RUV *)object_get_data(r->repl_ruv));
  1015. replica_unlock(r->repl_lock);
  1016. }
  1017. return gen;
  1018. }
  1019. PRBool
  1020. replica_is_flag_set(const Replica *r, PRUint32 flag)
  1021. {
  1022. if (r)
  1023. return (r->repl_flags & flag);
  1024. else
  1025. return PR_FALSE;
  1026. }
  1027. void
  1028. replica_set_flag(Replica *r, PRUint32 flag, PRBool clear)
  1029. {
  1030. if (r == NULL)
  1031. return;
  1032. replica_lock(r->repl_lock);
  1033. if (clear) {
  1034. r->repl_flags &= ~flag;
  1035. } else {
  1036. r->repl_flags |= flag;
  1037. }
  1038. replica_unlock(r->repl_lock);
  1039. }
  1040. void
  1041. replica_replace_flags(Replica *r, PRUint32 flags)
  1042. {
  1043. if (r) {
  1044. replica_lock(r->repl_lock);
  1045. r->repl_flags = flags;
  1046. replica_unlock(r->repl_lock);
  1047. }
  1048. }
  1049. void
  1050. replica_get_referrals(const Replica *r, char ***referrals)
  1051. {
  1052. replica_lock(r->repl_lock);
  1053. replica_get_referrals_nolock(r, referrals);
  1054. replica_unlock(r->repl_lock);
  1055. }
  1056. void
  1057. replica_set_referrals(Replica *r, const Slapi_ValueSet *vs)
  1058. {
  1059. int ii = 0;
  1060. Slapi_Value *vv = NULL;
  1061. if (r->repl_referral == NULL) {
  1062. r->repl_referral = slapi_valueset_new();
  1063. } else {
  1064. slapi_valueset_done(r->repl_referral);
  1065. }
  1066. slapi_valueset_set_valueset(r->repl_referral, vs);
  1067. /* make sure the DN is included in the referral LDAP URL */
  1068. if (r->repl_referral) {
  1069. Slapi_ValueSet *newvs = slapi_valueset_new();
  1070. const char *repl_root = slapi_sdn_get_dn(r->repl_root);
  1071. ii = slapi_valueset_first_value(r->repl_referral, &vv);
  1072. while (vv) {
  1073. const char *ref = slapi_value_get_string(vv);
  1074. LDAPURLDesc *lud = NULL;
  1075. (void)slapi_ldap_url_parse(ref, &lud, 0, NULL);
  1076. /* see if the dn is already in the referral URL */
  1077. if (!lud || !lud->lud_dn) {
  1078. /* add the dn */
  1079. Slapi_Value *newval = NULL;
  1080. int len = strlen(ref);
  1081. char *tmpref = NULL;
  1082. int need_slash = 0;
  1083. if (ref[len - 1] != '/') {
  1084. need_slash = 1;
  1085. }
  1086. tmpref = slapi_ch_smprintf("%s%s%s", ref, (need_slash ? "/" : ""),
  1087. repl_root);
  1088. newval = slapi_value_new_string(tmpref);
  1089. slapi_ch_free_string(&tmpref); /* sv_new_string makes a copy */
  1090. slapi_valueset_add_value(newvs, newval);
  1091. slapi_value_free(&newval); /* s_vs_add_value makes a copy */
  1092. }
  1093. if (lud)
  1094. ldap_free_urldesc(lud);
  1095. ii = slapi_valueset_next_value(r->repl_referral, ii, &vv);
  1096. }
  1097. if (slapi_valueset_count(newvs) > 0) {
  1098. slapi_valueset_done(r->repl_referral);
  1099. slapi_valueset_set_valueset(r->repl_referral, newvs);
  1100. }
  1101. slapi_valueset_free(newvs); /* s_vs_set_vs makes a copy */
  1102. }
  1103. }
  1104. int
  1105. replica_update_csngen_state_ext(Replica *r, const RUV *ruv, const CSN *extracsn)
  1106. {
  1107. int rc = 0;
  1108. CSNGen *gen;
  1109. CSN *csn = NULL;
  1110. PR_ASSERT(r && ruv);
  1111. rc = ruv_get_max_csn(ruv, &csn);
  1112. if (rc != RUV_SUCCESS) {
  1113. return -1;
  1114. }
  1115. if ((csn == NULL) && (extracsn == NULL)) /* ruv contains no csn and no extra - we are done */
  1116. {
  1117. return 0;
  1118. }
  1119. if (csn_compare(extracsn, csn) > 0) /* extracsn > csn */
  1120. {
  1121. csn_free(&csn); /* free */
  1122. csn = (CSN *)extracsn; /* use this csn to do the update */
  1123. }
  1124. replica_lock(r->repl_lock);
  1125. gen = (CSNGen *)object_get_data(r->repl_csngen);
  1126. PR_ASSERT(gen);
  1127. rc = csngen_adjust_time(gen, csn);
  1128. /* rc will be either CSN_SUCCESS (0) or clock skew */
  1129. /* done: */
  1130. replica_unlock(r->repl_lock);
  1131. if (csn != extracsn) /* do not free the given csn */
  1132. {
  1133. csn_free(&csn);
  1134. }
  1135. return rc;
  1136. }
  1137. int
  1138. replica_update_csngen_state(Replica *r, const RUV *ruv)
  1139. {
  1140. return replica_update_csngen_state_ext(r, ruv, NULL);
  1141. }
  1142. /*
  1143. * dumps replica state for debugging purpose
  1144. */
  1145. void
  1146. replica_dump(Replica *r)
  1147. {
  1148. char *updatedn_list = NULL;
  1149. PR_ASSERT(r);
  1150. replica_lock(r->repl_lock);
  1151. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "Replica state:\n");
  1152. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "\treplica root: %s\n",
  1153. slapi_sdn_get_ndn(r->repl_root));
  1154. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "\treplica type: %s\n",
  1155. _replica_type_as_string(r));
  1156. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "\treplica id: %d\n", r->repl_rid);
  1157. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "\tflags: %d\n", r->repl_flags);
  1158. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "\tstate flags: %lu\n", r->repl_state_flags);
  1159. if (r->updatedn_list)
  1160. updatedn_list = replica_updatedn_list_to_string(r->updatedn_list, "\n\t\t");
  1161. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "\tupdate dn: %s\n",
  1162. updatedn_list ? updatedn_list : "not configured");
  1163. slapi_ch_free_string(&updatedn_list);
  1164. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "\truv: %s configured and is %sdirty\n",
  1165. r->repl_ruv ? "" : "not", r->repl_ruv_dirty ? "" : "not ");
  1166. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "\tCSN generator: %s configured\n",
  1167. r->repl_csngen ? "" : "not");
  1168. /* JCMREPL - Dump Referrals */
  1169. replica_unlock(r->repl_lock);
  1170. }
  1171. /*
  1172. * Return the CSN of the purge point. Any CSNs smaller than the
  1173. * purge point can be safely removed from entries within this
  1174. * this replica. Returns an allocated CSN that must be freed by
  1175. * the caller, or NULL if purging is disabled.
  1176. */
  1177. CSN *
  1178. replica_get_purge_csn(const Replica *r)
  1179. {
  1180. CSN *csn;
  1181. replica_lock(r->repl_lock);
  1182. csn = _replica_get_purge_csn_nolock(r);
  1183. replica_unlock(r->repl_lock);
  1184. return csn;
  1185. }
  1186. /*
  1187. * This function logs a dummy entry for the smallest csn in the RUV.
  1188. * This is necessary because, to get the next change, we need to position
  1189. * changelog on the previous change. So this function insures that we always have one.
  1190. */
  1191. /* ONREPL we will need to change this function to log all the
  1192. * ruv elements not just the smallest when changelog iteration
  1193. * algoritm changes to iterate replica by replica
  1194. */
  1195. int
  1196. replica_log_ruv_elements(const Replica *r)
  1197. {
  1198. int rc = 0;
  1199. PR_ASSERT(r);
  1200. replica_lock(r->repl_lock);
  1201. rc = replica_log_ruv_elements_nolock(r);
  1202. replica_unlock(r->repl_lock);
  1203. return rc;
  1204. }
  1205. void
  1206. consumer5_set_mapping_tree_state_for_replica(const Replica *r, RUV *supplierRuv)
  1207. {
  1208. const Slapi_DN *repl_root_sdn = replica_get_root(r);
  1209. char **ruv_referrals = NULL;
  1210. char **replica_referrals = NULL;
  1211. RUV *ruv;
  1212. int state_backend = -1;
  1213. const char *mtn_state = NULL;
  1214. replica_lock(r->repl_lock);
  1215. if (supplierRuv == NULL) {
  1216. ruv = (RUV *)object_get_data(r->repl_ruv);
  1217. PR_ASSERT(ruv);
  1218. ruv_referrals = ruv_get_referrals(ruv); /* ruv_referrals has to be free'd */
  1219. } else {
  1220. ruv_referrals = ruv_get_referrals(supplierRuv);
  1221. }
  1222. replica_get_referrals_nolock(r, &replica_referrals); /* replica_referrals has to be free'd */
  1223. /* JCMREPL - What if there's a Total update in progress? */
  1224. if (r->repl_type == REPLICA_TYPE_READONLY) {
  1225. state_backend = 0;
  1226. } else if (r->repl_type == REPLICA_TYPE_UPDATABLE) {
  1227. state_backend = 1;
  1228. }
  1229. /* Unlock to avoid changing MTN state under repl lock */
  1230. replica_unlock(r->repl_lock);
  1231. if (state_backend == 0) {
  1232. /* Read-Only - The mapping tree should be refering all update operations. */
  1233. mtn_state = STATE_UPDATE_REFERRAL;
  1234. } else if (state_backend == 1) {
  1235. /* Updatable - The mapping tree should be accepting all update operations. */
  1236. mtn_state = STATE_BACKEND;
  1237. }
  1238. /* JCMREPL - Check the return code. */
  1239. repl_set_mtn_state_and_referrals(repl_root_sdn, mtn_state, NULL,
  1240. ruv_referrals, replica_referrals);
  1241. charray_free(ruv_referrals);
  1242. charray_free(replica_referrals);
  1243. }
  1244. void
  1245. replica_set_enabled(Replica *r, PRBool enable)
  1246. {
  1247. PR_ASSERT(r);
  1248. replica_lock(r->repl_lock);
  1249. if (enable) {
  1250. if (r->repl_eqcxt_rs == NULL) /* event is not already registered */
  1251. {
  1252. r->repl_eqcxt_rs = slapi_eq_repeat(replica_update_state, r->repl_name,
  1253. slapi_current_utc_time() + START_UPDATE_DELAY, RUV_SAVE_INTERVAL);
  1254. }
  1255. } else /* disable */
  1256. {
  1257. if (r->repl_eqcxt_rs) /* event is still registerd */
  1258. {
  1259. slapi_eq_cancel(r->repl_eqcxt_rs);
  1260. r->repl_eqcxt_rs = NULL;
  1261. }
  1262. }
  1263. replica_unlock(r->repl_lock);
  1264. }
  1265. /* This function is generally called when replica's data store
  1266. is reloaded. It retrieves new RUV from the datastore. If new
  1267. RUV does not exist or if it is not as up to date as the purge RUV
  1268. of the corresponding changelog file, we need to remove */
  1269. /* the function minimizes the use of replica lock where ever possible.
  1270. Locking replica lock while calling changelog functions
  1271. causes a deadlock because changelog calls replica functions that
  1272. that lock the same lock */
  1273. int
  1274. replica_reload_ruv(Replica *r)
  1275. {
  1276. int rc = 0;
  1277. Object *old_ruv_obj = NULL, *new_ruv_obj = NULL;
  1278. RUV *upper_bound_ruv = NULL;
  1279. RUV *new_ruv = NULL;
  1280. Object *r_obj;
  1281. PR_ASSERT(r);
  1282. replica_lock(r->repl_lock);
  1283. old_ruv_obj = r->repl_ruv;
  1284. r->repl_ruv = NULL;
  1285. rc = _replica_configure_ruv(r, PR_TRUE);
  1286. replica_unlock(r->repl_lock);
  1287. if (rc != 0) {
  1288. return rc;
  1289. }
  1290. /* check if there is a changelog and whether this replica logs changes */
  1291. if (cl5GetState() == CL5_STATE_OPEN && (r->repl_flags & REPLICA_LOG_CHANGES)) {
  1292. /* Compare new ruv to the changelog's upper bound ruv. We could only keep
  1293. the existing changelog if its upper bound is the same as replica's RUV.
  1294. This is because if changelog has changes not in RUV, they will be
  1295. eventually sent to the consumer's which will cause a state mismatch
  1296. (because the supplier does not actually contain the changes in its data store.
  1297. If, on the other hand, the changelog is not as up to date as the supplier,
  1298. it is not really useful since out of sync consumer's can't be brought
  1299. up to date using this changelog and hence will need to be reinitialized */
  1300. /* replace ruv to make sure we work with the correct changelog file */
  1301. replica_lock(r->repl_lock);
  1302. new_ruv_obj = r->repl_ruv;
  1303. r->repl_ruv = old_ruv_obj;
  1304. replica_unlock(r->repl_lock);
  1305. rc = cl5GetUpperBoundRUV(r, &upper_bound_ruv);
  1306. if (rc != CL5_SUCCESS && rc != CL5_NOTFOUND) {
  1307. return -1;
  1308. }
  1309. if (upper_bound_ruv) {
  1310. new_ruv = object_get_data(new_ruv_obj);
  1311. PR_ASSERT(new_ruv);
  1312. /* ONREPL - there are more efficient ways to establish RUV equality.
  1313. However, because this is not in the critical path and we at most
  1314. have 2 elements in the RUV, this will not effect performance */
  1315. if (!ruv_covers_ruv(new_ruv, upper_bound_ruv) ||
  1316. !ruv_covers_ruv(upper_bound_ruv, new_ruv)) {
  1317. /* create a temporary replica object to conform to the interface */
  1318. r_obj = object_new(r, NULL);
  1319. /* We can't use existing changelog - remove existing file */
  1320. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name, "replica_reload_ruv - "
  1321. "New data for replica %s does not match the data in the changelog.\n"
  1322. " Recreating the changelog file. This could affect replication with replica's "
  1323. " consumers in which case the consumers should be reinitialized.\n",
  1324. slapi_sdn_get_dn(r->repl_root));
  1325. rc = cl5DeleteDBSync(r_obj);
  1326. /* reinstate new ruv */
  1327. replica_lock(r->repl_lock);
  1328. r->repl_ruv = new_ruv_obj;
  1329. object_release(r_obj);
  1330. if (rc == CL5_SUCCESS) {
  1331. /* log changes to mark starting point for replication */
  1332. rc = replica_log_ruv_elements_nolock(r);
  1333. }
  1334. replica_unlock(r->repl_lock);
  1335. } else {
  1336. /* we just need to reinstate new ruv */
  1337. replica_lock(r->repl_lock);
  1338. r->repl_ruv = new_ruv_obj;
  1339. replica_unlock(r->repl_lock);
  1340. }
  1341. } else /* upper bound vector is not there - we have no changes logged */
  1342. {
  1343. /* reinstate new ruv */
  1344. replica_lock(r->repl_lock);
  1345. r->repl_ruv = new_ruv_obj;
  1346. /* just log elements of the current RUV. This is to have
  1347. a starting point for iteration through the changes */
  1348. rc = replica_log_ruv_elements_nolock(r);
  1349. replica_unlock(r->repl_lock);
  1350. }
  1351. }
  1352. if (rc == 0) {
  1353. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  1354. /* reset mapping tree referrals based on new local RUV */
  1355. }
  1356. if (old_ruv_obj)
  1357. object_release(old_ruv_obj);
  1358. if (upper_bound_ruv)
  1359. ruv_destroy(&upper_bound_ruv);
  1360. return rc;
  1361. }
  1362. /* this function is called during server startup for each replica
  1363. to check whether the replica's data was reloaded offline and
  1364. whether replica's changelog needs to be reinitialized */
  1365. /* the function does not use replica lock but all functions it calls are
  1366. thread safe. Locking replica lock while calling changelog functions
  1367. causes a deadlock because changelog calls replica functions that
  1368. that lock the same lock */
  1369. int
  1370. replica_check_for_data_reload(Replica *r, void *arg __attribute__((unused)))
  1371. {
  1372. int rc = 0;
  1373. RUV *upper_bound_ruv = NULL;
  1374. RUV *r_ruv = NULL;
  1375. Object *r_obj, *ruv_obj;
  1376. PR_ASSERT(r);
  1377. /* check that we have a changelog and if this replica logs changes */
  1378. if (cl5GetState() == CL5_STATE_OPEN && (r->repl_flags & REPLICA_LOG_CHANGES)) {
  1379. /* Compare new ruv to the purge ruv. If the new contains csns which
  1380. are smaller than those in purge ruv, we need to remove old and
  1381. create new changelog file for this replica. This is because we
  1382. will not have sufficient changes to incrementally update a consumer
  1383. to the current state of the supplier. */
  1384. rc = cl5GetUpperBoundRUV(r, &upper_bound_ruv);
  1385. if (rc != CL5_SUCCESS && rc != CL5_NOTFOUND) {
  1386. return -1;
  1387. }
  1388. if (upper_bound_ruv) {
  1389. ruv_obj = replica_get_ruv(r);
  1390. r_ruv = object_get_data(ruv_obj);
  1391. PR_ASSERT(r_ruv);
  1392. /* Compare new ruv to the changelog's upper bound ruv. We could only keep
  1393. the existing changelog if its upper bound is the same as replica's RUV.
  1394. This is because if changelog has changes not in RUV, they will be
  1395. eventually sent to the consumer's which will cause a state mismatch
  1396. (because the supplier does not actually contain the changes in its data store.
  1397. If, on the other hand, the changelog is not as up to date as the supplier,
  1398. it is not really useful since out of sync consumer's can't be brought
  1399. up to date using this changelog and hence will need to be reinitialized */
  1400. /*
  1401. * Actually we can ignore the scenario that the changelog's upper
  1402. * bound ruv covers data store's ruv for two reasons: (1) a change
  1403. * is always written to the changelog after it is committed to the
  1404. * data store; (2) a change will be ignored if the server has seen
  1405. * it before - this happens frequently at the beginning of replication
  1406. * sessions.
  1407. */
  1408. if (slapi_disordely_shutdown(PR_FALSE)) {
  1409. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name, "replica_check_for_data_reload - "
  1410. "Disorderly shutdown for replica %s. Check if DB RUV needs to be updated\n",
  1411. slapi_sdn_get_dn(r->repl_root));
  1412. if (ruv_covers_ruv(upper_bound_ruv, r_ruv) && !ruv_covers_ruv(r_ruv, upper_bound_ruv)) {
  1413. /*
  1414. * The Changelog RUV is ahead of the RUV in the DB.
  1415. * RUV DB was likely not flushed on disk.
  1416. */
  1417. ruv_force_csn_update_from_ruv(upper_bound_ruv, r_ruv,
  1418. "Force update of database RUV (from CL RUV) -> ", SLAPI_LOG_NOTICE);
  1419. replica_set_ruv_dirty(r);
  1420. }
  1421. } else {
  1422. rc = ruv_compare_ruv(upper_bound_ruv, "changelog max RUV", r_ruv, "database RUV", 0, SLAPI_LOG_ERR);
  1423. if (RUV_COMP_IS_FATAL(rc)) {
  1424. /* create a temporary replica object to conform to the interface */
  1425. r_obj = object_new(r, NULL);
  1426. /* We can't use existing changelog - remove existing file */
  1427. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name, "replica_check_for_data_reload - "
  1428. "Data for replica %s does not match the data in the changelog. "
  1429. "Recreating the changelog file. "
  1430. "This could affect replication with replica's consumers in which case the "
  1431. "consumers should be reinitialized.\n",
  1432. slapi_sdn_get_dn(r->repl_root));
  1433. rc = cl5DeleteDBSync(r_obj);
  1434. object_release(r_obj);
  1435. if (rc == CL5_SUCCESS) {
  1436. /* log changes to mark starting point for replication */
  1437. rc = replica_log_ruv_elements(r);
  1438. }
  1439. } else if (rc) {
  1440. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name, "replica_check_for_data_reload - "
  1441. "For replica %s there were some differences between the changelog max RUV and the "
  1442. "database RUV. If there are obsolete elements in the database RUV, you "
  1443. "should remove them using the CLEANALLRUV task. If they are not obsolete, "
  1444. "you should check their status to see why there are no changes from those "
  1445. "servers in the changelog.\n",
  1446. slapi_sdn_get_dn(r->repl_root));
  1447. rc = 0;
  1448. }
  1449. } /* slapi_disordely_shutdown */
  1450. object_release(ruv_obj);
  1451. } else /* we have no changes currently logged for this replica */
  1452. {
  1453. /* log changes to mark starting point for replication */
  1454. rc = replica_log_ruv_elements(r);
  1455. }
  1456. }
  1457. if (rc == 0) {
  1458. /* reset mapping tree referrals based on new local RUV */
  1459. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  1460. }
  1461. if (upper_bound_ruv)
  1462. ruv_destroy(&upper_bound_ruv);
  1463. return rc;
  1464. }
  1465. /* Helper functions */
  1466. /* reads replica configuration entry. The entry is the child of the
  1467. mapping tree node for the replica's backend */
  1468. static Slapi_Entry *
  1469. _replica_get_config_entry(const Slapi_DN *root, const char **attrs)
  1470. {
  1471. int rc = 0;
  1472. char *dn = NULL;
  1473. Slapi_Entry **entries;
  1474. Slapi_Entry *e = NULL;
  1475. Slapi_PBlock *pb = NULL;
  1476. dn = _replica_get_config_dn(root);
  1477. if (NULL == dn) {
  1478. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  1479. "_replica_get_config_entry - Failed to get the config dn for %s\n",
  1480. slapi_sdn_get_dn(root));
  1481. return NULL;
  1482. }
  1483. pb = slapi_pblock_new();
  1484. slapi_search_internal_set_pb(pb, dn, LDAP_SCOPE_BASE, "objectclass=*", (char **)attrs, 0, NULL,
  1485. NULL, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  1486. slapi_search_internal_pb(pb);
  1487. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1488. if (rc == 0) {
  1489. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1490. e = slapi_entry_dup(entries[0]);
  1491. }
  1492. slapi_free_search_results_internal(pb);
  1493. slapi_pblock_destroy(pb);
  1494. slapi_ch_free_string(&dn);
  1495. return e;
  1496. }
  1497. /* It does an internal search to read the in memory RUV
  1498. * of the provided suffix
  1499. */
  1500. Slapi_Entry *
  1501. get_in_memory_ruv(Slapi_DN *suffix_sdn)
  1502. {
  1503. const char *attrs[4];
  1504. /* these two attributes needs to be asked when reading the RUV */
  1505. attrs[0] = type_ruvElement;
  1506. attrs[1] = type_ruvElementUpdatetime;
  1507. attrs[2] = type_agmtMaxCSN;
  1508. attrs[3] = NULL;
  1509. return (_replica_get_config_entry(suffix_sdn, attrs));
  1510. }
  1511. char *
  1512. replica_get_dn(Replica *r)
  1513. {
  1514. return _replica_get_config_dn(r->repl_root);
  1515. }
  1516. static int
  1517. _replica_check_validity(const Replica *r)
  1518. {
  1519. PR_ASSERT(r);
  1520. if (r->repl_root == NULL || r->repl_type == 0 || r->repl_rid == 0 ||
  1521. r->repl_csngen == NULL || r->repl_name == NULL) {
  1522. return -1;
  1523. } else {
  1524. return 0;
  1525. }
  1526. }
  1527. /* replica configuration entry has the following format:
  1528. dn: cn=replica,<mapping tree node dn>
  1529. objectclass: top
  1530. objectclass: nsds5Replica
  1531. objectclass: extensibleObject
  1532. nsds5ReplicaRoot: <root of the replica>
  1533. nsds5ReplicaId: <replica id>
  1534. nsds5ReplicaType: <type of the replica: primary, read-write or read-only>
  1535. nsState: <state of the csn generator> missing the first time replica is started
  1536. nsds5ReplicaBindDN: <supplier update dn> consumers only
  1537. nsds5ReplicaBindDNGroup: group, containing replicaBindDNs
  1538. nsds5ReplicaBindDNGroupCheckInterval: defines how frequently to check for update of bindGroup
  1539. nsds5ReplicaReferral: <referral URL to updatable replica> consumers only
  1540. nsds5ReplicaPurgeDelay: <time, in seconds, to keep purgeable CSNs, 0 == keep forever>
  1541. nsds5ReplicaTombstonePurgeInterval: <time, in seconds, between tombstone purge runs, 0 == don't reap>
  1542. richm: changed slapi entry from const to editable - if the replica id is supplied for a read
  1543. only replica, we ignore it and replace the value with the READ_ONLY_REPLICA_ID
  1544. */
  1545. static int
  1546. _replica_init_from_config(Replica *r, Slapi_Entry *e, char *errortext)
  1547. {
  1548. Slapi_Attr *a = NULL;
  1549. Slapi_Attr *attr;
  1550. CSNGen *gen;
  1551. char *precise_purging = NULL;
  1552. char buf[SLAPI_DSE_RETURNTEXT_SIZE];
  1553. char *errormsg = errortext ? errortext : buf;
  1554. char *val;
  1555. int backoff_min;
  1556. int backoff_max;
  1557. int ptimeout = 0;
  1558. int release_timeout = 0;
  1559. int rc;
  1560. PR_ASSERT(r && e);
  1561. /* get replica root */
  1562. val = slapi_entry_attr_get_charptr(e, attr_replicaRoot);
  1563. if (val == NULL) {
  1564. PR_snprintf(errormsg, SLAPI_DSE_RETURNTEXT_SIZE, "Failed to retrieve %s attribute from (%s)\n",
  1565. attr_replicaRoot,
  1566. (char *)slapi_entry_get_dn((Slapi_Entry *)e));
  1567. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "_replica_init_from_config - %s\n",
  1568. errormsg);
  1569. return -1;
  1570. }
  1571. r->repl_root = slapi_sdn_new_dn_passin(val);
  1572. /* get replica type */
  1573. val = slapi_entry_attr_get_charptr(e, attr_replicaType);
  1574. if (val) {
  1575. r->repl_type = atoi(val);
  1576. slapi_ch_free((void **)&val);
  1577. } else {
  1578. r->repl_type = REPLICA_TYPE_READONLY;
  1579. }
  1580. /* grab and validate the backoff retry settings */
  1581. if (slapi_entry_attr_exists(e, type_replicaBackoffMin)) {
  1582. backoff_min = slapi_entry_attr_get_int(e, type_replicaBackoffMin);
  1583. if (backoff_min <= 0) {
  1584. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name, "_replica_init_from_config - "
  1585. "Invalid value for %s: %d Using default value (%d)\n",
  1586. type_replicaBackoffMin, backoff_min, PROTOCOL_BACKOFF_MINIMUM);
  1587. backoff_min = PROTOCOL_BACKOFF_MINIMUM;
  1588. }
  1589. } else {
  1590. backoff_min = PROTOCOL_BACKOFF_MINIMUM;
  1591. }
  1592. if (slapi_entry_attr_exists(e, type_replicaBackoffMax)) {
  1593. backoff_max = slapi_entry_attr_get_int(e, type_replicaBackoffMax);
  1594. if (backoff_max <= 0) {
  1595. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name, "_replica_init_from_config - "
  1596. "Invalid value for %s: %d Using default value (%d)\n",
  1597. type_replicaBackoffMax, backoff_max, PROTOCOL_BACKOFF_MAXIMUM);
  1598. backoff_max = PROTOCOL_BACKOFF_MAXIMUM;
  1599. }
  1600. } else {
  1601. backoff_max = PROTOCOL_BACKOFF_MAXIMUM;
  1602. }
  1603. if (backoff_min > backoff_max) {
  1604. /* Ok these values are invalid, reset back the defaults */
  1605. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name, "_replica_init_from_config - "
  1606. "Backoff minimum (%d) can not be greater than "
  1607. "the backoff maximum (%d). Using default values: min (%d) max (%d)\n",
  1608. backoff_min, backoff_max, PROTOCOL_BACKOFF_MINIMUM, PROTOCOL_BACKOFF_MAXIMUM);
  1609. slapi_counter_set_value(r->backoff_min, PROTOCOL_BACKOFF_MINIMUM);
  1610. slapi_counter_set_value(r->backoff_max, PROTOCOL_BACKOFF_MAXIMUM);
  1611. } else {
  1612. slapi_counter_set_value(r->backoff_min, backoff_min);
  1613. slapi_counter_set_value(r->backoff_max, backoff_max);
  1614. }
  1615. /* get the protocol timeout */
  1616. ptimeout = slapi_entry_attr_get_int(e, type_replicaProtocolTimeout);
  1617. if (ptimeout <= 0) {
  1618. slapi_counter_set_value(r->protocol_timeout, DEFAULT_PROTOCOL_TIMEOUT);
  1619. } else {
  1620. slapi_counter_set_value(r->protocol_timeout, ptimeout);
  1621. }
  1622. /* Get the release timeout */
  1623. release_timeout = slapi_entry_attr_get_int(e, type_replicaReleaseTimeout);
  1624. if (release_timeout <= 0) {
  1625. slapi_counter_set_value(r->release_timeout, 0);
  1626. } else {
  1627. slapi_counter_set_value(r->release_timeout, release_timeout);
  1628. }
  1629. /* check for precise tombstone purging */
  1630. precise_purging = slapi_entry_attr_get_charptr(e, type_replicaPrecisePurge);
  1631. if (precise_purging) {
  1632. if (strcasecmp(precise_purging, "on") == 0) {
  1633. slapi_counter_set_value(r->precise_purging, 1);
  1634. } else if (strcasecmp(precise_purging, "off") == 0) {
  1635. slapi_counter_set_value(r->precise_purging, 0);
  1636. } else {
  1637. /* Invalid value */
  1638. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name, "_replica_init_from_config - "
  1639. "Invalid value for %s: %s Using default value (off)\n",
  1640. type_replicaPrecisePurge, precise_purging);
  1641. slapi_counter_set_value(r->precise_purging, 0);
  1642. }
  1643. slapi_ch_free_string(&precise_purging);
  1644. } else {
  1645. slapi_counter_set_value(r->precise_purging, 0);
  1646. }
  1647. /* get replica flags */
  1648. r->repl_flags = slapi_entry_attr_get_ulong(e, attr_flags);
  1649. /*
  1650. * Get replicaid
  1651. * The replica id is ignored for read only replicas and is set to the
  1652. * special value READ_ONLY_REPLICA_ID
  1653. */
  1654. if (r->repl_type == REPLICA_TYPE_READONLY) {
  1655. r->repl_rid = READ_ONLY_REPLICA_ID;
  1656. slapi_entry_attr_set_uint(e, attr_replicaId, (unsigned int)READ_ONLY_REPLICA_ID);
  1657. }
  1658. /* a replica id is required for updatable and primary replicas */
  1659. else if (r->repl_type == REPLICA_TYPE_UPDATABLE ||
  1660. r->repl_type == REPLICA_TYPE_PRIMARY) {
  1661. if ((val = slapi_entry_attr_get_charptr(e, attr_replicaId))) {
  1662. int temprid = atoi(val);
  1663. slapi_ch_free((void **)&val);
  1664. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID) {
  1665. PR_snprintf(errormsg, SLAPI_DSE_RETURNTEXT_SIZE,
  1666. "Attribute %s must have a value greater than 0 "
  1667. "and less than %d: entry %s",
  1668. attr_replicaId, READ_ONLY_REPLICA_ID,
  1669. (char *)slapi_entry_get_dn((Slapi_Entry *)e));
  1670. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  1671. "_replica_init_from_config - %s\n", errormsg);
  1672. return -1;
  1673. } else {
  1674. r->repl_rid = (ReplicaId)temprid;
  1675. }
  1676. } else {
  1677. PR_snprintf(errormsg, SLAPI_DSE_RETURNTEXT_SIZE,
  1678. "Failed to retrieve required %s attribute from %s",
  1679. attr_replicaId, (char *)slapi_entry_get_dn((Slapi_Entry *)e));
  1680. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  1681. "_replica_init_from_config - %s\n", errormsg);
  1682. return -1;
  1683. }
  1684. }
  1685. attr = NULL;
  1686. rc = slapi_entry_attr_find(e, attr_state, &attr);
  1687. gen = csngen_new(r->repl_rid, attr);
  1688. if (gen == NULL) {
  1689. PR_snprintf(errormsg, SLAPI_DSE_RETURNTEXT_SIZE,
  1690. "Failed to create csn generator for replica (%s)",
  1691. (char *)slapi_entry_get_dn((Slapi_Entry *)e));
  1692. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  1693. "_replica_init_from_config - %s\n", errormsg);
  1694. return -1;
  1695. }
  1696. r->repl_csngen = object_new((void *)gen, (FNFree)csngen_free);
  1697. /* Hook generator so we can maintain min/max CSN info */
  1698. r->csn_pl_reg_id = csngen_register_callbacks(gen, assign_csn_callback, r, abort_csn_callback, r);
  1699. /* get replication bind dn */
  1700. r->updatedn_list = replica_updatedn_list_new(e);
  1701. /* get replication bind dn groups */
  1702. r->updatedn_groups = replica_updatedn_group_new(e);
  1703. r->groupdn_list = replica_groupdn_list_new(r->updatedn_groups);
  1704. r->updatedn_group_last_check = time(NULL);
  1705. /* get groupdn check interval */
  1706. val = slapi_entry_attr_get_charptr(e, attr_replicaBindDnGroupCheckInterval);
  1707. if (val) {
  1708. r->updatedn_group_check_interval = atoi(val);
  1709. slapi_ch_free((void **)&val);
  1710. } else {
  1711. r->updatedn_group_check_interval = -1;
  1712. }
  1713. /* get replica name */
  1714. val = slapi_entry_attr_get_charptr(e, attr_replicaName);
  1715. if (val) {
  1716. r->repl_name = val;
  1717. } else {
  1718. rc = slapi_uniqueIDGenerateString(&r->repl_name);
  1719. if (rc != UID_SUCCESS) {
  1720. PR_snprintf(errormsg, SLAPI_DSE_RETURNTEXT_SIZE,
  1721. "Failed to assign replica name for replica (%s); uuid generator error - %d ",
  1722. (char *)slapi_entry_get_dn((Slapi_Entry *)e), rc);
  1723. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "_replica_init_from_config - %s\n",
  1724. errormsg);
  1725. return -1;
  1726. } else
  1727. r->new_name = PR_TRUE;
  1728. }
  1729. /* get the list of referrals */
  1730. slapi_entry_attr_find(e, attr_replicaReferral, &attr);
  1731. if (attr != NULL) {
  1732. slapi_attr_get_valueset(attr, &r->repl_referral);
  1733. }
  1734. /*
  1735. * Set the purge offset (default 7 days). This is the extra
  1736. * time we allow purgeable CSNs to stick around, in case a
  1737. * replica regresses. Could also be useful when LCUP happens,
  1738. * since we don't know about LCUP replicas, and they can just
  1739. * turn up whenever they want to.
  1740. */
  1741. if (slapi_entry_attr_find(e, type_replicaPurgeDelay, &a) == -1) {
  1742. /* No purge delay provided, so use default */
  1743. r->repl_purge_delay = 60 * 60 * 24 * 7; /* One week, in seconds */
  1744. } else {
  1745. r->repl_purge_delay = slapi_entry_attr_get_uint(e, type_replicaPurgeDelay);
  1746. }
  1747. if (slapi_entry_attr_find(e, type_replicaTombstonePurgeInterval, &a) == -1) {
  1748. /* No reap interval provided, so use default */
  1749. r->tombstone_reap_interval = 3600 * 24; /* One day */
  1750. } else {
  1751. r->tombstone_reap_interval = slapi_entry_attr_get_int(e, type_replicaTombstonePurgeInterval);
  1752. }
  1753. r->tombstone_reap_stop = r->tombstone_reap_active = PR_FALSE;
  1754. /* No supplier holding the replica */
  1755. r->locking_conn = ULONG_MAX;
  1756. return (_replica_check_validity(r));
  1757. }
  1758. void
  1759. replica_check_for_tasks(Replica *r, Slapi_Entry *e)
  1760. {
  1761. char **clean_vals;
  1762. if (e == NULL || ldif_dump_is_running() == PR_TRUE) {
  1763. /* If db2ldif is being run, do not check if there are incomplete tasks */
  1764. return;
  1765. }
  1766. /*
  1767. * check if we are in the middle of a CLEANALLRUV task,
  1768. * if so set the cleaned rid, and fire off the thread
  1769. */
  1770. if ((clean_vals = slapi_entry_attr_get_charray(e, type_replicaCleanRUV)) != NULL) {
  1771. PRThread *thread = NULL;
  1772. struct berval *payload = NULL;
  1773. CSN *maxcsn = NULL;
  1774. ReplicaId rid;
  1775. char csnstr[CSN_STRSIZE];
  1776. char *token = NULL;
  1777. char *forcing;
  1778. char *csnpart;
  1779. char *ridstr;
  1780. char *iter = NULL;
  1781. int i;
  1782. for (i = 0; i < CLEANRIDSIZ && clean_vals[i]; i++) {
  1783. cleanruv_data *data = NULL;
  1784. /*
  1785. * Set the cleanruv data, and add the cleaned rid
  1786. */
  1787. token = ldap_utf8strtok_r(clean_vals[i], ":", &iter);
  1788. if (token) {
  1789. rid = atoi(token);
  1790. if (rid <= 0 || rid >= READ_ONLY_REPLICA_ID) {
  1791. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "CleanAllRUV Task - Invalid replica id(%d) "
  1792. "aborting task.\n",
  1793. rid);
  1794. goto done;
  1795. }
  1796. } else {
  1797. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "CleanAllRUV Task - Unable to parse cleanallruv "
  1798. "data (%s), aborting task.\n",
  1799. clean_vals[i]);
  1800. goto done;
  1801. }
  1802. csnpart = ldap_utf8strtok_r(iter, ":", &iter);
  1803. maxcsn = csn_new();
  1804. csn_init_by_string(maxcsn, csnpart);
  1805. csn_as_string(maxcsn, PR_FALSE, csnstr);
  1806. forcing = ldap_utf8strtok_r(iter, ":", &iter);
  1807. if (forcing == NULL) {
  1808. forcing = "no";
  1809. }
  1810. slapi_log_err(SLAPI_LOG_NOTICE, repl_plugin_name, "CleanAllRUV Task - cleanAllRUV task found, "
  1811. "resuming the cleaning of rid(%d)...\n",
  1812. rid);
  1813. /*
  1814. * Create payload
  1815. */
  1816. ridstr = slapi_ch_smprintf("%d:%s:%s:%s", rid, slapi_sdn_get_dn(replica_get_root(r)), csnstr, forcing);
  1817. payload = create_cleanruv_payload(ridstr);
  1818. slapi_ch_free_string(&ridstr);
  1819. if (payload == NULL) {
  1820. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "CleanAllRUV Task - Startup: Failed to "
  1821. "create extended op payload, aborting task");
  1822. csn_free(&maxcsn);
  1823. goto done;
  1824. }
  1825. /*
  1826. * Setup the data struct, and fire off the thread.
  1827. */
  1828. data = (cleanruv_data *)slapi_ch_calloc(1, sizeof(cleanruv_data));
  1829. if (data == NULL) {
  1830. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "CleanAllRUV Task - Failed to allocate cleanruv_data.\n");
  1831. csn_free(&maxcsn);
  1832. } else {
  1833. /* setup our data */
  1834. data->repl_obj = NULL;
  1835. data->replica = NULL;
  1836. data->rid = rid;
  1837. data->task = NULL;
  1838. data->maxcsn = maxcsn;
  1839. data->payload = payload;
  1840. data->sdn = slapi_sdn_dup(r->repl_root);
  1841. data->force = slapi_ch_strdup(forcing);
  1842. data->repl_root = NULL;
  1843. thread = PR_CreateThread(PR_USER_THREAD, replica_cleanallruv_thread_ext,
  1844. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1845. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1846. if (thread == NULL) {
  1847. /* log an error and free everything */
  1848. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "CleanAllRUV Task - Unable to create cleanAllRUV "
  1849. "thread for rid(%d)\n",
  1850. (int)data->rid);
  1851. csn_free(&maxcsn);
  1852. slapi_sdn_free(&data->sdn);
  1853. ber_bvfree(data->payload);
  1854. slapi_ch_free_string(&data->force);
  1855. slapi_ch_free((void **)&data);
  1856. }
  1857. }
  1858. }
  1859. done:
  1860. slapi_ch_array_free(clean_vals);
  1861. }
  1862. if ((clean_vals = slapi_entry_attr_get_charray(e, type_replicaAbortCleanRUV)) != NULL) {
  1863. PRThread *thread = NULL;
  1864. struct berval *payload;
  1865. ReplicaId rid;
  1866. char *certify = NULL;
  1867. char *ridstr = NULL;
  1868. char *token = NULL;
  1869. char *repl_root;
  1870. char *iter = NULL;
  1871. int i;
  1872. for (i = 0; clean_vals[i]; i++) {
  1873. cleanruv_data *data = NULL;
  1874. token = ldap_utf8strtok_r(clean_vals[i], ":", &iter);
  1875. if (token) {
  1876. rid = atoi(token);
  1877. if (rid <= 0 || rid >= READ_ONLY_REPLICA_ID) {
  1878. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "Abort CleanAllRUV Task - Invalid replica id(%d) "
  1879. "aborting abort task.\n",
  1880. rid);
  1881. goto done2;
  1882. }
  1883. } else {
  1884. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "Abort CleanAllRUV Task - Unable to parse cleanallruv "
  1885. "data (%s), aborting abort task.\n",
  1886. clean_vals[i]);
  1887. goto done2;
  1888. }
  1889. repl_root = ldap_utf8strtok_r(iter, ":", &iter);
  1890. certify = ldap_utf8strtok_r(iter, ":", &iter);
  1891. if (!is_cleaned_rid(rid)) {
  1892. slapi_log_err(SLAPI_LOG_NOTICE, repl_plugin_name, "Abort CleanAllRUV Task - Replica id(%d) is not "
  1893. "being cleaned, nothing to abort. Aborting abort task.\n",
  1894. rid);
  1895. delete_aborted_rid(r, rid, repl_root, 0);
  1896. goto done2;
  1897. }
  1898. add_aborted_rid(rid, r, repl_root);
  1899. stop_ruv_cleaning();
  1900. slapi_log_err(SLAPI_LOG_NOTICE, repl_plugin_name, "Abort CleanAllRUV Task - Abort task found, "
  1901. "resuming abort of rid(%d).\n",
  1902. rid);
  1903. /*
  1904. * Setup the data struct, and fire off the abort thread.
  1905. */
  1906. data = (cleanruv_data *)slapi_ch_calloc(1, sizeof(cleanruv_data));
  1907. if (data == NULL) {
  1908. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "Abort CleanAllRUV Task - Failed to allocate cleanruv_data.\n");
  1909. } else {
  1910. ridstr = slapi_ch_smprintf("%d:%s:%s", rid, repl_root, certify);
  1911. payload = create_cleanruv_payload(ridstr);
  1912. slapi_ch_free_string(&ridstr);
  1913. if (payload == NULL) {
  1914. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "Abort CleanAllRUV Task - Failed to create extended "
  1915. "op payload\n");
  1916. slapi_ch_free((void **)&data);
  1917. } else {
  1918. /* setup the data */
  1919. data->repl_obj = NULL;
  1920. data->replica = NULL;
  1921. data->rid = rid;
  1922. data->task = NULL;
  1923. data->payload = payload;
  1924. data->repl_root = slapi_ch_strdup(repl_root);
  1925. data->sdn = slapi_sdn_dup(r->repl_root);
  1926. data->certify = slapi_ch_strdup(certify);
  1927. thread = PR_CreateThread(PR_USER_THREAD, replica_abort_task_thread,
  1928. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1929. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1930. if (thread == NULL) {
  1931. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "Abort CleanAllRUV Task - Unable to create abort cleanAllRUV "
  1932. "thread for rid(%d)\n",
  1933. (int)data->rid);
  1934. slapi_sdn_free(&data->sdn);
  1935. ber_bvfree(data->payload);
  1936. slapi_ch_free_string(&data->repl_root);
  1937. slapi_ch_free_string(&data->certify);
  1938. slapi_ch_free((void **)&data);
  1939. }
  1940. }
  1941. }
  1942. }
  1943. done2:
  1944. slapi_ch_array_free(clean_vals);
  1945. }
  1946. }
  1947. /* This function updates the entry to contain information generated
  1948. during replica initialization.
  1949. Returns 0 if successful and -1 otherwise */
  1950. static int
  1951. _replica_update_entry(Replica *r, Slapi_Entry *e, char *errortext)
  1952. {
  1953. int rc;
  1954. Slapi_Mod smod;
  1955. Slapi_Value *val;
  1956. PR_ASSERT(r);
  1957. /* add attribute that stores state of csn generator */
  1958. rc = csngen_get_state((CSNGen *)object_get_data(r->repl_csngen), &smod);
  1959. if (rc != CSN_SUCCESS) {
  1960. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "Failed to get csn generator's state; csn error - %d", rc);
  1961. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  1962. "_replica_update_entry - %s\n", errortext);
  1963. return -1;
  1964. }
  1965. val = slapi_value_new_berval(slapi_mod_get_first_value(&smod));
  1966. rc = slapi_entry_add_value(e, slapi_mod_get_type(&smod), val);
  1967. slapi_value_free(&val);
  1968. slapi_mod_done(&smod);
  1969. if (rc != 0) {
  1970. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "Failed to update replica entry");
  1971. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  1972. "_replica_update_entry - %s\n", errortext);
  1973. return -1;
  1974. }
  1975. /* add attribute that stores replica name */
  1976. rc = slapi_entry_add_string(e, attr_replicaName, r->repl_name);
  1977. if (rc != 0) {
  1978. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "Failed to update replica entry");
  1979. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  1980. "_replica_update_entry - %s\n", errortext);
  1981. return -1;
  1982. } else
  1983. r->new_name = PR_FALSE;
  1984. return 0;
  1985. }
  1986. /* DN format: cn=replica,cn=\"<root>\",cn=mapping tree,cn=config */
  1987. static char *
  1988. _replica_get_config_dn(const Slapi_DN *root)
  1989. {
  1990. char *dn;
  1991. /* "cn=mapping tree,cn=config" */
  1992. const char *mp_base = slapi_get_mapping_tree_config_root();
  1993. PR_ASSERT(root);
  1994. /* This function converts the old style DN to the new style. */
  1995. dn = slapi_ch_smprintf("%s,cn=\"%s\",%s",
  1996. REPLICA_RDN, slapi_sdn_get_dn(root), mp_base);
  1997. return dn;
  1998. }
  1999. /* This function retrieves RUV from the root of the replicated tree.
  2000. * The attribute can be missing if
  2001. * (1) this replica is the first supplier and replica generation has not been assigned
  2002. * or
  2003. * (2) this is a consumer that has not been yet initialized
  2004. * In either case, replica_set_ruv should be used to further initialize the replica.
  2005. * Returns 0 on success, -1 on failure. If 0 is returned, the RUV is present in the replica.
  2006. */
  2007. static int
  2008. _replica_configure_ruv(Replica *r, PRBool isLocked __attribute__((unused)))
  2009. {
  2010. Slapi_PBlock *pb = NULL;
  2011. char *attrs[2];
  2012. int rc;
  2013. int return_value = -1;
  2014. Slapi_Entry **entries = NULL;
  2015. Slapi_Attr *attr;
  2016. RUV *ruv = NULL;
  2017. CSN *csn = NULL;
  2018. ReplicaId rid = 0;
  2019. /* read ruv state from the ruv tombstone entry */
  2020. pb = slapi_pblock_new();
  2021. if (!pb) {
  2022. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  2023. "_replica_configure_ruv - Out of memory\n");
  2024. goto done;
  2025. }
  2026. attrs[0] = (char *)type_ruvElement;
  2027. attrs[1] = NULL;
  2028. slapi_search_internal_set_pb(
  2029. pb,
  2030. slapi_sdn_get_dn(r->repl_root),
  2031. LDAP_SCOPE_BASE,
  2032. "objectclass=*",
  2033. attrs,
  2034. 0, /* attrsonly */
  2035. NULL, /* controls */
  2036. RUV_STORAGE_ENTRY_UNIQUEID,
  2037. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  2038. OP_FLAG_REPLICATED); /* flags */
  2039. slapi_search_internal_pb(pb);
  2040. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2041. if (rc == LDAP_SUCCESS) {
  2042. /* get RUV attributes and construct the RUV */
  2043. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2044. if (NULL == entries || NULL == entries[0]) {
  2045. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  2046. "_replica_configure_ruv - Replica ruv tombstone entry for "
  2047. "replica %s not found\n",
  2048. slapi_sdn_get_dn(r->repl_root));
  2049. goto done;
  2050. }
  2051. rc = slapi_entry_attr_find(entries[0], type_ruvElement, &attr);
  2052. if (rc != 0) /* ruv attribute is missing - this not allowed */
  2053. {
  2054. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  2055. "_replica_configure_ruv - Replica ruv tombstone entry for "
  2056. "replica %s does not contain %s\n",
  2057. slapi_sdn_get_dn(r->repl_root), type_ruvElement);
  2058. goto done;
  2059. }
  2060. /* Check in the tombstone we have retrieved if the local purl is
  2061. already present:
  2062. rid == 0: the local purl is not present
  2063. rid != 0: the local purl is present ==> nothing to do
  2064. */
  2065. ruv_init_from_slapi_attr_and_check_purl(attr, &ruv, &rid);
  2066. if (ruv) {
  2067. char *generation = NULL;
  2068. generation = ruv_get_replica_generation(ruv);
  2069. if (NULL != generation) {
  2070. r->repl_ruv = object_new((void *)ruv, (FNFree)ruv_destroy);
  2071. /* Is the local purl in the ruv? (the port or the host could have
  2072. changed)
  2073. */
  2074. /* A consumer only doesn't have its purl in its ruv */
  2075. if (r->repl_type == REPLICA_TYPE_UPDATABLE) {
  2076. int need_update = 0;
  2077. #define RUV_UPDATE_PARTIAL 1
  2078. #define RUV_UPDATE_FULL 2
  2079. if (rid == 0) {
  2080. /* We can not have more than 1 ruv with the same rid
  2081. so we replace it */
  2082. const char *purl = NULL;
  2083. purl = multimaster_get_local_purl();
  2084. ruv_delete_replica(ruv, r->repl_rid);
  2085. ruv_add_index_replica(ruv, r->repl_rid, purl, 1);
  2086. need_update = RUV_UPDATE_PARTIAL; /* ruv changed, so write tombstone */
  2087. } else /* bug 540844: make sure the local supplier rid is first in the ruv */
  2088. {
  2089. /* make sure local supplier is first in list */
  2090. ReplicaId first_rid = 0;
  2091. char *first_purl = NULL;
  2092. ruv_get_first_id_and_purl(ruv, &first_rid, &first_purl);
  2093. /* if the local supplier is not first in the list . . . */
  2094. /* rid is from changelog; first_rid is from backend */
  2095. if (rid != first_rid) {
  2096. /* . . . move the local supplier to the beginning of the list */
  2097. ruv_move_local_supplier_to_first(ruv, rid);
  2098. need_update = RUV_UPDATE_PARTIAL; /* must update tombstone also */
  2099. }
  2100. /* r->repl_rid is from config; rid is from changelog */
  2101. if (r->repl_rid != rid) {
  2102. /* Most likely, the replica was once deleted
  2103. * and recreated with a different rid from the
  2104. * previous. */
  2105. /* must recreate ruv tombstone */
  2106. need_update = RUV_UPDATE_FULL;
  2107. if (NULL != r->repl_ruv) {
  2108. object_release(r->repl_ruv);
  2109. r->repl_ruv = NULL;
  2110. }
  2111. }
  2112. }
  2113. /* Update also the directory entry */
  2114. if (RUV_UPDATE_PARTIAL == need_update) {
  2115. replica_replace_ruv_tombstone(r);
  2116. } else if (RUV_UPDATE_FULL == need_update) {
  2117. _delete_tombstone(slapi_sdn_get_dn(r->repl_root),
  2118. RUV_STORAGE_ENTRY_UNIQUEID,
  2119. OP_FLAG_REPL_RUV);
  2120. rc = replica_create_ruv_tombstone(r);
  2121. if (rc) {
  2122. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  2123. "_replica_configure_ruv - "
  2124. "Failed to recreate replica ruv tombstone entry"
  2125. " (%s); LDAP error - %d\n",
  2126. slapi_sdn_get_dn(r->repl_root), rc);
  2127. slapi_ch_free_string(&generation);
  2128. goto done;
  2129. }
  2130. }
  2131. #undef RUV_UPDATE_PARTIAL
  2132. #undef RUV_UPDATE_FULL
  2133. }
  2134. slapi_ch_free_string(&generation);
  2135. return_value = 0;
  2136. } else {
  2137. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "_replica_configure_ruv - "
  2138. "RUV for replica %s is missing replica generation\n",
  2139. slapi_sdn_get_dn(r->repl_root));
  2140. goto done;
  2141. }
  2142. } else {
  2143. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "_replica_configure_ruv - "
  2144. "Unable to convert %s attribute in entry %s to a replica update vector.\n",
  2145. type_ruvElement, slapi_sdn_get_dn(r->repl_root));
  2146. goto done;
  2147. }
  2148. } else /* search failed */
  2149. {
  2150. if (LDAP_NO_SUCH_OBJECT == rc) {
  2151. /* The entry doesn't exist: create it */
  2152. rc = replica_create_ruv_tombstone(r);
  2153. if (LDAP_SUCCESS != rc) {
  2154. /*
  2155. * XXXggood - the following error appears on startup if we try
  2156. * to initialize replica RUVs before the backend instance is up.
  2157. * It's alarming to see this error, and we should suppress it
  2158. * (or avoid trying to configure it) if the backend instance is
  2159. * not yet online.
  2160. */
  2161. /*
  2162. * XXXrichm - you can also get this error when the backend is in
  2163. * read only mode c.f. bug 539782
  2164. */
  2165. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  2166. "_replica_configure_ruv - Failed to create replica ruv tombstone "
  2167. "entry (%s); LDAP error - %d\n",
  2168. slapi_sdn_get_dn(r->repl_root), rc);
  2169. goto done;
  2170. } else {
  2171. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2172. "_replica_configure_ruv - No ruv tombstone found for replica %s. "
  2173. "Created a new one\n",
  2174. slapi_sdn_get_dn(r->repl_root));
  2175. return_value = 0;
  2176. }
  2177. } else {
  2178. /* see if the suffix is disabled */
  2179. char *state = slapi_mtn_get_state(r->repl_root);
  2180. if (state && !strcasecmp(state, "disabled")) {
  2181. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  2182. "_replica_configure_ruv - Replication disabled for "
  2183. "entry (%s); LDAP error - %d\n",
  2184. slapi_sdn_get_dn(r->repl_root), rc);
  2185. slapi_ch_free_string(&state);
  2186. goto done;
  2187. } else if (!r->repl_ruv) /* other error */
  2188. {
  2189. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  2190. "_replica_configure_ruv - Replication broken for "
  2191. "entry (%s); LDAP error - %d\n",
  2192. slapi_sdn_get_dn(r->repl_root), rc);
  2193. slapi_ch_free_string(&state);
  2194. goto done;
  2195. } else /* some error but continue anyway? */
  2196. {
  2197. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2198. "_replica_configure_ruv - Error %d reading tombstone for replica %s.\n",
  2199. rc, slapi_sdn_get_dn(r->repl_root));
  2200. return_value = 0;
  2201. }
  2202. slapi_ch_free_string(&state);
  2203. }
  2204. }
  2205. if (NULL != r->min_csn_pl) {
  2206. csnplFree(&r->min_csn_pl);
  2207. }
  2208. /* create pending list for min csn if necessary */
  2209. if (ruv_get_smallest_csn_for_replica((RUV *)object_get_data(r->repl_ruv),
  2210. r->repl_rid, &csn) == RUV_SUCCESS) {
  2211. csn_free(&csn);
  2212. r->min_csn_pl = NULL;
  2213. } else {
  2214. /*
  2215. * The local replica has not generated any of its own CSNs yet.
  2216. * We need to watch CSNs being generated and note the first
  2217. * locally-generated CSN that's committed. Once that event occurs,
  2218. * the RUV is suitable for iteration over locally generated
  2219. * changes.
  2220. */
  2221. r->min_csn_pl = csnplNew();
  2222. }
  2223. done:
  2224. if (NULL != pb) {
  2225. slapi_free_search_results_internal(pb);
  2226. slapi_pblock_destroy(pb);
  2227. }
  2228. if (return_value != 0) {
  2229. if (ruv)
  2230. ruv_destroy(&ruv);
  2231. }
  2232. return return_value;
  2233. }
  2234. /* NOTE - this is the only non-api function that performs locking because
  2235. it is called by the event queue */
  2236. void
  2237. replica_update_state(time_t when __attribute__((unused)), void *arg)
  2238. {
  2239. int rc;
  2240. const char *replica_name = (const char *)arg;
  2241. Object *replica_object = NULL;
  2242. Replica *r;
  2243. Slapi_Mod smod;
  2244. LDAPMod *mods[3];
  2245. Slapi_PBlock *pb = NULL;
  2246. char *dn = NULL;
  2247. struct berval *vals[2];
  2248. struct berval val;
  2249. LDAPMod mod;
  2250. if (NULL == replica_name)
  2251. return;
  2252. /*
  2253. * replica_get_by_name() will acquire the replica object
  2254. * and that could prevent the replica from being destroyed
  2255. * until the object_release is called.
  2256. */
  2257. replica_object = replica_get_by_name(replica_name);
  2258. if (NULL == replica_object) {
  2259. return;
  2260. }
  2261. /* We have a reference, so replica won't vanish on us. */
  2262. r = (Replica *)object_get_data(replica_object);
  2263. if (NULL == r) {
  2264. goto done;
  2265. }
  2266. replica_lock(r->repl_lock);
  2267. /* replica state is currently being updated
  2268. or no CSN was assigned - bail out */
  2269. if (r->state_update_inprogress) {
  2270. replica_unlock(r->repl_lock);
  2271. goto done;
  2272. }
  2273. /* This might be a consumer */
  2274. if (!r->repl_csn_assigned) {
  2275. /* EY: the consumer needs to flush ruv to disk. */
  2276. replica_unlock(r->repl_lock);
  2277. if (replica_write_ruv(r)) {
  2278. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2279. "replica_update_state - Failed write RUV for %s\n",
  2280. slapi_sdn_get_dn(r->repl_root));
  2281. }
  2282. goto done;
  2283. }
  2284. /* ONREPL update csn generator state of an updatable replica only */
  2285. /* ONREPL state always changes because we update time every second and
  2286. we write state to the disk less frequently */
  2287. rc = csngen_get_state((CSNGen *)object_get_data(r->repl_csngen), &smod);
  2288. if (rc != 0) {
  2289. replica_unlock(r->repl_lock);
  2290. goto done;
  2291. }
  2292. r->state_update_inprogress = PR_TRUE;
  2293. r->repl_csn_assigned = PR_FALSE;
  2294. dn = _replica_get_config_dn(r->repl_root);
  2295. if (NULL == dn) {
  2296. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  2297. "replica_update_state - Failed to get the config dn for %s\n",
  2298. slapi_sdn_get_dn(r->repl_root));
  2299. replica_unlock(r->repl_lock);
  2300. goto done;
  2301. }
  2302. pb = slapi_pblock_new();
  2303. mods[0] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod);
  2304. /* we don't want to held lock during operations since it causes lock contention
  2305. and sometimes deadlock. So releasing lock here */
  2306. replica_unlock(r->repl_lock);
  2307. /* replica repl_name and new_name attributes do not get changed once
  2308. the replica is configured - so it is ok that they are outside replica lock */
  2309. /* write replica name if it has not been written before */
  2310. if (r->new_name) {
  2311. mods[1] = &mod;
  2312. mod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  2313. mod.mod_type = (char *)attr_replicaName;
  2314. mod.mod_bvalues = vals;
  2315. vals[0] = &val;
  2316. vals[1] = NULL;
  2317. val.bv_val = r->repl_name;
  2318. val.bv_len = strlen(val.bv_val);
  2319. mods[2] = NULL;
  2320. } else {
  2321. mods[1] = NULL;
  2322. }
  2323. slapi_modify_internal_set_pb(pb, dn, mods, NULL, NULL,
  2324. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  2325. slapi_modify_internal_pb(pb);
  2326. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2327. if (rc != LDAP_SUCCESS) {
  2328. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_update_state - "
  2329. "Failed to update state of csn generator for replica %s: LDAP "
  2330. "error - %d\n",
  2331. slapi_sdn_get_dn(r->repl_root), rc);
  2332. } else {
  2333. r->new_name = PR_FALSE;
  2334. }
  2335. /* update RUV - performs its own locking */
  2336. if (replica_write_ruv(r)) {
  2337. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2338. "replica_update_state - Failed write RUV for %s\n",
  2339. slapi_sdn_get_dn(r->repl_root));
  2340. }
  2341. /* since this is the only place this value is changed and we are
  2342. guaranteed that only one thread enters the function, its ok
  2343. to change it outside replica lock */
  2344. r->state_update_inprogress = PR_FALSE;
  2345. slapi_ch_free((void **)&dn);
  2346. slapi_pblock_destroy(pb);
  2347. slapi_mod_done(&smod);
  2348. done:
  2349. if (replica_object)
  2350. object_release(replica_object);
  2351. }
  2352. int
  2353. replica_write_ruv(Replica *r)
  2354. {
  2355. int rc = LDAP_SUCCESS;
  2356. Slapi_Mod smod, rmod;
  2357. Slapi_Mod smod_last_modified;
  2358. LDAPMod *mods[4];
  2359. Slapi_PBlock *pb;
  2360. PR_ASSERT(r);
  2361. replica_lock(r->repl_lock);
  2362. if (!r->repl_ruv_dirty) {
  2363. replica_unlock(r->repl_lock);
  2364. return rc;
  2365. }
  2366. PR_ASSERT(r->repl_ruv);
  2367. ruv_to_smod((RUV *)object_get_data(r->repl_ruv), &smod);
  2368. ruv_last_modified_to_smod((RUV *)object_get_data(r->repl_ruv), &smod_last_modified);
  2369. replica_unlock(r->repl_lock);
  2370. mods[0] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod);
  2371. mods[1] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod_last_modified);
  2372. if (agmt_maxcsn_to_smod(r, &rmod) == LDAP_SUCCESS) {
  2373. mods[2] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&rmod);
  2374. } else {
  2375. mods[2] = NULL;
  2376. }
  2377. mods[3] = NULL;
  2378. pb = slapi_pblock_new();
  2379. /* replica name never changes so it is ok to reference it outside the lock */
  2380. slapi_modify_internal_set_pb_ext(
  2381. pb,
  2382. r->repl_root, /* only used to select be */
  2383. mods,
  2384. NULL, /* controls */
  2385. RUV_STORAGE_ENTRY_UNIQUEID,
  2386. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  2387. /* Add OP_FLAG_TOMBSTONE_ENTRY so that this doesn't get logged in the Retro ChangeLog */
  2388. OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | OP_FLAG_TOMBSTONE_ENTRY |
  2389. OP_FLAG_REPL_RUV);
  2390. slapi_modify_internal_pb(pb);
  2391. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2392. /* ruv does not exist - create one */
  2393. replica_lock(r->repl_lock);
  2394. if (rc == LDAP_SUCCESS) {
  2395. r->repl_ruv_dirty = PR_FALSE;
  2396. } else if (rc == LDAP_NO_SUCH_OBJECT) {
  2397. /* this includes an internal operation - but since this only happens
  2398. during server startup - its ok that we have lock around it */
  2399. rc = _replica_configure_ruv(r, PR_TRUE);
  2400. if (rc == 0)
  2401. r->repl_ruv_dirty = PR_FALSE;
  2402. } else /* error */
  2403. {
  2404. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2405. "replica_write_ruv - Failed to update RUV tombstone for %s; "
  2406. "LDAP error - %d\n",
  2407. slapi_sdn_get_dn(r->repl_root), rc);
  2408. }
  2409. replica_unlock(r->repl_lock);
  2410. slapi_mod_done(&smod);
  2411. slapi_mod_done(&rmod);
  2412. slapi_mod_done(&smod_last_modified);
  2413. slapi_pblock_destroy(pb);
  2414. return rc;
  2415. }
  2416. /* This routine figures out if an operation is for a replicated area and if so,
  2417. * pulls out the operation CSN and returns it through the smods parameter.
  2418. * It also informs the caller of the RUV entry's unique ID, since the caller
  2419. * may not have access to the macro in repl5.h. */
  2420. int
  2421. replica_ruv_smods_for_op(Slapi_PBlock *pb, char **uniqueid, Slapi_Mods **smods)
  2422. {
  2423. Object *replica_obj;
  2424. Object *ruv_obj;
  2425. Replica *replica;
  2426. RUV *ruv;
  2427. RUV *ruv_copy;
  2428. CSN *opcsn = NULL;
  2429. Slapi_Mod smod;
  2430. Slapi_Mod smod_last_modified;
  2431. Slapi_Operation *op;
  2432. Slapi_Entry *target_entry = NULL;
  2433. int rc = 0;
  2434. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &target_entry);
  2435. if (target_entry && is_ruv_tombstone_entry(target_entry)) {
  2436. /* disallow direct modification of the RUV tombstone entry
  2437. must use the CLEANRUV task instead */
  2438. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  2439. "replica_ruv_smods_for_op - Attempted to directly modify the tombstone RUV "
  2440. "entry [%s] - use the CLEANALLRUV task instead\n",
  2441. slapi_entry_get_dn_const(target_entry));
  2442. return (-1);
  2443. }
  2444. replica_obj = replica_get_replica_for_op(pb);
  2445. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  2446. if (NULL != replica_obj && NULL != op) {
  2447. opcsn = operation_get_csn(op);
  2448. }
  2449. /* If the op has no CSN then it's not in a replicated area, so we're done */
  2450. if (NULL == opcsn) {
  2451. return (0);
  2452. }
  2453. replica = (Replica *)object_get_data(replica_obj);
  2454. PR_ASSERT(replica);
  2455. ruv_obj = replica_get_ruv(replica);
  2456. PR_ASSERT(ruv_obj);
  2457. ruv = (RUV *)object_get_data(ruv_obj);
  2458. PR_ASSERT(ruv);
  2459. ruv_copy = ruv_dup(ruv);
  2460. object_release(ruv_obj);
  2461. object_release(replica_obj);
  2462. rc = ruv_set_max_csn_ext(ruv_copy, opcsn, NULL, PR_TRUE);
  2463. if (rc == RUV_COVERS_CSN) { /* change would "revert" RUV - ignored */
  2464. rc = 0; /* tell caller to ignore */
  2465. } else if (rc == RUV_SUCCESS) {
  2466. rc = 1; /* tell caller success */
  2467. } else { /* error */
  2468. rc = -1; /* tell caller error */
  2469. }
  2470. if (rc == 1) {
  2471. ruv_to_smod(ruv_copy, &smod);
  2472. ruv_last_modified_to_smod(ruv_copy, &smod_last_modified);
  2473. }
  2474. ruv_destroy(&ruv_copy);
  2475. if (rc == 1) {
  2476. *smods = slapi_mods_new();
  2477. slapi_mods_add_smod(*smods, &smod);
  2478. slapi_mods_add_smod(*smods, &smod_last_modified);
  2479. *uniqueid = slapi_ch_strdup(RUV_STORAGE_ENTRY_UNIQUEID);
  2480. } else {
  2481. *smods = NULL;
  2482. *uniqueid = NULL;
  2483. }
  2484. return rc;
  2485. }
  2486. const CSN *
  2487. entry_get_deletion_csn(Slapi_Entry *e)
  2488. {
  2489. const CSN *deletion_csn = NULL;
  2490. PR_ASSERT(NULL != e);
  2491. if (NULL != e) {
  2492. Slapi_Attr *oc_attr = NULL;
  2493. if (entry_attr_find_wsi(e, SLAPI_ATTR_OBJECTCLASS, &oc_attr) == ATTRIBUTE_PRESENT) {
  2494. Slapi_Value *tombstone_value = NULL;
  2495. struct berval v;
  2496. v.bv_val = SLAPI_ATTR_VALUE_TOMBSTONE;
  2497. v.bv_len = strlen(SLAPI_ATTR_VALUE_TOMBSTONE);
  2498. if (attr_value_find_wsi(oc_attr, &v, &tombstone_value) == VALUE_PRESENT) {
  2499. deletion_csn = value_get_csn(tombstone_value, CSN_TYPE_VALUE_UPDATED);
  2500. }
  2501. }
  2502. }
  2503. return deletion_csn;
  2504. }
  2505. static void
  2506. _delete_tombstone(const char *tombstone_dn, const char *uniqueid, int ext_op_flags)
  2507. {
  2508. PR_ASSERT(NULL != tombstone_dn && NULL != uniqueid);
  2509. if (NULL == tombstone_dn || NULL == uniqueid) {
  2510. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "_delete_tombstone - "
  2511. "NULL tombstone_dn or uniqueid provided.\n");
  2512. } else {
  2513. int ldaprc;
  2514. Slapi_PBlock *pb = slapi_pblock_new();
  2515. slapi_delete_internal_set_pb(pb, tombstone_dn, NULL, /* controls */
  2516. uniqueid, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  2517. OP_FLAG_TOMBSTONE_ENTRY | ext_op_flags);
  2518. slapi_delete_internal_pb(pb);
  2519. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ldaprc);
  2520. if (LDAP_SUCCESS != ldaprc) {
  2521. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  2522. "_delete_tombstone - Unable to delete tombstone %s, "
  2523. "uniqueid %s: %s.\n",
  2524. tombstone_dn, uniqueid,
  2525. ldap_err2string(ldaprc));
  2526. }
  2527. slapi_pblock_destroy(pb);
  2528. }
  2529. }
  2530. static void
  2531. get_reap_result(int rc, void *cb_data)
  2532. {
  2533. PR_ASSERT(cb_data);
  2534. ((reap_callback_data *)cb_data)->rc = rc;
  2535. }
  2536. static int
  2537. process_reap_entry(Slapi_Entry *entry, void *cb_data)
  2538. {
  2539. char deletion_csn_str[CSN_STRSIZE];
  2540. char purge_csn_str[CSN_STRSIZE];
  2541. unsigned long *num_entriesp = &((reap_callback_data *)cb_data)->num_entries;
  2542. unsigned long *num_purged_entriesp = &((reap_callback_data *)cb_data)->num_purged_entries;
  2543. CSN *purge_csn = ((reap_callback_data *)cb_data)->purge_csn;
  2544. /* this is a pointer into the actual value in the Replica object - so that
  2545. if the value is set in the replica, we will know about it immediately */
  2546. PRBool *tombstone_reap_stop = ((reap_callback_data *)cb_data)->tombstone_reap_stop;
  2547. const CSN *deletion_csn = NULL;
  2548. int rc = -1;
  2549. /* abort reaping if we've been told to stop or we're shutting down */
  2550. if (*tombstone_reap_stop || slapi_is_shutting_down()) {
  2551. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2552. "process_reap_entry - The tombstone reap process "
  2553. " has been stopped\n");
  2554. return rc;
  2555. }
  2556. /* we only ask for the objectclass in the search - the deletion csn is in the
  2557. objectclass attribute values - if we need more attributes returned by the
  2558. search in the future, see _replica_reap_tombstones below and add more to the
  2559. attrs array */
  2560. deletion_csn = entry_get_deletion_csn(entry);
  2561. if ((NULL == deletion_csn || csn_compare(deletion_csn, purge_csn) < 0) &&
  2562. (!is_ruv_tombstone_entry(entry))) {
  2563. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  2564. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2565. "process_reap_entry - Removing tombstone %s "
  2566. "because its deletion csn (%s) is less than the "
  2567. "purge csn (%s).\n",
  2568. slapi_entry_get_dn(entry),
  2569. csn_as_string(deletion_csn, PR_FALSE, deletion_csn_str),
  2570. csn_as_string(purge_csn, PR_FALSE, purge_csn_str));
  2571. }
  2572. if (slapi_entry_attr_get_ulong(entry, "tombstonenumsubordinates") < 1) {
  2573. _delete_tombstone(slapi_entry_get_dn(entry),
  2574. slapi_entry_get_uniqueid(entry), 0);
  2575. (*num_purged_entriesp)++;
  2576. }
  2577. } else {
  2578. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  2579. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2580. "process_reap_entry - NOT removing tombstone "
  2581. "%s\n",
  2582. slapi_entry_get_dn(entry));
  2583. }
  2584. }
  2585. if (!is_ruv_tombstone_entry(entry)) {
  2586. /* Don't update the count for the database tombstone entry */
  2587. (*num_entriesp)++;
  2588. }
  2589. return 0;
  2590. }
  2591. /* This does the actual work of searching for tombstones and deleting them.
  2592. This must be called in a separate thread because it may take a long time.
  2593. */
  2594. static void
  2595. _replica_reap_tombstones(void *arg)
  2596. {
  2597. const char *replica_name = (const char *)arg;
  2598. Slapi_PBlock *pb = NULL;
  2599. Object *replica_object = NULL;
  2600. Replica *replica = NULL;
  2601. CSN *purge_csn = NULL;
  2602. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2603. "_replica_reap_tombstones - Beginning tombstone reap for replica %s.\n",
  2604. replica_name ? replica_name : "(null)");
  2605. if (NULL == replica_name) {
  2606. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2607. "_replica_reap_tombstones - Replica name is null in tombstone reap\n");
  2608. goto done;
  2609. }
  2610. /*
  2611. * replica_get_by_name() will acquire the replica object
  2612. * and that could prevent the replica from being destroyed
  2613. * until the object_release is called.
  2614. */
  2615. replica_object = replica_get_by_name(replica_name);
  2616. if (NULL == replica_object) {
  2617. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2618. "_replica_reap_tombstones - Replica object %s is null in tombstone reap\n", replica_name);
  2619. goto done;
  2620. }
  2621. /* We have a reference, so replica won't vanish on us. */
  2622. replica = (Replica *)object_get_data(replica_object);
  2623. if (NULL == replica) {
  2624. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2625. "_replica_reap_tombstones - Replica %s is null in tombstone reap\n", replica_name);
  2626. goto done;
  2627. }
  2628. if (replica->tombstone_reap_stop) {
  2629. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2630. "_replica_reap_tombstones - Replica %s reap stop flag is set for tombstone reap\n", replica_name);
  2631. goto done;
  2632. }
  2633. purge_csn = replica_get_purge_csn(replica);
  2634. if (NULL != purge_csn) {
  2635. LDAPControl **ctrls;
  2636. reap_callback_data cb_data;
  2637. char deletion_csn_str[CSN_STRSIZE];
  2638. char tombstone_filter[128];
  2639. char **attrs = NULL;
  2640. int oprc;
  2641. if (replica_get_precise_purging(replica)) {
  2642. /*
  2643. * Using precise tombstone purging. Create filter to lookup the exact
  2644. * entries that need to be purged by using a range search on the new
  2645. * tombstone csn index.
  2646. */
  2647. csn_as_string(purge_csn, PR_FALSE, deletion_csn_str);
  2648. PR_snprintf(tombstone_filter, 128,
  2649. "(&(%s<=%s)(objectclass=nsTombstone))", SLAPI_ATTR_TOMBSTONE_CSN,
  2650. csn_as_string(purge_csn, PR_FALSE, deletion_csn_str));
  2651. } else {
  2652. /* Use the old inefficient filter */
  2653. PR_snprintf(tombstone_filter, 128, "(objectclass=nsTombstone)");
  2654. }
  2655. /* we just need the objectclass - for the deletion csn
  2656. and the dn and nsuniqueid - for possible deletion
  2657. and tombstonenumsubordinates to check if it has numsubordinates
  2658. saves time to return only 3 attrs
  2659. */
  2660. charray_add(&attrs, slapi_ch_strdup("objectclass"));
  2661. charray_add(&attrs, slapi_ch_strdup("nsuniqueid"));
  2662. charray_add(&attrs, slapi_ch_strdup("tombstonenumsubordinates"));
  2663. charray_add(&attrs, slapi_ch_strdup(SLAPI_ATTR_TOMBSTONE_CSN));
  2664. ctrls = (LDAPControl **)slapi_ch_calloc(3, sizeof(LDAPControl *));
  2665. ctrls[0] = create_managedsait_control();
  2666. ctrls[1] = create_backend_control(replica->repl_root);
  2667. ctrls[2] = NULL;
  2668. pb = slapi_pblock_new();
  2669. slapi_search_internal_set_pb(pb, slapi_sdn_get_dn(replica->repl_root),
  2670. LDAP_SCOPE_SUBTREE, tombstone_filter,
  2671. attrs, 0, ctrls, NULL,
  2672. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  2673. OP_FLAG_REVERSE_CANDIDATE_ORDER);
  2674. cb_data.rc = 0;
  2675. cb_data.num_entries = 0UL;
  2676. cb_data.num_purged_entries = 0UL;
  2677. cb_data.purge_csn = purge_csn;
  2678. /* set the cb data pointer to point to the actual memory address in
  2679. the actual Replica object - so that when the value in the Replica
  2680. is set, the reap process will know about it immediately */
  2681. cb_data.tombstone_reap_stop = &(replica->tombstone_reap_stop);
  2682. slapi_search_internal_callback_pb(pb, &cb_data /* callback data */,
  2683. get_reap_result /* result callback */,
  2684. process_reap_entry /* entry callback */,
  2685. NULL /* referral callback*/);
  2686. charray_free(attrs);
  2687. oprc = cb_data.rc;
  2688. if (LDAP_SUCCESS != oprc) {
  2689. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  2690. "_replica_reap_tombstones - Failed when searching for "
  2691. "tombstones in replica %s: %s. Will try again in %ld "
  2692. "seconds.\n",
  2693. slapi_sdn_get_dn(replica->repl_root),
  2694. ldap_err2string(oprc), replica->tombstone_reap_interval);
  2695. } else {
  2696. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2697. "_replica_reap_tombstones - Purged %ld of %ld tombstones "
  2698. "in replica %s. Will try again in %ld "
  2699. "seconds.\n",
  2700. cb_data.num_purged_entries, cb_data.num_entries,
  2701. slapi_sdn_get_dn(replica->repl_root),
  2702. replica->tombstone_reap_interval);
  2703. }
  2704. } else {
  2705. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2706. "_replica_reap_tombstones - No purge CSN for tombstone reap for replica %s.\n",
  2707. replica_name);
  2708. }
  2709. done:
  2710. if (replica) {
  2711. replica_lock(replica->repl_lock);
  2712. replica->tombstone_reap_active = PR_FALSE;
  2713. replica_unlock(replica->repl_lock);
  2714. }
  2715. if (NULL != purge_csn) {
  2716. csn_free(&purge_csn);
  2717. }
  2718. if (NULL != pb) {
  2719. slapi_free_search_results_internal(pb);
  2720. slapi_pblock_destroy(pb);
  2721. }
  2722. if (NULL != replica_object) {
  2723. object_release(replica_object);
  2724. replica_object = NULL;
  2725. replica = NULL;
  2726. }
  2727. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  2728. "_replica_reap_tombstones - Finished tombstone reap for replica %s.\n",
  2729. replica_name ? replica_name : "(null)");
  2730. }
  2731. /*
  2732. We don't want to run the reaper function directly from the event
  2733. queue since it may hog the event queue, starving other events.
  2734. See bug 604441
  2735. The function eq_cb_reap_tombstones will fire off the actual thread
  2736. that does the real work.
  2737. */
  2738. static void
  2739. eq_cb_reap_tombstones(time_t when __attribute__((unused)), void *arg)
  2740. {
  2741. const char *replica_name = (const char *)arg;
  2742. Object *replica_object = NULL;
  2743. Replica *replica = NULL;
  2744. if (NULL != replica_name) {
  2745. /*
  2746. * replica_get_by_name() will acquire the replica object
  2747. * and that could prevent the replica from being destroyed
  2748. * until the object_release is called.
  2749. */
  2750. replica_object = replica_get_by_name(replica_name);
  2751. if (NULL != replica_object) {
  2752. /* We have a reference, so replica won't vanish on us. */
  2753. replica = (Replica *)object_get_data(replica_object);
  2754. if (replica) {
  2755. replica_lock(replica->repl_lock);
  2756. /* No action if purge is disabled or the previous purge is not done yet */
  2757. if (replica->tombstone_reap_interval != 0 &&
  2758. replica->tombstone_reap_active == PR_FALSE) {
  2759. /* set the flag here to minimize race conditions */
  2760. replica->tombstone_reap_active = PR_TRUE;
  2761. if (PR_CreateThread(PR_USER_THREAD,
  2762. _replica_reap_tombstones, (void *)replica_name,
  2763. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD,
  2764. SLAPD_DEFAULT_THREAD_STACKSIZE) == NULL) {
  2765. replica->tombstone_reap_active = PR_FALSE;
  2766. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  2767. "eq_cb_reap_tombstones - Unable to create the tombstone reap thread for replica %s. "
  2768. "Possible system resources problem\n",
  2769. replica_name);
  2770. }
  2771. }
  2772. /* reap thread will wait until this lock is released */
  2773. replica_unlock(replica->repl_lock);
  2774. }
  2775. object_release(replica_object);
  2776. replica_object = NULL;
  2777. replica = NULL;
  2778. }
  2779. }
  2780. }
  2781. static char *
  2782. _replica_type_as_string(const Replica *r)
  2783. {
  2784. switch (r->repl_type) {
  2785. case REPLICA_TYPE_PRIMARY:
  2786. return "primary";
  2787. case REPLICA_TYPE_READONLY:
  2788. return "read-only";
  2789. case REPLICA_TYPE_UPDATABLE:
  2790. return "updatable";
  2791. default:
  2792. return "unknown";
  2793. }
  2794. }
  2795. static const char *root_glue =
  2796. "dn: %s\n"
  2797. "objectclass: top\n"
  2798. "objectclass: nsTombstone\n"
  2799. "objectclass: extensibleobject\n"
  2800. "nsuniqueid: %s\n";
  2801. static int
  2802. replica_create_ruv_tombstone(Replica *r)
  2803. {
  2804. int return_value = LDAP_LOCAL_ERROR;
  2805. char *root_entry_str;
  2806. Slapi_Entry *e = NULL;
  2807. const char *purl = NULL;
  2808. RUV *ruv;
  2809. struct berval **bvals = NULL;
  2810. Slapi_PBlock *pb = NULL;
  2811. int rc;
  2812. PR_ASSERT(NULL != r && NULL != r->repl_root);
  2813. root_entry_str = slapi_ch_smprintf(root_glue, slapi_sdn_get_ndn(r->repl_root), RUV_STORAGE_ENTRY_UNIQUEID);
  2814. e = slapi_str2entry(root_entry_str, SLAPI_STR2ENTRY_TOMBSTONE_CHECK);
  2815. if (e == NULL)
  2816. goto done;
  2817. /* Add ruv */
  2818. if (r->repl_ruv == NULL) {
  2819. CSNGen *gen;
  2820. CSN *csn;
  2821. char csnstr[CSN_STRSIZE];
  2822. /* first attempt to write RUV tombstone - need to create RUV */
  2823. gen = (CSNGen *)object_get_data(r->repl_csngen);
  2824. PR_ASSERT(gen);
  2825. if (csngen_new_csn(gen, &csn, PR_FALSE /* notify */) == CSN_SUCCESS) {
  2826. (void)csn_as_string(csn, PR_FALSE, csnstr);
  2827. csn_free(&csn);
  2828. /*
  2829. * if this is an updateable replica - add its own
  2830. * element to the RUV so that referrals work correctly
  2831. */
  2832. if (r->repl_type == REPLICA_TYPE_UPDATABLE)
  2833. purl = multimaster_get_local_purl();
  2834. if (ruv_init_new(csnstr, r->repl_rid, purl, &ruv) == RUV_SUCCESS) {
  2835. r->repl_ruv = object_new((void *)ruv, (FNFree)ruv_destroy);
  2836. r->repl_ruv_dirty = PR_TRUE;
  2837. return_value = LDAP_SUCCESS;
  2838. } else {
  2839. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_create_ruv_tombstone - "
  2840. "Cannot create new replica update vector for %s\n",
  2841. slapi_sdn_get_dn(r->repl_root));
  2842. ruv_destroy(&ruv);
  2843. goto done;
  2844. }
  2845. } else {
  2846. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_create_ruv_tombstone - "
  2847. "Cannot obtain CSN for new replica update vector for %s\n",
  2848. slapi_sdn_get_dn(r->repl_root));
  2849. csn_free(&csn);
  2850. goto done;
  2851. }
  2852. } else { /* failed to write the entry because DB was not initialized - retry */
  2853. ruv = (RUV *)object_get_data(r->repl_ruv);
  2854. PR_ASSERT(ruv);
  2855. }
  2856. PR_ASSERT(r->repl_ruv);
  2857. rc = ruv_to_bervals(ruv, &bvals);
  2858. if (rc != RUV_SUCCESS) {
  2859. goto done;
  2860. }
  2861. /* ONREPL this is depricated function but there is currently no better API to use */
  2862. rc = slapi_entry_add_values(e, type_ruvElement, bvals);
  2863. if (rc != 0) {
  2864. goto done;
  2865. }
  2866. pb = slapi_pblock_new();
  2867. slapi_add_entry_internal_set_pb(pb, e, NULL /* controls */, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  2868. OP_FLAG_TOMBSTONE_ENTRY | OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | OP_FLAG_REPL_RUV);
  2869. slapi_add_internal_pb(pb);
  2870. e = NULL; /* add consumes e, upon success or failure */
  2871. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
  2872. if (return_value == LDAP_SUCCESS)
  2873. r->repl_ruv_dirty = PR_FALSE;
  2874. done:
  2875. slapi_entry_free(e);
  2876. if (bvals)
  2877. ber_bvecfree(bvals);
  2878. if (pb)
  2879. slapi_pblock_destroy(pb);
  2880. slapi_ch_free_string(&root_entry_str);
  2881. return return_value;
  2882. }
  2883. static void
  2884. assign_csn_callback(const CSN *csn, void *data)
  2885. {
  2886. Replica *r = (Replica *)data;
  2887. Object *ruv_obj;
  2888. RUV *ruv;
  2889. PR_ASSERT(NULL != csn);
  2890. PR_ASSERT(NULL != r);
  2891. ruv_obj = replica_get_ruv(r);
  2892. PR_ASSERT(ruv_obj);
  2893. ruv = (RUV *)object_get_data(ruv_obj);
  2894. PR_ASSERT(ruv);
  2895. replica_lock(r->repl_lock);
  2896. r->repl_csn_assigned = PR_TRUE;
  2897. if (NULL != r->min_csn_pl) {
  2898. if (csnplInsert(r->min_csn_pl, csn, NULL) != 0) {
  2899. char csn_str[CSN_STRSIZE]; /* For logging only */
  2900. /* Ack, we can't keep track of min csn. Punt. */
  2901. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  2902. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "assign_csn_callback - "
  2903. "Failed to insert csn %s for replica %s\n",
  2904. csn_as_string(csn, PR_FALSE, csn_str),
  2905. slapi_sdn_get_dn(r->repl_root));
  2906. }
  2907. csnplFree(&r->min_csn_pl);
  2908. }
  2909. }
  2910. ruv_add_csn_inprogress(r, ruv, csn);
  2911. replica_unlock(r->repl_lock);
  2912. object_release(ruv_obj);
  2913. }
  2914. static void
  2915. abort_csn_callback(const CSN *csn, void *data)
  2916. {
  2917. Replica *r = (Replica *)data;
  2918. Object *ruv_obj;
  2919. RUV *ruv;
  2920. PR_ASSERT(NULL != csn);
  2921. PR_ASSERT(NULL != data);
  2922. ruv_obj = replica_get_ruv(r);
  2923. PR_ASSERT(ruv_obj);
  2924. ruv = (RUV *)object_get_data(ruv_obj);
  2925. PR_ASSERT(ruv);
  2926. replica_lock(r->repl_lock);
  2927. if (NULL != r->min_csn_pl) {
  2928. int rc = csnplRemove(r->min_csn_pl, csn);
  2929. if (rc) {
  2930. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "abort_csn_callback - csnplRemove failed\n");
  2931. replica_unlock(r->repl_lock);
  2932. return;
  2933. }
  2934. }
  2935. ruv_cancel_csn_inprogress(r, ruv, csn, replica_get_rid(r));
  2936. replica_unlock(r->repl_lock);
  2937. object_release(ruv_obj);
  2938. }
  2939. static CSN *
  2940. _replica_get_purge_csn_nolock(const Replica *r)
  2941. {
  2942. CSN *purge_csn = NULL;
  2943. CSN **csns = NULL;
  2944. RUV *ruv;
  2945. int i;
  2946. if (r->repl_purge_delay > 0) {
  2947. /* get a sorted list of all maxcsns in ruv in ascend order */
  2948. object_acquire(r->repl_ruv);
  2949. ruv = object_get_data(r->repl_ruv);
  2950. csns = cl5BuildCSNList(ruv, NULL);
  2951. object_release(r->repl_ruv);
  2952. if (csns == NULL)
  2953. return NULL;
  2954. /* locate the most recent maxcsn in the csn list */
  2955. for (i = 0; csns[i]; i++)
  2956. ;
  2957. purge_csn = csn_dup(csns[i - 1]);
  2958. /* set purge_csn to the most recent maxcsn - purge_delay */
  2959. if ((csn_get_time(purge_csn) - r->repl_purge_delay) > 0)
  2960. csn_set_time(purge_csn, csn_get_time(purge_csn) - r->repl_purge_delay);
  2961. }
  2962. if (csns)
  2963. cl5DestroyCSNList(&csns);
  2964. return purge_csn;
  2965. }
  2966. static void
  2967. replica_get_referrals_nolock(const Replica *r, char ***referrals)
  2968. {
  2969. if (referrals != NULL) {
  2970. int hint;
  2971. int i = 0;
  2972. Slapi_Value *v = NULL;
  2973. if (NULL == r->repl_referral) {
  2974. *referrals = NULL;
  2975. } else {
  2976. /* richm: +1 for trailing NULL */
  2977. *referrals = (char **)slapi_ch_calloc(sizeof(char *), 1 + slapi_valueset_count(r->repl_referral));
  2978. hint = slapi_valueset_first_value(r->repl_referral, &v);
  2979. while (v != NULL) {
  2980. const char *s = slapi_value_get_string(v);
  2981. if (s != NULL && s[0] != '\0') {
  2982. (*referrals)[i] = slapi_ch_strdup(s);
  2983. i++;
  2984. }
  2985. hint = slapi_valueset_next_value(r->repl_referral, hint, &v);
  2986. }
  2987. (*referrals)[i] = NULL;
  2988. }
  2989. }
  2990. }
  2991. typedef struct replinfo
  2992. {
  2993. char *repl_gen;
  2994. char *repl_name;
  2995. } replinfo;
  2996. static int
  2997. replica_log_start_iteration(const ruv_enum_data *rid_data, void *data)
  2998. {
  2999. int rc = 0;
  3000. replinfo *r_info = (replinfo *)data;
  3001. slapi_operation_parameters op_params;
  3002. if (rid_data->csn == NULL)
  3003. return 0;
  3004. memset(&op_params, 0, sizeof(op_params));
  3005. op_params.operation_type = SLAPI_OPERATION_DELETE;
  3006. op_params.target_address.sdn = slapi_sdn_new_ndn_byval(START_ITERATION_ENTRY_DN);
  3007. op_params.target_address.uniqueid = START_ITERATION_ENTRY_UNIQUEID;
  3008. op_params.csn = csn_dup(rid_data->csn);
  3009. rc = cl5WriteOperation(r_info->repl_name, r_info->repl_gen, &op_params, PR_FALSE);
  3010. if (rc == CL5_SUCCESS)
  3011. rc = 0;
  3012. else
  3013. rc = -1;
  3014. slapi_sdn_free(&op_params.target_address.sdn);
  3015. csn_free(&op_params.csn);
  3016. return rc;
  3017. }
  3018. static int
  3019. replica_log_ruv_elements_nolock(const Replica *r)
  3020. {
  3021. int rc = 0;
  3022. RUV *ruv;
  3023. char *repl_gen;
  3024. replinfo r_info;
  3025. ruv = (RUV *)object_get_data(r->repl_ruv);
  3026. PR_ASSERT(ruv);
  3027. /* we log it as a delete operation to have the least number of fields
  3028. to set. the entry can be identified by a special target uniqueid and
  3029. special target dn */
  3030. repl_gen = ruv_get_replica_generation(ruv);
  3031. r_info.repl_name = r->repl_name;
  3032. r_info.repl_gen = repl_gen;
  3033. rc = ruv_enumerate_elements(ruv, replica_log_start_iteration, &r_info);
  3034. slapi_ch_free((void **)&repl_gen);
  3035. return rc;
  3036. }
  3037. void
  3038. replica_set_purge_delay(Replica *r, PRUint32 purge_delay)
  3039. {
  3040. PR_ASSERT(r);
  3041. replica_lock(r->repl_lock);
  3042. r->repl_purge_delay = purge_delay;
  3043. replica_unlock(r->repl_lock);
  3044. }
  3045. void
  3046. replica_set_tombstone_reap_interval(Replica *r, long interval)
  3047. {
  3048. replica_lock(r->repl_lock);
  3049. /*
  3050. * Leave the event there to purge the existing tombstones
  3051. * if we are about to turn off tombstone creation
  3052. */
  3053. if (interval > 0 && r->repl_eqcxt_tr && r->tombstone_reap_interval != interval) {
  3054. int found;
  3055. found = slapi_eq_cancel(r->repl_eqcxt_tr);
  3056. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  3057. "replica_set_tombstone_reap_interval - tombstone_reap event (interval=%ld) was %s\n",
  3058. r->tombstone_reap_interval, (found ? "cancelled" : "not found"));
  3059. r->repl_eqcxt_tr = NULL;
  3060. }
  3061. r->tombstone_reap_interval = interval;
  3062. if (interval > 0 && r->repl_eqcxt_tr == NULL) {
  3063. r->repl_eqcxt_tr = slapi_eq_repeat(eq_cb_reap_tombstones, r->repl_name,
  3064. slapi_current_utc_time() + r->tombstone_reap_interval,
  3065. 1000 * r->tombstone_reap_interval);
  3066. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  3067. "replica_set_tombstone_reap_interval - tombstone_reap event (interval=%ld) was %s\n",
  3068. r->tombstone_reap_interval, (r->repl_eqcxt_tr ? "scheduled" : "not scheduled successfully"));
  3069. }
  3070. replica_unlock(r->repl_lock);
  3071. }
  3072. static void
  3073. replica_strip_cleaned_rids(Replica *r)
  3074. {
  3075. Object *RUVObj;
  3076. RUV *ruv = NULL;
  3077. ReplicaId rid[32] = {0};
  3078. int i = 0;
  3079. RUVObj = replica_get_ruv(r);
  3080. ruv = (RUV *)object_get_data(RUVObj);
  3081. ruv_get_cleaned_rids(ruv, rid);
  3082. while (rid[i] != 0) {
  3083. ruv_delete_replica(ruv, rid[i]);
  3084. replica_set_ruv_dirty(r);
  3085. if (replica_write_ruv(r)) {
  3086. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  3087. "replica_strip_cleaned_rids - Failed to write RUV\n");
  3088. }
  3089. i++;
  3090. }
  3091. object_release(RUVObj);
  3092. }
  3093. /* Update the tombstone entry to reflect the content of the ruv */
  3094. static void
  3095. replica_replace_ruv_tombstone(Replica *r)
  3096. {
  3097. Slapi_PBlock *pb = NULL;
  3098. Slapi_Mod smod;
  3099. Slapi_Mod smod_last_modified;
  3100. LDAPMod *mods[3];
  3101. char *dn;
  3102. int rc;
  3103. PR_ASSERT(NULL != r && NULL != r->repl_root);
  3104. replica_strip_cleaned_rids(r);
  3105. replica_lock(r->repl_lock);
  3106. PR_ASSERT(r->repl_ruv);
  3107. ruv_to_smod((RUV *)object_get_data(r->repl_ruv), &smod);
  3108. ruv_last_modified_to_smod((RUV *)object_get_data(r->repl_ruv), &smod_last_modified);
  3109. dn = _replica_get_config_dn(r->repl_root);
  3110. if (NULL == dn) {
  3111. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  3112. "replica_replace_ruv_tombstone - "
  3113. "Failed to get the config dn for %s\n",
  3114. slapi_sdn_get_dn(r->repl_root));
  3115. replica_unlock(r->repl_lock);
  3116. goto bail;
  3117. }
  3118. mods[0] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod);
  3119. mods[1] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod_last_modified);
  3120. replica_unlock(r->repl_lock);
  3121. mods[2] = NULL;
  3122. pb = slapi_pblock_new();
  3123. slapi_modify_internal_set_pb_ext(
  3124. pb,
  3125. r->repl_root, /* only used to select be */
  3126. mods,
  3127. NULL, /* controls */
  3128. RUV_STORAGE_ENTRY_UNIQUEID,
  3129. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  3130. OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | OP_FLAG_REPL_RUV | OP_FLAG_TOMBSTONE_ENTRY);
  3131. slapi_modify_internal_pb(pb);
  3132. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3133. if (rc != LDAP_SUCCESS) {
  3134. if ((rc != LDAP_NO_SUCH_OBJECT && rc != LDAP_TYPE_OR_VALUE_EXISTS) || !replica_is_state_flag_set(r, REPLICA_IN_USE)) {
  3135. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name, "replica_replace_ruv_tombstone - "
  3136. "Failed to update replication update vector for replica %s: LDAP "
  3137. "error - %d\n",
  3138. (char *)slapi_sdn_get_dn(r->repl_root), rc);
  3139. }
  3140. }
  3141. slapi_ch_free((void **)&dn);
  3142. slapi_pblock_destroy(pb);
  3143. bail:
  3144. slapi_mod_done(&smod);
  3145. slapi_mod_done(&smod_last_modified);
  3146. }
  3147. void
  3148. replica_update_ruv_consumer(Replica *r, RUV *supplier_ruv)
  3149. {
  3150. ReplicaId supplier_id = 0;
  3151. char *supplier_purl = NULL;
  3152. if (ruv_get_first_id_and_purl(supplier_ruv, &supplier_id, &supplier_purl) == RUV_SUCCESS) {
  3153. RUV *local_ruv = NULL;
  3154. replica_lock(r->repl_lock);
  3155. local_ruv = (RUV *)object_get_data(r->repl_ruv);
  3156. if (is_cleaned_rid(supplier_id) || local_ruv == NULL) {
  3157. replica_unlock(r->repl_lock);
  3158. return;
  3159. }
  3160. if (ruv_local_contains_supplier(local_ruv, supplier_id) == 0) {
  3161. if (r->repl_type == REPLICA_TYPE_UPDATABLE) {
  3162. /* Add the new ruv right after the consumer own purl */
  3163. ruv_add_index_replica(local_ruv, supplier_id, supplier_purl, 2);
  3164. } else {
  3165. /* This is a consumer only, add it first */
  3166. ruv_add_index_replica(local_ruv, supplier_id, supplier_purl, 1);
  3167. }
  3168. } else {
  3169. /* Replace it */
  3170. ruv_replace_replica_purl(local_ruv, supplier_id, supplier_purl);
  3171. }
  3172. replica_unlock(r->repl_lock);
  3173. /* Update also the directory entry */
  3174. replica_replace_ruv_tombstone(r);
  3175. }
  3176. }
  3177. void
  3178. replica_set_ruv_dirty(Replica *r)
  3179. {
  3180. PR_ASSERT(r);
  3181. replica_lock(r->repl_lock);
  3182. r->repl_ruv_dirty = PR_TRUE;
  3183. replica_unlock(r->repl_lock);
  3184. }
  3185. PRBool
  3186. replica_is_state_flag_set(Replica *r, PRInt32 flag)
  3187. {
  3188. PR_ASSERT(r);
  3189. if (r)
  3190. return (r->repl_state_flags & flag);
  3191. else
  3192. return PR_FALSE;
  3193. }
  3194. void
  3195. replica_set_state_flag(Replica *r, PRUint32 flag, PRBool clear)
  3196. {
  3197. if (r == NULL)
  3198. return;
  3199. replica_lock(r->repl_lock);
  3200. if (clear) {
  3201. r->repl_state_flags &= ~flag;
  3202. } else {
  3203. r->repl_state_flags |= flag;
  3204. }
  3205. replica_unlock(r->repl_lock);
  3206. }
  3207. /**
  3208. * Use this to tell the tombstone reap process to stop. This will
  3209. * typically be used when we (consumer) get a request to do a
  3210. * total update.
  3211. */
  3212. void
  3213. replica_set_tombstone_reap_stop(Replica *r, PRBool val)
  3214. {
  3215. if (r == NULL)
  3216. return;
  3217. replica_lock(r->repl_lock);
  3218. r->tombstone_reap_stop = val;
  3219. replica_unlock(r->repl_lock);
  3220. }
  3221. /* replica just came back online, probably after data was reloaded */
  3222. void
  3223. replica_enable_replication(Replica *r)
  3224. {
  3225. int rc;
  3226. PR_ASSERT(r);
  3227. /* prevent creation of new agreements until the replica is enabled */
  3228. PR_Lock(r->agmt_lock);
  3229. /* retrieve new ruv */
  3230. rc = replica_reload_ruv(r);
  3231. if (rc) {
  3232. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name, "replica_enable_replication - "
  3233. "Reloading ruv failed\n");
  3234. /* What to do ? */
  3235. }
  3236. /* Replica came back online, Check if the total update was terminated.
  3237. If flag is still set, it was not terminated, therefore the data is
  3238. very likely to be incorrect, and we should not restart Replication threads...
  3239. */
  3240. if (!replica_is_state_flag_set(r, REPLICA_TOTAL_IN_PROGRESS)) {
  3241. /* restart outbound replication */
  3242. start_agreements_for_replica(r, PR_TRUE);
  3243. /* enable ruv state update */
  3244. replica_set_enabled(r, PR_TRUE);
  3245. }
  3246. /* mark the replica as being available for updates */
  3247. replica_relinquish_exclusive_access(r, 0, 0);
  3248. replica_set_state_flag(r, REPLICA_AGREEMENTS_DISABLED, PR_TRUE /* clear */);
  3249. PR_Unlock(r->agmt_lock);
  3250. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "replica_enable_replication - "
  3251. "Replica %s is relinquished\n",
  3252. slapi_sdn_get_ndn(replica_get_root(r)));
  3253. }
  3254. /* replica is about to be taken offline */
  3255. void
  3256. replica_disable_replication(Replica *r, Object *r_obj __attribute__((unused)))
  3257. {
  3258. char *current_purl = NULL;
  3259. char *p_locking_purl = NULL;
  3260. char *locking_purl = NULL;
  3261. ReplicaId junkrid;
  3262. PRBool isInc = PR_FALSE; /* get exclusive access, but not for inc update */
  3263. RUV *repl_ruv = NULL;
  3264. /* prevent creation of new agreements until the replica is disabled */
  3265. PR_Lock(r->agmt_lock);
  3266. /* stop ruv update */
  3267. replica_set_enabled(r, PR_FALSE);
  3268. /* disable outbound replication */
  3269. start_agreements_for_replica(r, PR_FALSE);
  3270. /* close the corresponding changelog file */
  3271. /* close_changelog_for_replica (r_obj); */
  3272. /* mark the replica as being unavailable for updates */
  3273. /* If an incremental update is in progress, we want to wait until it is
  3274. finished until we get exclusive access to the replica, because we have
  3275. to make sure no operations are in progress - it messes up replication
  3276. when a restore is in progress but we are still adding replicated entries
  3277. from a supplier
  3278. */
  3279. repl_ruv = (RUV *)object_get_data(r->repl_ruv);
  3280. ruv_get_first_id_and_purl(repl_ruv, &junkrid, &p_locking_purl);
  3281. locking_purl = slapi_ch_strdup(p_locking_purl);
  3282. p_locking_purl = NULL;
  3283. repl_ruv = NULL;
  3284. while (!replica_get_exclusive_access(r, &isInc, 0, 0, "replica_disable_replication",
  3285. &current_purl)) {
  3286. if (!isInc) /* already locked, but not by inc update - break */
  3287. break;
  3288. isInc = PR_FALSE;
  3289. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  3290. "replica_disable_replication - "
  3291. "replica %s is already locked by (%s) for incoming "
  3292. "incremental update; sleeping 100ms\n",
  3293. slapi_sdn_get_ndn(replica_get_root(r)),
  3294. current_purl ? current_purl : "unknown");
  3295. slapi_ch_free_string(&current_purl);
  3296. DS_Sleep(PR_MillisecondsToInterval(100));
  3297. }
  3298. slapi_ch_free_string(&current_purl);
  3299. slapi_ch_free_string(&locking_purl);
  3300. replica_set_state_flag(r, REPLICA_AGREEMENTS_DISABLED, PR_FALSE);
  3301. PR_Unlock(r->agmt_lock);
  3302. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "replica_disable_replication - "
  3303. "replica %s is acquired\n",
  3304. slapi_sdn_get_ndn(replica_get_root(r)));
  3305. }
  3306. static void
  3307. start_agreements_for_replica(Replica *r, PRBool start)
  3308. {
  3309. Object *agmt_obj;
  3310. Repl_Agmt *agmt;
  3311. agmt_obj = agmtlist_get_first_agreement_for_replica(r);
  3312. while (agmt_obj) {
  3313. agmt = (Repl_Agmt *)object_get_data(agmt_obj);
  3314. PR_ASSERT(agmt);
  3315. if (agmt_is_enabled(agmt)) {
  3316. if (start)
  3317. agmt_start(agmt);
  3318. else /* stop */
  3319. agmt_stop(agmt);
  3320. }
  3321. agmt_obj = agmtlist_get_next_agreement_for_replica(r, agmt_obj);
  3322. }
  3323. }
  3324. int
  3325. replica_start_agreement(Replica *r, Repl_Agmt *ra)
  3326. {
  3327. int ret = 0;
  3328. if (r == NULL)
  3329. return -1;
  3330. PR_Lock(r->agmt_lock);
  3331. if (!replica_is_state_flag_set(r, REPLICA_AGREEMENTS_DISABLED) && agmt_is_enabled(ra)) {
  3332. ret = agmt_start(ra); /* Start the replication agreement */
  3333. }
  3334. PR_Unlock(r->agmt_lock);
  3335. return ret;
  3336. }
  3337. int
  3338. windows_replica_start_agreement(Replica *r, Repl_Agmt *ra)
  3339. {
  3340. int ret = 0;
  3341. if (r == NULL)
  3342. return -1;
  3343. PR_Lock(r->agmt_lock);
  3344. if (!replica_is_state_flag_set(r, REPLICA_AGREEMENTS_DISABLED)) {
  3345. ret = windows_agmt_start(ra); /* Start the replication agreement */
  3346. /* ret = windows_agmt_start(ra); Start the replication agreement */
  3347. }
  3348. PR_Unlock(r->agmt_lock);
  3349. return ret;
  3350. }
  3351. /*
  3352. * A callback function registered as op->o_csngen_handler and
  3353. * called by backend ops to generate opcsn.
  3354. */
  3355. CSN *
  3356. replica_generate_next_csn(Slapi_PBlock *pb, const CSN *basecsn)
  3357. {
  3358. CSN *opcsn = NULL;
  3359. Object *replica_obj;
  3360. replica_obj = replica_get_replica_for_op(pb);
  3361. if (NULL != replica_obj) {
  3362. Replica *replica = (Replica *)object_get_data(replica_obj);
  3363. if (NULL != replica) {
  3364. Slapi_Operation *op;
  3365. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  3366. if (replica->repl_type != REPLICA_TYPE_READONLY) {
  3367. Object *gen_obj = replica_get_csngen(replica);
  3368. if (NULL != gen_obj) {
  3369. CSNGen *gen = (CSNGen *)object_get_data(gen_obj);
  3370. if (NULL != gen) {
  3371. /* The new CSN should be greater than the base CSN */
  3372. csngen_new_csn(gen, &opcsn, PR_FALSE /* don't notify */);
  3373. if (csn_compare(opcsn, basecsn) <= 0) {
  3374. char opcsnstr[CSN_STRSIZE], basecsnstr[CSN_STRSIZE];
  3375. char opcsn2str[CSN_STRSIZE];
  3376. csn_as_string(opcsn, PR_FALSE, opcsnstr);
  3377. csn_as_string(basecsn, PR_FALSE, basecsnstr);
  3378. csn_free(&opcsn);
  3379. csngen_adjust_time(gen, basecsn);
  3380. csngen_new_csn(gen, &opcsn, PR_FALSE /* don't notify */);
  3381. csn_as_string(opcsn, PR_FALSE, opcsn2str);
  3382. slapi_log_err(SLAPI_LOG_WARNING, repl_plugin_name,
  3383. "replica_generate_next_csn - "
  3384. "opcsn=%s <= basecsn=%s, adjusted opcsn=%s\n",
  3385. opcsnstr, basecsnstr, opcsn2str);
  3386. }
  3387. /*
  3388. * Insert opcsn into the csn pending list.
  3389. * This is the notify effect in csngen_new_csn().
  3390. */
  3391. assign_csn_callback(opcsn, (void *)replica);
  3392. }
  3393. object_release(gen_obj);
  3394. }
  3395. }
  3396. }
  3397. object_release(replica_obj);
  3398. }
  3399. return opcsn;
  3400. }
  3401. /*
  3402. * A callback function registed as op->o_replica_attr_handler and
  3403. * called by backend ops to get replica attributes.
  3404. */
  3405. int
  3406. replica_get_attr(Slapi_PBlock *pb, const char *type, void *value)
  3407. {
  3408. int rc = -1;
  3409. Object *replica_obj;
  3410. replica_obj = replica_get_replica_for_op(pb);
  3411. if (NULL != replica_obj) {
  3412. Replica *replica = (Replica *)object_get_data(replica_obj);
  3413. if (NULL != replica) {
  3414. if (strcasecmp(type, type_replicaTombstonePurgeInterval) == 0) {
  3415. *((int *)value) = replica->tombstone_reap_interval;
  3416. rc = 0;
  3417. } else if (strcasecmp(type, type_replicaPurgeDelay) == 0) {
  3418. *((int *)value) = replica->repl_purge_delay;
  3419. rc = 0;
  3420. }
  3421. }
  3422. object_release(replica_obj);
  3423. }
  3424. return rc;
  3425. }
  3426. PRUint64
  3427. replica_get_backoff_min(Replica *r)
  3428. {
  3429. if (r) {
  3430. return slapi_counter_get_value(r->backoff_min);
  3431. } else {
  3432. return PROTOCOL_BACKOFF_MINIMUM;
  3433. }
  3434. }
  3435. PRUint64
  3436. replica_get_backoff_max(Replica *r)
  3437. {
  3438. if (r) {
  3439. return slapi_counter_get_value(r->backoff_max);
  3440. } else {
  3441. return PROTOCOL_BACKOFF_MAXIMUM;
  3442. }
  3443. }
  3444. void
  3445. replica_set_backoff_min(Replica *r, PRUint64 min)
  3446. {
  3447. if (r) {
  3448. slapi_counter_set_value(r->backoff_min, min);
  3449. }
  3450. }
  3451. void
  3452. replica_set_backoff_max(Replica *r, PRUint64 max)
  3453. {
  3454. if (r) {
  3455. slapi_counter_set_value(r->backoff_max, max);
  3456. }
  3457. }
  3458. void
  3459. replica_set_precise_purging(Replica *r, PRUint64 on_off)
  3460. {
  3461. if (r) {
  3462. slapi_counter_set_value(r->precise_purging, on_off);
  3463. }
  3464. }
  3465. PRUint64
  3466. replica_get_precise_purging(Replica *r)
  3467. {
  3468. if (r) {
  3469. return slapi_counter_get_value(r->precise_purging);
  3470. } else {
  3471. return 0;
  3472. }
  3473. }
  3474. int
  3475. replica_get_agmt_count(Replica *r)
  3476. {
  3477. return r->agmt_count;
  3478. }
  3479. void
  3480. replica_incr_agmt_count(Replica *r)
  3481. {
  3482. if (r) {
  3483. r->agmt_count++;
  3484. }
  3485. }
  3486. void
  3487. replica_decr_agmt_count(Replica *r)
  3488. {
  3489. if (r) {
  3490. if (r->agmt_count > 0) {
  3491. r->agmt_count--;
  3492. }
  3493. }
  3494. }
  3495. /*
  3496. * Add the "Abort Replication Session" control to the pblock
  3497. */
  3498. static void
  3499. replica_add_session_abort_control(Slapi_PBlock *pb)
  3500. {
  3501. LDAPControl ctrl = {0};
  3502. BerElement *ber;
  3503. struct berval *bvp;
  3504. int rc;
  3505. /* Build the BER payload */
  3506. if ((ber = der_alloc()) == NULL) {
  3507. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  3508. "add_session_abort_control - Failed to create ber\n");
  3509. return;
  3510. }
  3511. rc = ber_printf(ber, "{}");
  3512. if (rc != -1) {
  3513. rc = ber_flatten(ber, &bvp);
  3514. }
  3515. ber_free(ber, 1);
  3516. if (rc == -1) {
  3517. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  3518. "add_session_abort_control - Failed to flatten ber\n");
  3519. return;
  3520. }
  3521. ctrl.ldctl_oid = slapi_ch_strdup(REPL_ABORT_SESSION_OID);
  3522. ctrl.ldctl_value = *bvp;
  3523. bvp->bv_val = NULL;
  3524. ber_bvfree(bvp);
  3525. slapi_pblock_set(pb, SLAPI_ADD_RESCONTROL, &ctrl);
  3526. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  3527. "add_session_abort_control - abort control successfully added to result\n");
  3528. }
  3529. /*
  3530. * Check if we have exceeded the failed replica acquire limit,
  3531. * if so, end the replication session.
  3532. */
  3533. void
  3534. replica_check_release_timeout(Replica *r, Slapi_PBlock *pb)
  3535. {
  3536. replica_lock(r->repl_lock);
  3537. if (r->abort_session == ABORT_SESSION) {
  3538. /* Need to abort this session (just send the control once) */
  3539. replica_add_session_abort_control(pb);
  3540. r->abort_session = SESSION_ABORTED;
  3541. }
  3542. replica_unlock(r->repl_lock);
  3543. }
  3544. void
  3545. replica_lock_replica(Replica *r)
  3546. {
  3547. replica_lock(r->repl_lock);
  3548. }
  3549. void
  3550. replica_unlock_replica(Replica *r)
  3551. {
  3552. replica_unlock(r->repl_lock);
  3553. }