repl5_replica.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /* repl5_replica.c */
  7. #include "slapi-plugin.h"
  8. #include "repl.h" /* ONREPL - this is bad */
  9. #include "repl5.h"
  10. #include "repl_shared.h"
  11. #include "csnpl.h"
  12. #include "cl5_api.h"
  13. /* from proto-slap.h */
  14. int g_get_shutdown();
  15. #define RUV_SAVE_INTERVAL (30 * 1000) /* 30 seconds */
  16. #define START_UPDATE_DELAY 2 /* 2 second */
  17. #define REPLICA_RDN "cn=replica"
  18. #define CHANGELOG_RDN "cn=legacy changelog"
  19. /*
  20. * A replica is a locally-held copy of a portion of the DIT.
  21. */
  22. struct replica {
  23. Slapi_DN *repl_root; /* top of the replicated area */
  24. char *repl_name; /* unique replica name */
  25. PRBool new_name; /* new name was generated - need to be saved */
  26. ReplicaUpdateDNList updatedn_list; /* list of dns with which a supplier should bind
  27. to update this replica */
  28. ReplicaType repl_type; /* is this replica read-only ? */
  29. PRBool legacy_consumer; /* if true, this replica is supplied by 4.0 consumer */
  30. char* legacy_purl; /* partial url of the legacy supplier */
  31. ReplicaId repl_rid; /* replicaID */
  32. Object *repl_ruv; /* replica update vector */
  33. PRBool repl_ruv_dirty; /* Dirty flag for ruv */
  34. CSNPL *min_csn_pl; /* Pending list for minimal CSN */
  35. void *csn_pl_reg_id; /* registration assignment for csn callbacks */
  36. unsigned long repl_state_flags; /* state flags */
  37. PRUint32 repl_flags; /* persistent, externally visible flags */
  38. PRLock *repl_lock; /* protects entire structure */
  39. Slapi_Eq_Context repl_eqcxt_rs; /* context to cancel event that saves ruv */
  40. Slapi_Eq_Context repl_eqcxt_tr; /* context to cancel event that reaps tombstones */
  41. Object *repl_csngen; /* CSN generator for this replica */
  42. PRBool repl_csn_assigned; /* Flag set when new csn is assigned. */
  43. PRUint32 repl_purge_delay; /* When purgeable, CSNs are held on to for this many extra seconds */
  44. PRBool tombstone_reap_stop; /* TRUE when the tombstone reaper should stop */
  45. PRBool tombstone_reap_active; /* TRUE when the tombstone reaper is running */
  46. long tombstone_reap_interval; /* Time in seconds between tombstone reaping */
  47. Slapi_ValueSet *repl_referral; /* A list of administrator provided referral URLs */
  48. PRBool state_update_inprogress; /* replica state is being updated */
  49. PRLock *agmt_lock; /* protects agreement creation, start and stop */
  50. char *locking_purl; /* supplier who has exclusive access */
  51. };
  52. typedef struct reap_callback_data
  53. {
  54. int rc;
  55. unsigned long num_entries;
  56. unsigned long num_purged_entries;
  57. CSN *purge_csn;
  58. PRBool *tombstone_reap_stop;
  59. } reap_callback_data;
  60. /* Forward declarations of helper functions*/
  61. static Slapi_Entry* _replica_get_config_entry (const Slapi_DN *root);
  62. static int _replica_check_validity (const Replica *r);
  63. static int _replica_init_from_config (Replica *r, Slapi_Entry *e, char *errortext);
  64. static int _replica_update_entry (Replica *r, Slapi_Entry *e, char *errortext);
  65. static int _replica_configure_ruv (Replica *r, PRBool isLocked);
  66. static void _replica_update_state (time_t when, void *arg);
  67. static char * _replica_get_config_dn (const Slapi_DN *root);
  68. static char * _replica_type_as_string (const Replica *r);
  69. static int replica_create_ruv_tombstone(Replica *r);
  70. static void assign_csn_callback(const CSN *csn, void *data);
  71. static void abort_csn_callback(const CSN *csn, void *data);
  72. static void eq_cb_reap_tombstones(time_t when, void *arg);
  73. static CSN *_replica_get_purge_csn_nolock (const Replica *r);
  74. static void replica_get_referrals_nolock (const Replica *r, char ***referrals);
  75. static void replica_clear_legacy_referrals (const Slapi_DN *repl_root_sdn, char **referrals, const char *state);
  76. static void replica_remove_legacy_attr (const Slapi_DN *repl_root_sdn, const char *attr);
  77. static int replica_log_ruv_elements_nolock (const Replica *r);
  78. static void replica_replace_ruv_tombstone(Replica *r);
  79. static void start_agreements_for_replica (Replica *r, PRBool start);
  80. /* Allocates new replica and reads its state and state of its component from
  81. * various parts of the DIT.
  82. */
  83. Replica *
  84. replica_new(const Slapi_DN *root)
  85. {
  86. Replica *r = NULL;
  87. Slapi_Entry *e = NULL;
  88. char errorbuf[BUFSIZ];
  89. char ebuf[BUFSIZ];
  90. PR_ASSERT (root);
  91. /* check if there is a replica associated with the tree */
  92. e = _replica_get_config_entry (root);
  93. if (e)
  94. {
  95. errorbuf[0] = '\0';
  96. r = replica_new_from_entry(e, errorbuf,
  97. PR_FALSE /* not a newly added entry */);
  98. if (NULL == r)
  99. {
  100. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Unable to "
  101. "configure replica %s: %s\n",
  102. escape_string(slapi_sdn_get_dn(root), ebuf),
  103. errorbuf);
  104. }
  105. slapi_entry_free (e);
  106. }
  107. return r;
  108. }
  109. /* constructs the replica object from the newly added entry */
  110. Replica *
  111. replica_new_from_entry (Slapi_Entry *e, char *errortext, PRBool is_add_operation)
  112. {
  113. int rc = 0;
  114. Replica *r;
  115. RUV *ruv;
  116. char *repl_name = NULL;
  117. if (e == NULL)
  118. {
  119. if (NULL != errortext)
  120. {
  121. sprintf (errortext, "NULL entry");
  122. }
  123. return NULL;
  124. }
  125. r = (Replica *)slapi_ch_calloc(1, sizeof(Replica));
  126. if ((r->repl_lock = PR_NewLock()) == NULL)
  127. {
  128. if (NULL != errortext)
  129. {
  130. sprintf (errortext, "failed to create replica lock");
  131. }
  132. rc = -1;
  133. goto done;
  134. }
  135. if ((r->agmt_lock = PR_NewLock()) == NULL)
  136. {
  137. if (NULL != errortext)
  138. {
  139. sprintf (errortext, "failed to create replica lock");
  140. }
  141. rc = -1;
  142. goto done;
  143. }
  144. /* read parameters from the replica config entry */
  145. rc = _replica_init_from_config (r, e, errortext);
  146. if (rc != 0)
  147. {
  148. goto done;
  149. }
  150. /* configure ruv */
  151. rc = _replica_configure_ruv (r, PR_FALSE);
  152. if (rc != 0)
  153. {
  154. goto done;
  155. }
  156. /* If smallest csn exists in RUV for our local replica, it's ok to begin iteration */
  157. ruv = (RUV*) object_get_data (r->repl_ruv);
  158. PR_ASSERT (ruv);
  159. if (is_add_operation)
  160. {
  161. /*
  162. * This is called by an ldap add operation.
  163. * Update the entry to contain information generated
  164. * during replica initialization
  165. */
  166. rc = _replica_update_entry (r, e, errortext);
  167. }
  168. else
  169. {
  170. /*
  171. * Entry is already in dse.ldif - update it on the disk
  172. * (done by the update state event scheduled below)
  173. */
  174. }
  175. if (rc != 0)
  176. goto done;
  177. /* ONREPL - the state update can occur before the entry is added to the DIT.
  178. In that case the updated would fail but nothing bad would happen. The next
  179. scheduled update would save the state */
  180. repl_name = slapi_ch_strdup (r->repl_name);
  181. r->repl_eqcxt_rs = slapi_eq_repeat(_replica_update_state, repl_name,
  182. current_time () + START_UPDATE_DELAY, RUV_SAVE_INTERVAL);
  183. if (r->tombstone_reap_interval > 0)
  184. {
  185. /*
  186. * Reap Tombstone should be started some time after the plugin started.
  187. * This will allow the server to fully start before consuming resources.
  188. */
  189. repl_name = slapi_ch_strdup (r->repl_name);
  190. r->repl_eqcxt_tr = slapi_eq_repeat(eq_cb_reap_tombstones, repl_name,
  191. current_time() + r->tombstone_reap_interval,
  192. 1000 * r->tombstone_reap_interval);
  193. }
  194. if (r->legacy_consumer)
  195. {
  196. char ebuf[BUFSIZ];
  197. legacy_consumer_init_referrals (r);
  198. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_new_from_entry: "
  199. "replica for %s was configured as legacy consumer\n",
  200. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  201. }
  202. done:
  203. if (rc != 0 && r)
  204. {
  205. replica_destroy ((void**)&r);
  206. }
  207. return r;
  208. }
  209. void
  210. replica_flush(Replica *r)
  211. {
  212. PR_ASSERT(NULL != r);
  213. if (NULL != r)
  214. {
  215. PR_Lock(r->repl_lock);
  216. /* Make sure we dump the CSNGen state */
  217. r->repl_csn_assigned = PR_TRUE;
  218. PR_Unlock(r->repl_lock);
  219. /* This function take the Lock Inside */
  220. /* And also write the RUV */
  221. _replica_update_state((time_t)0, r->repl_name);
  222. }
  223. }
  224. /*
  225. * Deallocate a replica. arg should point to the address of a
  226. * pointer that points to a replica structure.
  227. */
  228. void
  229. replica_destroy(void **arg)
  230. {
  231. Replica *r;
  232. void *repl_name;
  233. if (arg == NULL)
  234. return;
  235. r = *((Replica **)arg);
  236. PR_ASSERT(r);
  237. slapi_log_error (SLAPI_LOG_REPL, NULL, "replica_destroy\n");
  238. /*
  239. * The function will not be called unless the refcnt of its
  240. * wrapper object is 0. Hopefully this refcnt could sync up
  241. * this destruction and the events such as tombstone reap
  242. * and ruv updates.
  243. */
  244. if (r->repl_eqcxt_rs)
  245. {
  246. repl_name = slapi_eq_get_arg (r->repl_eqcxt_rs);
  247. slapi_ch_free (&repl_name);
  248. slapi_eq_cancel(r->repl_eqcxt_rs);
  249. r->repl_eqcxt_rs = NULL;
  250. }
  251. if (r->repl_eqcxt_tr)
  252. {
  253. repl_name = slapi_eq_get_arg (r->repl_eqcxt_tr);
  254. slapi_ch_free (&repl_name);
  255. slapi_eq_cancel(r->repl_eqcxt_tr);
  256. r->repl_eqcxt_tr = NULL;
  257. }
  258. if (r->repl_root)
  259. {
  260. slapi_sdn_free(&r->repl_root);
  261. }
  262. slapi_ch_free_string(&r->locking_purl);
  263. if (r->updatedn_list)
  264. {
  265. replica_updatedn_list_free(r->updatedn_list);
  266. r->updatedn_list = NULL;
  267. }
  268. /* slapi_ch_free accepts NULL pointer */
  269. slapi_ch_free ((void**)&r->repl_name);
  270. slapi_ch_free ((void**)&r->legacy_purl);
  271. if (r->repl_lock)
  272. {
  273. PR_DestroyLock(r->repl_lock);
  274. r->repl_lock = NULL;
  275. }
  276. if (r->agmt_lock)
  277. {
  278. PR_DestroyLock(r->agmt_lock);
  279. r->agmt_lock = NULL;
  280. }
  281. if(NULL != r->repl_ruv)
  282. {
  283. object_release(r->repl_ruv);
  284. }
  285. if(NULL != r->repl_csngen)
  286. {
  287. if (r->csn_pl_reg_id)
  288. {
  289. csngen_unregister_callbacks((CSNGen *)object_get_data (r->repl_csngen), r->csn_pl_reg_id);
  290. }
  291. object_release(r->repl_csngen);
  292. }
  293. if (NULL != r->repl_referral)
  294. {
  295. slapi_valueset_free(r->repl_referral);
  296. }
  297. if (NULL != r->min_csn_pl)
  298. {
  299. csnplFree(&r->min_csn_pl);;
  300. }
  301. slapi_ch_free((void **)arg);
  302. }
  303. /*
  304. * Attempt to obtain exclusive access to replica (advisory only)
  305. *
  306. * Returns PR_TRUE if exclusive access was granted,
  307. * PR_FALSE otherwise
  308. * The parameter isInc tells whether or not the replica is being
  309. * locked for an incremental update session - if the replica is
  310. * successfully locked, this value is used - if the replica is already
  311. * in use, this value will be set to TRUE or FALSE, depending on what
  312. * type of update session has the replica in use currently
  313. * locking_purl is the supplier who is attempting to acquire access
  314. * current_purl is the supplier who already has access, if any
  315. */
  316. PRBool
  317. replica_get_exclusive_access(Replica *r, PRBool *isInc, int connid, int opid,
  318. const char *locking_purl,
  319. char **current_purl)
  320. {
  321. char ebuf[BUFSIZ];
  322. PRBool rval = PR_TRUE;
  323. PR_ASSERT(r);
  324. PR_Lock(r->repl_lock);
  325. if (r->repl_state_flags & REPLICA_IN_USE)
  326. {
  327. if (isInc)
  328. *isInc = (r->repl_state_flags & REPLICA_INCREMENTAL_IN_PROGRESS);
  329. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  330. "conn=%d op=%d repl=\"%s\": "
  331. "Replica in use locking_purl=%s\n",
  332. connid, opid,
  333. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf),
  334. r->locking_purl ? r->locking_purl : "unknown");
  335. rval = PR_FALSE;
  336. if (current_purl)
  337. {
  338. *current_purl = slapi_ch_strdup(r->locking_purl);
  339. }
  340. }
  341. else
  342. {
  343. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  344. "conn=%d op=%d repl=\"%s\": Acquired replica\n",
  345. connid, opid,
  346. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  347. r->repl_state_flags |= REPLICA_IN_USE;
  348. if (isInc && *isInc)
  349. {
  350. r->repl_state_flags |= REPLICA_INCREMENTAL_IN_PROGRESS;
  351. }
  352. else
  353. {
  354. /* if connid or opid != 0, it's a total update */
  355. /* Both set to 0 means we're disabling replication */
  356. if (connid || opid)
  357. {
  358. r->repl_state_flags |= REPLICA_TOTAL_IN_PROGRESS;
  359. }
  360. }
  361. slapi_ch_free_string(&r->locking_purl);
  362. r->locking_purl = slapi_ch_strdup(locking_purl);
  363. }
  364. PR_Unlock(r->repl_lock);
  365. return rval;
  366. }
  367. /*
  368. * Relinquish exclusive access to the replica
  369. */
  370. void
  371. replica_relinquish_exclusive_access(Replica *r, int connid, int opid)
  372. {
  373. char ebuf[BUFSIZ];
  374. PRBool isInc;
  375. PR_ASSERT(r);
  376. PR_Lock(r->repl_lock);
  377. isInc = (r->repl_state_flags & REPLICA_INCREMENTAL_IN_PROGRESS);
  378. /* check to see if the replica is in use and log a warning if not */
  379. if (!(r->repl_state_flags & REPLICA_IN_USE))
  380. {
  381. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  382. "conn=%d op=%d repl=\"%s\": "
  383. "Replica not in use\n",
  384. connid, opid,
  385. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  386. } else {
  387. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  388. "conn=%d op=%d repl=\"%s\": "
  389. "Released replica\n",
  390. connid, opid,
  391. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  392. slapi_ch_free_string(&r->locking_purl);
  393. r->repl_state_flags &= ~(REPLICA_IN_USE);
  394. if (isInc)
  395. r->repl_state_flags &= ~(REPLICA_INCREMENTAL_IN_PROGRESS);
  396. else
  397. r->repl_state_flags &= ~(REPLICA_TOTAL_IN_PROGRESS);
  398. }
  399. PR_Unlock(r->repl_lock);
  400. }
  401. /*
  402. * Returns root of the replicated area
  403. */
  404. PRBool
  405. replica_get_tombstone_reap_active(const Replica *r)
  406. {
  407. PR_ASSERT(r);
  408. return(r->tombstone_reap_active);
  409. }
  410. /*
  411. * Returns root of the replicated area
  412. */
  413. const Slapi_DN *
  414. replica_get_root(const Replica *r) /* ONREPL - should we return copy instead? */
  415. {
  416. PR_ASSERT(r);
  417. /* replica root never changes so we don't have to lock */
  418. return(r->repl_root);
  419. }
  420. /*
  421. * Returns normalized dn of the root of the replicated area
  422. */
  423. const char *
  424. replica_get_name(const Replica *r) /* ONREPL - should we return copy instead? */
  425. {
  426. PR_ASSERT(r);
  427. /* replica name never changes so we don't have to lock */
  428. return(r->repl_name);
  429. }
  430. /*
  431. * Returns replicaid of this replica
  432. */
  433. ReplicaId
  434. replica_get_rid (const Replica *r)
  435. {
  436. ReplicaId rid;
  437. PR_ASSERT(r);
  438. PR_Lock(r->repl_lock);
  439. rid = r->repl_rid;
  440. PR_Unlock(r->repl_lock);
  441. return rid;
  442. }
  443. /*
  444. * Sets replicaid of this replica - should only be used when also changing the type
  445. */
  446. void
  447. replica_set_rid (Replica *r, ReplicaId rid)
  448. {
  449. PR_ASSERT(r);
  450. PR_Lock(r->repl_lock);
  451. r->repl_rid = rid;
  452. PR_Unlock(r->repl_lock);
  453. }
  454. /* Returns true if replica was initialized through ORC or import;
  455. * otherwise, false. An uninitialized replica should return
  456. * LDAP_UNWILLING_TO_PERFORM to all client requests
  457. */
  458. PRBool
  459. replica_is_initialized (const Replica *r)
  460. {
  461. PR_ASSERT(r);
  462. return (r->repl_ruv != NULL);
  463. }
  464. /*
  465. * Returns refcounted object that contains RUV. The caller should release the
  466. * object once it is no longer used. To release, call object_release
  467. */
  468. Object *
  469. replica_get_ruv (const Replica *r)
  470. {
  471. Object *ruv = NULL;
  472. PR_ASSERT(r);
  473. PR_Lock(r->repl_lock);
  474. PR_ASSERT (r->repl_ruv);
  475. object_acquire (r->repl_ruv);
  476. ruv = r->repl_ruv;
  477. PR_Unlock(r->repl_lock);
  478. return ruv;
  479. }
  480. /*
  481. * Sets RUV vector. This function should be called during replica
  482. * (re)initialization. During normal operation, the RUV is read from
  483. * the root of the replicated in the replica_new call
  484. */
  485. void
  486. replica_set_ruv (Replica *r, RUV *ruv)
  487. {
  488. PR_ASSERT(r && ruv);
  489. PR_Lock(r->repl_lock);
  490. if(NULL != r->repl_ruv)
  491. {
  492. object_release(r->repl_ruv);
  493. }
  494. /* if the local replica is not in the RUV and it is writable - add it
  495. and reinitialize min_csn pending list */
  496. if (r->repl_type == REPLICA_TYPE_UPDATABLE)
  497. {
  498. CSN *csn = NULL;
  499. if (r->min_csn_pl)
  500. csnplFree (&r->min_csn_pl);
  501. if (ruv_contains_replica (ruv, r->repl_rid))
  502. {
  503. ruv_get_smallest_csn_for_replica(ruv, r->repl_rid, &csn);
  504. if (csn)
  505. csn_free (&csn);
  506. else
  507. r->min_csn_pl = csnplNew ();
  508. /* We need to make sure the local ruv element is the 1st. */
  509. ruv_move_local_supplier_to_first(ruv, r->repl_rid);
  510. }
  511. else
  512. {
  513. r->min_csn_pl = csnplNew ();
  514. /* To be sure that the local is in first */
  515. ruv_add_index_replica(ruv, r->repl_rid, multimaster_get_local_purl(), 1);
  516. }
  517. }
  518. r->repl_ruv = object_new((void*)ruv, (FNFree)ruv_destroy);
  519. r->repl_ruv_dirty = PR_TRUE;
  520. PR_Unlock(r->repl_lock);
  521. }
  522. /*
  523. * Update one particular CSN in an RUV. This is meant to be called
  524. * whenever (a) the server has processed a client operation and
  525. * needs to update its CSN, or (b) the server is completing an
  526. * inbound replication session operation, and needs to update its
  527. * local RUV.
  528. */
  529. void
  530. replica_update_ruv(Replica *r, const CSN *updated_csn, const char *replica_purl)
  531. {
  532. char csn_str[CSN_STRSIZE];
  533. char ebuf[BUFSIZ];
  534. PR_ASSERT(NULL != r);
  535. PR_ASSERT(NULL != updated_csn);
  536. #ifdef DEBUG
  537. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  538. "replica_update_ruv: csn %s\n",
  539. csn_as_string(updated_csn, PR_FALSE, csn_str)); /* XXXggood remove debugging */
  540. #endif
  541. if (NULL == r)
  542. {
  543. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_update_ruv: replica "
  544. "is NULL\n");
  545. }
  546. else if (NULL == updated_csn)
  547. {
  548. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_update_ruv: csn "
  549. "is NULL when updating replica %s\n", escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  550. }
  551. else
  552. {
  553. RUV *ruv;
  554. PR_Lock(r->repl_lock);
  555. if (r->repl_ruv != NULL)
  556. {
  557. ruv = object_get_data(r->repl_ruv);
  558. if (NULL != ruv)
  559. {
  560. ReplicaId rid = csn_get_replicaid(updated_csn);
  561. if (rid == r->repl_rid)
  562. {
  563. if (NULL != r->min_csn_pl)
  564. {
  565. CSN *min_csn;
  566. PRBool committed;
  567. (void)csnplCommit(r->min_csn_pl, updated_csn);
  568. min_csn = csnplGetMinCSN(r->min_csn_pl, &committed);
  569. if (NULL != min_csn)
  570. {
  571. if (committed)
  572. {
  573. ruv_set_min_csn(ruv, min_csn, replica_purl);
  574. csnplFree(&r->min_csn_pl);
  575. }
  576. csn_free(&min_csn);
  577. }
  578. }
  579. }
  580. /* Update max csn for local and remote replicas */
  581. if (ruv_update_ruv (ruv, updated_csn, replica_purl, rid == r->repl_rid)
  582. != RUV_SUCCESS)
  583. {
  584. slapi_log_error(SLAPI_LOG_FATAL,
  585. repl_plugin_name, "replica_update_ruv: unable "
  586. "to update RUV for replica %s, csn = %s\n",
  587. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf),
  588. csn_as_string(updated_csn, PR_FALSE, csn_str));
  589. }
  590. r->repl_ruv_dirty = PR_TRUE;
  591. }
  592. else
  593. {
  594. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  595. "replica_update_ruv: unable to get RUV object for replica "
  596. "%s\n", escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  597. }
  598. }
  599. else
  600. {
  601. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_update_ruv: "
  602. "unable to initialize RUV for replica %s\n",
  603. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  604. }
  605. PR_Unlock(r->repl_lock);
  606. }
  607. }
  608. /*
  609. * Returns refcounted object that contains csn generator. The caller should release the
  610. * object once it is no longer used. To release, call object_release
  611. */
  612. Object *
  613. replica_get_csngen (const Replica *r)
  614. {
  615. Object *csngen;
  616. PR_ASSERT(r);
  617. PR_Lock(r->repl_lock);
  618. object_acquire (r->repl_csngen);
  619. csngen = r->repl_csngen;
  620. PR_Unlock(r->repl_lock);
  621. return csngen;
  622. }
  623. /*
  624. * Returns the replica type.
  625. */
  626. ReplicaType
  627. replica_get_type (const Replica *r)
  628. {
  629. PR_ASSERT(r);
  630. return r->repl_type;
  631. }
  632. /*
  633. * Sets the replica type.
  634. */
  635. void
  636. replica_set_type (Replica *r, ReplicaType type)
  637. {
  638. PR_ASSERT(r);
  639. PR_Lock(r->repl_lock);
  640. r->repl_type = type;
  641. PR_Unlock(r->repl_lock);
  642. }
  643. /*
  644. * Returns PR_TRUE if this replica is a consumer of 4.0 server
  645. * and PR_FALSE otherwise
  646. */
  647. PRBool
  648. replica_is_legacy_consumer (const Replica *r)
  649. {
  650. PR_ASSERT(r);
  651. return r->legacy_consumer;
  652. }
  653. /*
  654. * Sets the replica type.
  655. */
  656. void
  657. replica_set_legacy_consumer (Replica *r, PRBool legacy_consumer)
  658. {
  659. int legacy2mmr;
  660. Slapi_DN *repl_root_sdn = NULL;
  661. char **referrals = NULL;
  662. char *replstate = NULL;
  663. PR_ASSERT(r);
  664. PR_Lock(r->repl_lock);
  665. legacy2mmr = r->legacy_consumer && !legacy_consumer;
  666. /* making the server a regular 5.0 replica */
  667. if (legacy2mmr)
  668. {
  669. slapi_ch_free ((void**)&r->legacy_purl);
  670. /* Remove copiedFrom/copyingFrom attributes from the root entry */
  671. /* set the right state in the mapping tree */
  672. if (r->repl_type == REPLICA_TYPE_READONLY)
  673. {
  674. replica_get_referrals_nolock (r, &referrals);
  675. replstate = STATE_UPDATE_REFERRAL;
  676. }
  677. else /* updateable */
  678. {
  679. replstate = STATE_BACKEND;
  680. }
  681. }
  682. r->legacy_consumer = legacy_consumer;
  683. repl_root_sdn = slapi_sdn_dup(r->repl_root);
  684. PR_Unlock(r->repl_lock);
  685. if (legacy2mmr)
  686. {
  687. replica_clear_legacy_referrals(repl_root_sdn, referrals, replstate);
  688. /* Also change state of the mapping tree node and/or referrals */
  689. replica_remove_legacy_attr (repl_root_sdn, type_copiedFrom);
  690. replica_remove_legacy_attr (repl_root_sdn, type_copyingFrom);
  691. }
  692. charray_free(referrals);
  693. slapi_sdn_free(&repl_root_sdn);
  694. }
  695. /* Gets partial url of the legacy supplier - applicable for legacy consumer only */
  696. char *
  697. replica_get_legacy_purl (const Replica *r)
  698. {
  699. char *purl;
  700. PR_Lock (r->repl_lock);
  701. PR_ASSERT (r->legacy_consumer);
  702. purl = slapi_ch_strdup (r->legacy_purl);
  703. PR_Unlock (r->repl_lock);
  704. return purl;
  705. }
  706. void
  707. replica_set_legacy_purl (Replica *r, const char *purl)
  708. {
  709. PR_Lock (r->repl_lock);
  710. PR_ASSERT (r->legacy_consumer);
  711. /* slapi_ch_free accepts NULL pointer */
  712. slapi_ch_free ((void**)&r->legacy_purl);
  713. r->legacy_purl = slapi_ch_strdup (purl);
  714. PR_Unlock (r->repl_lock);
  715. }
  716. /*
  717. * Returns true if sdn is the same as updatedn and false otherwise
  718. */
  719. PRBool
  720. replica_is_updatedn (const Replica *r, const Slapi_DN *sdn)
  721. {
  722. PRBool result;
  723. PR_ASSERT (r);
  724. PR_Lock(r->repl_lock);
  725. if (sdn == NULL)
  726. {
  727. result = (r->updatedn_list == NULL);
  728. }
  729. else if (r->updatedn_list == NULL)
  730. {
  731. result = PR_FALSE;
  732. }
  733. else
  734. {
  735. result = replica_updatedn_list_ismember(r->updatedn_list, sdn);
  736. }
  737. PR_Unlock(r->repl_lock);
  738. return result;
  739. }
  740. /*
  741. * Sets updatedn list for this replica
  742. */
  743. void
  744. replica_set_updatedn (Replica *r, const Slapi_ValueSet *vs, int mod_op)
  745. {
  746. PR_ASSERT (r);
  747. PR_Lock(r->repl_lock);
  748. if (!r->updatedn_list)
  749. r->updatedn_list = replica_updatedn_list_new(NULL);
  750. if (mod_op & LDAP_MOD_DELETE || vs == NULL ||
  751. (0 == slapi_valueset_count(vs))) /* null value also causes list deletion */
  752. replica_updatedn_list_delete(r->updatedn_list, vs);
  753. else if (mod_op & LDAP_MOD_REPLACE)
  754. replica_updatedn_list_replace(r->updatedn_list, vs);
  755. else if (mod_op & LDAP_MOD_ADD)
  756. replica_updatedn_list_add(r->updatedn_list, vs);
  757. PR_Unlock(r->repl_lock);
  758. }
  759. /* gets current replica generation for this replica */
  760. char *replica_get_generation (const Replica *r)
  761. {
  762. int rc = 0;
  763. char *gen = NULL;
  764. if (r)
  765. {
  766. PR_Lock(r->repl_lock);
  767. PR_ASSERT (r->repl_ruv);
  768. if (rc == 0)
  769. gen = ruv_get_replica_generation ((RUV*)object_get_data (r->repl_ruv));
  770. PR_Unlock(r->repl_lock);
  771. }
  772. return gen;
  773. }
  774. PRBool replica_is_flag_set (const Replica *r, PRUint32 flag)
  775. {
  776. if (r)
  777. return (r->repl_flags & flag);
  778. else
  779. return PR_FALSE;
  780. }
  781. void replica_set_flag (Replica *r, PRUint32 flag, PRBool clear)
  782. {
  783. if (r == NULL)
  784. return;
  785. PR_Lock(r->repl_lock);
  786. if (clear)
  787. {
  788. r->repl_flags &= ~flag;
  789. }
  790. else
  791. {
  792. r->repl_flags |= flag;
  793. }
  794. PR_Unlock(r->repl_lock);
  795. }
  796. void replica_replace_flags (Replica *r, PRUint32 flags)
  797. {
  798. if (r)
  799. {
  800. PR_Lock(r->repl_lock);
  801. r->repl_flags = flags;
  802. PR_Unlock(r->repl_lock);
  803. }
  804. }
  805. void
  806. replica_get_referrals(const Replica *r, char ***referrals)
  807. {
  808. PR_Lock(r->repl_lock);
  809. replica_get_referrals_nolock (r, referrals);
  810. PR_Unlock(r->repl_lock);
  811. }
  812. void
  813. replica_set_referrals(Replica *r,const Slapi_ValueSet *vs)
  814. {
  815. int ii = 0;
  816. Slapi_Value *vv = NULL;
  817. if (r->repl_referral == NULL)
  818. {
  819. r->repl_referral = slapi_valueset_new();
  820. }
  821. else
  822. {
  823. slapi_valueset_done(r->repl_referral);
  824. }
  825. slapi_valueset_set_valueset(r->repl_referral, vs);
  826. /* make sure the DN is included in the referral LDAP URL */
  827. if (r->repl_referral)
  828. {
  829. Slapi_ValueSet *newvs = slapi_valueset_new();
  830. const char *repl_root = slapi_sdn_get_dn(r->repl_root);
  831. int rootlen = strlen(repl_root);
  832. ii = slapi_valueset_first_value(r->repl_referral, &vv);
  833. while (vv)
  834. {
  835. const char *ref = slapi_value_get_string(vv);
  836. struct ldap_url_desc *lud = NULL;
  837. int myrc = ldap_url_parse(ref, &lud);
  838. /* see if the dn is already in the referral URL */
  839. if (myrc == LDAP_URL_ERR_NODN || !lud || !lud->lud_dn) {
  840. /* add the dn */
  841. Slapi_Value *newval = NULL;
  842. int len = strlen(ref);
  843. char *tmpref = NULL;
  844. int need_slash = 0;
  845. if (ref[len-1] != '/') {
  846. len++; /* add another one for the slash */
  847. need_slash = 1;
  848. }
  849. len += rootlen + 2;
  850. tmpref = slapi_ch_malloc(len);
  851. sprintf(tmpref, "%s%s%s", ref, (need_slash ? "/" : ""),
  852. repl_root);
  853. newval = slapi_value_new_string(tmpref);
  854. slapi_ch_free_string(&tmpref); /* sv_new_string makes a copy */
  855. slapi_valueset_add_value(newvs, newval);
  856. slapi_value_free(&newval); /* s_vs_add_value makes a copy */
  857. }
  858. if (lud)
  859. ldap_free_urldesc(lud);
  860. ii = slapi_valueset_next_value(r->repl_referral, ii, &vv);
  861. }
  862. if (slapi_valueset_count(newvs) > 0) {
  863. slapi_valueset_done(r->repl_referral);
  864. slapi_valueset_set_valueset(r->repl_referral, newvs);
  865. }
  866. slapi_valueset_free(newvs); /* s_vs_set_vs makes a copy */
  867. }
  868. }
  869. int
  870. replica_update_csngen_state (Replica *r, const RUV *ruv)
  871. {
  872. int rc = 0;
  873. CSNGen *gen;
  874. CSN *csn = NULL;
  875. PR_ASSERT (r && ruv);
  876. rc = ruv_get_max_csn(ruv, &csn);
  877. if (rc != RUV_SUCCESS)
  878. {
  879. return -1;
  880. }
  881. if (csn == NULL) /* ruv contains no csn - we are done */
  882. {
  883. return 0;
  884. }
  885. PR_Lock(r->repl_lock);
  886. gen = (CSNGen *)object_get_data (r->repl_csngen);
  887. PR_ASSERT (gen);
  888. rc = csngen_adjust_time (gen, csn);
  889. if (rc != CSN_SUCCESS)
  890. {
  891. rc = -1;
  892. goto done;
  893. }
  894. rc = 0;
  895. done:
  896. PR_Unlock(r->repl_lock);
  897. if (csn)
  898. csn_free (&csn);
  899. return rc;
  900. }
  901. /*
  902. * dumps replica state for debugging purpose
  903. */
  904. void
  905. replica_dump(Replica *r)
  906. {
  907. char *updatedn_list = NULL;
  908. PR_ASSERT (r);
  909. PR_Lock(r->repl_lock);
  910. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "Replica state:\n");
  911. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\treplica root: %s\n",
  912. slapi_sdn_get_ndn (r->repl_root));
  913. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\treplica type: %s\n",
  914. _replica_type_as_string (r));
  915. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\treplica id: %d\n", r->repl_rid);
  916. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\tflags: %d\n", r->repl_flags);
  917. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\tstate flags: %d\n", r->repl_state_flags);
  918. if (r->updatedn_list)
  919. updatedn_list = replica_updatedn_list_to_string(r->updatedn_list, "\n\t\t");
  920. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\tupdate dn: %s\n",
  921. updatedn_list? updatedn_list : "not configured");
  922. slapi_ch_free_string(&updatedn_list);
  923. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\truv: %s configured and is %sdirty\n",
  924. r->repl_ruv ? "" : "not", r->repl_ruv_dirty ? "" : "not ");
  925. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\tCSN generator: %s configured\n",
  926. r->repl_csngen ? "" : "not");
  927. /* JCMREPL - Dump Referrals */
  928. PR_Unlock(r->repl_lock);
  929. }
  930. /*
  931. * Return the CSN of the purge point. Any CSNs smaller than the
  932. * purge point can be safely removed from entries within this
  933. * this replica. Returns an allocated CSN that must be freed by
  934. * the caller, or NULL if purging is disabled.
  935. */
  936. CSN *
  937. replica_get_purge_csn(const Replica *r)
  938. {
  939. CSN *csn;
  940. PR_Lock(r->repl_lock);
  941. csn= _replica_get_purge_csn_nolock(r);
  942. PR_Unlock(r->repl_lock);
  943. return csn;
  944. }
  945. /*
  946. * This function logs a dummy entry for the smallest csn in the RUV.
  947. * This is necessary because, to get the next change, we need to position
  948. * changelog on the previous change. So this function insures that we always have one.
  949. */
  950. /* ONREPL we will need to change this function to log all the
  951. * ruv elements not just the smallest when changelog iteration
  952. * algoritm changes to iterate replica by replica
  953. */
  954. int
  955. replica_log_ruv_elements (const Replica *r)
  956. {
  957. int rc = 0;
  958. PR_ASSERT (r);
  959. PR_Lock(r->repl_lock);
  960. rc = replica_log_ruv_elements_nolock (r);
  961. PR_Unlock(r->repl_lock);
  962. return rc;
  963. }
  964. void
  965. consumer5_set_mapping_tree_state_for_replica(const Replica *r, RUV *supplierRuv)
  966. {
  967. const Slapi_DN *repl_root_sdn= replica_get_root(r);
  968. char **ruv_referrals= NULL;
  969. char **replica_referrals= NULL;
  970. RUV *ruv;
  971. int state_backend = -1;
  972. const char *mtn_state = NULL;
  973. PR_Lock (r->repl_lock);
  974. if ( supplierRuv == NULL )
  975. {
  976. ruv = (RUV*)object_get_data (r->repl_ruv);
  977. PR_ASSERT (ruv);
  978. ruv_referrals= ruv_get_referrals(ruv); /* ruv_referrals has to be free'd */
  979. }
  980. else
  981. {
  982. ruv_referrals = ruv_get_referrals(supplierRuv);
  983. }
  984. replica_get_referrals_nolock (r, &replica_referrals); /* replica_referrals has to be free'd */
  985. /* JCMREPL - What if there's a Total update in progress? */
  986. if( (r->repl_type==REPLICA_TYPE_READONLY) || (r->legacy_consumer) )
  987. {
  988. state_backend = 0;
  989. }
  990. else if (r->repl_type==REPLICA_TYPE_UPDATABLE)
  991. {
  992. state_backend = 1;
  993. }
  994. /* Unlock to avoid changing MTN state under repl lock */
  995. PR_Unlock (r->repl_lock);
  996. if(state_backend == 0 )
  997. {
  998. /* Read-Only - The mapping tree should be refering all update operations. */
  999. mtn_state = STATE_UPDATE_REFERRAL;
  1000. }
  1001. else if (state_backend == 1)
  1002. {
  1003. /* Updatable - The mapping tree should be accepting all update operations. */
  1004. mtn_state = STATE_BACKEND;
  1005. }
  1006. /* JCMREPL - Check the return code. */
  1007. repl_set_mtn_state_and_referrals(repl_root_sdn, mtn_state, NULL,
  1008. ruv_referrals, replica_referrals);
  1009. charray_free(ruv_referrals);
  1010. charray_free(replica_referrals);
  1011. }
  1012. void
  1013. replica_set_enabled (Replica *r, PRBool enable)
  1014. {
  1015. char *repl_name = NULL;
  1016. PR_ASSERT (r);
  1017. PR_Lock (r->repl_lock);
  1018. if (enable)
  1019. {
  1020. if (r->repl_eqcxt_rs == NULL) /* event is not already registered */
  1021. {
  1022. repl_name = slapi_ch_strdup (r->repl_name);
  1023. r->repl_eqcxt_rs = slapi_eq_repeat(_replica_update_state, repl_name,
  1024. current_time() + START_UPDATE_DELAY, RUV_SAVE_INTERVAL);
  1025. }
  1026. }
  1027. else /* disable */
  1028. {
  1029. if (r->repl_eqcxt_rs) /* event is still registerd */
  1030. {
  1031. repl_name = slapi_eq_get_arg (r->repl_eqcxt_rs);
  1032. slapi_ch_free ((void**)&repl_name);
  1033. slapi_eq_cancel(r->repl_eqcxt_rs);
  1034. r->repl_eqcxt_rs = NULL;
  1035. }
  1036. }
  1037. PR_Unlock (r->repl_lock);
  1038. }
  1039. /* This function is generally called when replica's data store
  1040. is reloaded. It retrieves new RUV from the datastore. If new
  1041. RUV does not exist or if it is not as up to date as the purge RUV
  1042. of the corresponding changelog file, we need to remove */
  1043. /* the function minimizes the use of replica lock where ever possible.
  1044. Locking replica lock while calling changelog functions
  1045. causes a deadlock because changelog calls replica functions that
  1046. that lock the same lock */
  1047. int
  1048. replica_reload_ruv (Replica *r)
  1049. {
  1050. int rc = 0;
  1051. Object *old_ruv_obj = NULL, *new_ruv_obj = NULL;
  1052. RUV *upper_bound_ruv = NULL;
  1053. RUV *new_ruv = NULL;
  1054. Object *r_obj;
  1055. PR_ASSERT (r);
  1056. PR_Lock (r->repl_lock);
  1057. old_ruv_obj = r->repl_ruv;
  1058. r->repl_ruv = NULL;
  1059. rc = _replica_configure_ruv (r, PR_TRUE);
  1060. PR_Unlock (r->repl_lock);
  1061. if (rc != 0)
  1062. {
  1063. return rc;
  1064. }
  1065. /* check if there is a changelog and whether this replica logs changes */
  1066. if (cl5GetState () == CL5_STATE_OPEN && r->repl_flags & REPLICA_LOG_CHANGES)
  1067. {
  1068. /* Compare new ruv to the changelog's upper bound ruv. We could only keep
  1069. the existing changelog if its upper bound is the same as replica's RUV.
  1070. This is because if changelog has changes not in RUV, they will be
  1071. eventually sent to the consumer's which will cause a state mismatch
  1072. (because the supplier does not actually contain the changes in its data store.
  1073. If, on the other hand, the changelog is not as up to date as the supplier,
  1074. it is not really useful since out of sync consumer's can't be brought
  1075. up to date using this changelog and hence will need to be reinitialized */
  1076. /* replace ruv to make sure we work with the correct changelog file */
  1077. PR_Lock (r->repl_lock);
  1078. new_ruv_obj = r->repl_ruv;
  1079. r->repl_ruv = old_ruv_obj;
  1080. PR_Unlock (r->repl_lock);
  1081. rc = cl5GetUpperBoundRUV (r, &upper_bound_ruv);
  1082. if (rc != CL5_SUCCESS && rc != CL5_NOTFOUND)
  1083. {
  1084. return -1;
  1085. }
  1086. if (upper_bound_ruv)
  1087. {
  1088. new_ruv = object_get_data (new_ruv_obj);
  1089. PR_ASSERT (new_ruv);
  1090. /* ONREPL - there are more efficient ways to establish RUV equality.
  1091. However, because this is not in the critical path and we at most
  1092. have 2 elements in the RUV, this will not effect performance */
  1093. if (!ruv_covers_ruv (new_ruv, upper_bound_ruv) ||
  1094. !ruv_covers_ruv (upper_bound_ruv, new_ruv))
  1095. {
  1096. char ebuf[BUFSIZ];
  1097. /* create a temporary replica object to conform to the interface */
  1098. r_obj = object_new (r, NULL);
  1099. /* We can't use existing changelog - remove existing file */
  1100. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_reload_ruv: "
  1101. "Warning: new data for replica %s does not match the data in the changelog.\n"
  1102. " Recreating the changelog file. This could affect replication with replica's "
  1103. " consumers in which case the consumers should be reinitialized.\n",
  1104. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  1105. rc = cl5DeleteDBSync (r_obj);
  1106. /* reinstate new ruv */
  1107. PR_Lock (r->repl_lock);
  1108. r->repl_ruv = new_ruv_obj;
  1109. object_release (r_obj);
  1110. if (rc == CL5_SUCCESS)
  1111. {
  1112. /* log changes to mark starting point for replication */
  1113. rc = replica_log_ruv_elements_nolock (r);
  1114. }
  1115. PR_Unlock (r->repl_lock);
  1116. }
  1117. else
  1118. {
  1119. /* we just need to reinstate new ruv */
  1120. PR_Lock (r->repl_lock);
  1121. r->repl_ruv = new_ruv_obj;
  1122. PR_Unlock (r->repl_lock);
  1123. }
  1124. }
  1125. else /* upper bound vector is not there - we have no changes logged */
  1126. {
  1127. /* reinstate new ruv */
  1128. PR_Lock (r->repl_lock);
  1129. r->repl_ruv = new_ruv_obj;
  1130. /* just log elements of the current RUV. This is to have
  1131. a starting point for iteration through the changes */
  1132. rc = replica_log_ruv_elements_nolock (r);
  1133. PR_Unlock (r->repl_lock);
  1134. }
  1135. }
  1136. if (rc == 0)
  1137. {
  1138. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  1139. /* reset mapping tree referrals based on new local RUV */
  1140. }
  1141. if (old_ruv_obj)
  1142. object_release (old_ruv_obj);
  1143. if (upper_bound_ruv)
  1144. ruv_destroy (&upper_bound_ruv);
  1145. return rc;
  1146. }
  1147. /* this function is called during server startup for each replica
  1148. to check whether the replica's data was reloaded offline and
  1149. whether replica's changelog needs to be reinitialized */
  1150. /* the function does not use replica lock but all functions it calls are
  1151. thread safe. Locking replica lock while calling changelog functions
  1152. causes a deadlock because changelog calls replica functions that
  1153. that lock the same lock */
  1154. int replica_check_for_data_reload (Replica *r, void *arg)
  1155. {
  1156. int rc = 0;
  1157. RUV *upper_bound_ruv = NULL;
  1158. RUV *r_ruv = NULL;
  1159. Object *r_obj, *ruv_obj;
  1160. int cl_cover_be, be_cover_cl;
  1161. PR_ASSERT (r);
  1162. /* check that we have a changelog and if this replica logs changes */
  1163. if (cl5GetState () == CL5_STATE_OPEN && r->repl_flags & REPLICA_LOG_CHANGES)
  1164. {
  1165. /* Compare new ruv to the purge ruv. If the new contains csns which
  1166. are smaller than those in purge ruv, we need to remove old and
  1167. create new changelog file for this replica. This is because we
  1168. will not have sufficient changes to incrementally update a consumer
  1169. to the current state of the supplier. */
  1170. rc = cl5GetUpperBoundRUV (r, &upper_bound_ruv);
  1171. if (rc != CL5_SUCCESS && rc != CL5_NOTFOUND)
  1172. {
  1173. return -1;
  1174. }
  1175. if (upper_bound_ruv)
  1176. {
  1177. ruv_obj = replica_get_ruv (r);
  1178. r_ruv = object_get_data (ruv_obj);
  1179. PR_ASSERT (r_ruv);
  1180. /* Compare new ruv to the changelog's upper bound ruv. We could only keep
  1181. the existing changelog if its upper bound is the same as replica's RUV.
  1182. This is because if changelog has changes not in RUV, they will be
  1183. eventually sent to the consumer's which will cause a state mismatch
  1184. (because the supplier does not actually contain the changes in its data store.
  1185. If, on the other hand, the changelog is not as up to date as the supplier,
  1186. it is not really useful since out of sync consumer's can't be brought
  1187. up to date using this changelog and hence will need to be reinitialized */
  1188. /*
  1189. * Actually we can ignore the scenario that the changelog's upper
  1190. * bound ruv covers data store's ruv for two reasons: (1) a change
  1191. * is always written to the changelog after it is committed to the
  1192. * data store; (2) a change will be ignored if the server has seen
  1193. * it before - this happens frequently at the beginning of replication
  1194. * sessions.
  1195. */
  1196. be_cover_cl = ruv_covers_ruv (r_ruv, upper_bound_ruv);
  1197. cl_cover_be = ruv_covers_ruv (upper_bound_ruv, r_ruv);
  1198. if (!cl_cover_be)
  1199. {
  1200. /* the data was reloaded and we can no longer use existing changelog */
  1201. char ebuf[BUFSIZ];
  1202. /* create a temporary replica object to conform to the interface */
  1203. r_obj = object_new (r, NULL);
  1204. /* We can't use existing changelog - remove existing file */
  1205. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_check_for_data_reload: "
  1206. "Warning: data for replica %s was reloaded and it no longer matches the data "
  1207. "in the changelog (replica data %s changelog). Recreating the changelog file. This could affect replication "
  1208. "with replica's consumers in which case the consumers should be reinitialized.\n",
  1209. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf),
  1210. ((!be_cover_cl && !cl_cover_be) ? "<>" : (!be_cover_cl ? "<" : ">")) );
  1211. rc = cl5DeleteDBSync (r_obj);
  1212. object_release (r_obj);
  1213. if (rc == CL5_SUCCESS)
  1214. {
  1215. /* log changes to mark starting point for replication */
  1216. rc = replica_log_ruv_elements (r);
  1217. }
  1218. }
  1219. object_release (ruv_obj);
  1220. }
  1221. else /* we have no changes currently logged for this replica */
  1222. {
  1223. /* log changes to mark starting point for replication */
  1224. rc = replica_log_ruv_elements (r);
  1225. }
  1226. }
  1227. if (rc == 0)
  1228. {
  1229. /* reset mapping tree referrals based on new local RUV */
  1230. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  1231. }
  1232. if (upper_bound_ruv)
  1233. ruv_destroy (&upper_bound_ruv);
  1234. return rc;
  1235. }
  1236. /* Helper functions */
  1237. /* reads replica configuration entry. The entry is the child of the
  1238. mapping tree node for the replica's backend */
  1239. static Slapi_Entry*
  1240. _replica_get_config_entry (const Slapi_DN *root)
  1241. {
  1242. int rc = 0;
  1243. char *dn = NULL;
  1244. Slapi_Entry **entries;
  1245. Slapi_Entry *e = NULL;
  1246. Slapi_PBlock *pb = NULL;
  1247. dn = _replica_get_config_dn (root);
  1248. pb = slapi_pblock_new ();
  1249. slapi_search_internal_set_pb (pb, dn, LDAP_SCOPE_BASE, "objectclass=*", NULL, 0, NULL,
  1250. NULL, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  1251. slapi_search_internal_pb (pb);
  1252. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1253. if (rc == 0)
  1254. {
  1255. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1256. e = slapi_entry_dup (entries [0]);
  1257. }
  1258. slapi_free_search_results_internal(pb);
  1259. slapi_pblock_destroy (pb);
  1260. slapi_ch_free_string(&dn);
  1261. return e;
  1262. }
  1263. static int
  1264. _replica_check_validity (const Replica *r)
  1265. {
  1266. PR_ASSERT (r);
  1267. if (r->repl_root == NULL || r->repl_type == 0 || r->repl_rid == 0 ||
  1268. r->repl_rid > MAX_REPLICA_ID || r->repl_csngen == NULL || r->repl_name == NULL)
  1269. {
  1270. return -1;
  1271. }
  1272. else
  1273. {
  1274. return 0;
  1275. }
  1276. }
  1277. /* replica configuration entry has the following format:
  1278. dn: cn=replica,<mapping tree node dn>
  1279. objectclass: top
  1280. objectclass: nsds5Replica
  1281. objectclass: extensibleObject
  1282. nsds5ReplicaRoot: <root of the replica>
  1283. nsds5ReplicaId: <replica id>
  1284. nsds5ReplicaType: <type of the replica: primary, read-write or read-only>
  1285. nsState: <state of the csn generator> missing the first time replica is started
  1286. nsds5ReplicaBindDN: <supplier update dn> consumers only
  1287. nsds5ReplicaReferral: <referral URL to updatable replica> consumers only
  1288. nsds5ReplicaPurgeDelay: <time, in seconds, to keep purgeable CSNs, 0 == keep forever>
  1289. nsds5ReplicaTombstonePurgeInterval: <time, in seconds, between tombstone purge runs, 0 == don't reap>
  1290. nsds5ReplicaLegacyConsumer: <TRUE | FALSE>
  1291. richm: changed slapi entry from const to editable - if the replica id is supplied for a read
  1292. only replica, we ignore it and replace the value with the READ_ONLY_REPLICA_ID
  1293. */
  1294. static int
  1295. _replica_init_from_config (Replica *r, Slapi_Entry *e, char *errortext)
  1296. {
  1297. int rc;
  1298. Slapi_Attr *attr;
  1299. char *val;
  1300. CSNGen *gen;
  1301. char buf [BUFSIZ];
  1302. char *errormsg = errortext? errortext : buf;
  1303. Slapi_Attr *a = NULL;
  1304. char dnescape[BUFSIZ]; /* for escape_string */
  1305. PR_ASSERT (r && e);
  1306. /* get replica root */
  1307. val = slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  1308. if (val == NULL)
  1309. {
  1310. sprintf (errormsg, "failed to retrieve %s attribute from (%s)\n",
  1311. attr_replicaRoot,
  1312. escape_string((char*)slapi_entry_get_dn ((Slapi_Entry*)e), dnescape));
  1313. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "_replica_init_from_config: %s\n",
  1314. errormsg);
  1315. return -1;
  1316. }
  1317. r->repl_root = slapi_sdn_new_dn_passin (val);
  1318. /* get replica type */
  1319. val = slapi_entry_attr_get_charptr (e, attr_replicaType);
  1320. if (val)
  1321. {
  1322. r->repl_type = atoi(val);
  1323. slapi_ch_free ((void**)&val);
  1324. }
  1325. else
  1326. {
  1327. r->repl_type = REPLICA_TYPE_READONLY;
  1328. }
  1329. /* get legacy consumer flag */
  1330. val = slapi_entry_attr_get_charptr (e, type_replicaLegacyConsumer);
  1331. if (val)
  1332. {
  1333. if (strcasecmp (val, "on") == 0 || strcasecmp (val, "yes") == 0 ||
  1334. strcasecmp (val, "true") == 0 || strcasecmp (val, "1") == 0)
  1335. {
  1336. r->legacy_consumer = PR_TRUE;
  1337. }
  1338. else
  1339. {
  1340. r->legacy_consumer = PR_FALSE;
  1341. }
  1342. slapi_ch_free ((void**)&val);
  1343. }
  1344. else
  1345. {
  1346. r->legacy_consumer = PR_FALSE;
  1347. }
  1348. /* get replica flags */
  1349. r->repl_flags = slapi_entry_attr_get_ulong(e, attr_flags);
  1350. /* get replicaid */
  1351. /* the replica id is ignored for read only replicas and is set to the
  1352. special value READ_ONLY_REPLICA_ID */
  1353. if (r->repl_type == REPLICA_TYPE_READONLY)
  1354. {
  1355. r->repl_rid = READ_ONLY_REPLICA_ID;
  1356. slapi_entry_attr_set_uint(e, attr_replicaId, (unsigned int)READ_ONLY_REPLICA_ID);
  1357. }
  1358. /* a replica id is required for updatable and primary replicas */
  1359. else if (r->repl_type == REPLICA_TYPE_UPDATABLE ||
  1360. r->repl_type == REPLICA_TYPE_PRIMARY)
  1361. {
  1362. if ((val = slapi_entry_attr_get_charptr (e, attr_replicaId)))
  1363. {
  1364. int temprid = atoi (val);
  1365. slapi_ch_free ((void**)&val);
  1366. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID)
  1367. {
  1368. sprintf (errormsg,
  1369. "attribute %s must have a value greater than 0 "
  1370. "and less than %d: entry %s",
  1371. attr_replicaId, READ_ONLY_REPLICA_ID,
  1372. escape_string((char*)slapi_entry_get_dn ((Slapi_Entry*)e),
  1373. dnescape));
  1374. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1375. "_replica_init_from_config: %s\n",
  1376. errormsg);
  1377. return -1;
  1378. }
  1379. else
  1380. {
  1381. r->repl_rid = (ReplicaId)temprid;
  1382. }
  1383. }
  1384. else
  1385. {
  1386. sprintf (errormsg, "failed to retrieve required %s attribute from %s",
  1387. attr_replicaId,
  1388. escape_string((char*)slapi_entry_get_dn ((Slapi_Entry*)e),
  1389. dnescape));
  1390. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1391. "_replica_init_from_config: %s\n",
  1392. errormsg);
  1393. return -1;
  1394. }
  1395. }
  1396. attr = NULL;
  1397. rc = slapi_entry_attr_find(e, attr_state, &attr);
  1398. gen = csngen_new (r->repl_rid, attr);
  1399. if (gen == NULL)
  1400. {
  1401. sprintf (errormsg, "failed to create csn generator for replica (%s)",
  1402. escape_string((char*)slapi_entry_get_dn ((Slapi_Entry*)e),
  1403. dnescape));
  1404. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1405. "_replica_init_from_config: %s\n",
  1406. errormsg);
  1407. return -1;
  1408. }
  1409. r->repl_csngen = object_new((void*)gen, (FNFree)csngen_free);
  1410. /* Hook generator so we can maintain min/max CSN info */
  1411. r->csn_pl_reg_id = csngen_register_callbacks(gen, assign_csn_callback, r, abort_csn_callback, r);
  1412. /* get replication bind dn */
  1413. r->updatedn_list = replica_updatedn_list_new(e);
  1414. /* get replica name */
  1415. val = slapi_entry_attr_get_charptr (e, attr_replicaName);
  1416. if (val) {
  1417. r->repl_name = val;
  1418. }
  1419. else
  1420. {
  1421. rc = slapi_uniqueIDGenerateString (&r->repl_name);
  1422. if (rc != UID_SUCCESS)
  1423. {
  1424. sprintf (errormsg, "failed to assign replica name for replica (%s); "
  1425. "uuid generator error - %d ",
  1426. escape_string((char*)slapi_entry_get_dn ((Slapi_Entry*)e), dnescape),
  1427. rc);
  1428. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "_replica_init_from_config: %s\n",
  1429. errormsg);
  1430. return -1;
  1431. }
  1432. else
  1433. r->new_name = PR_TRUE;
  1434. }
  1435. /* get the list of referrals */
  1436. slapi_entry_attr_find( e, attr_replicaReferral, &attr );
  1437. if(attr!=NULL)
  1438. {
  1439. slapi_attr_get_valueset(attr, &r->repl_referral);
  1440. }
  1441. /*
  1442. * Set the purge offset (default 7 days). This is the extra
  1443. * time we allow purgeable CSNs to stick around, in case a
  1444. * replica regresses. Could also be useful when LCUP happens,
  1445. * since we don't know about LCUP replicas, and they can just
  1446. * turn up whenever they want to.
  1447. */
  1448. if (slapi_entry_attr_find(e, type_replicaPurgeDelay, &a) == -1)
  1449. {
  1450. /* No purge delay provided, so use default */
  1451. r->repl_purge_delay = 60 * 60 * 24 * 7; /* One week, in seconds */
  1452. }
  1453. else
  1454. {
  1455. r->repl_purge_delay = slapi_entry_attr_get_uint(e, type_replicaPurgeDelay);
  1456. }
  1457. if (slapi_entry_attr_find(e, type_replicaTombstonePurgeInterval, &a) == -1)
  1458. {
  1459. /* No reap interval provided, so use default */
  1460. r->tombstone_reap_interval = 3600 * 24; /* One day */
  1461. }
  1462. else
  1463. {
  1464. r->tombstone_reap_interval = slapi_entry_attr_get_int(e, type_replicaTombstonePurgeInterval);
  1465. }
  1466. r->tombstone_reap_stop = r->tombstone_reap_active = PR_FALSE;
  1467. return (_replica_check_validity (r));
  1468. }
  1469. /* This function updates the entry to contain information generated
  1470. during replica initialization.
  1471. Returns 0 if successful and -1 otherwise */
  1472. static int
  1473. _replica_update_entry (Replica *r, Slapi_Entry *e, char *errortext)
  1474. {
  1475. int rc;
  1476. Slapi_Mod smod;
  1477. Slapi_Value *val;
  1478. PR_ASSERT (r);
  1479. /* add attribute that stores state of csn generator */
  1480. rc = csngen_get_state ((CSNGen*)object_get_data (r->repl_csngen), &smod);
  1481. if (rc != CSN_SUCCESS)
  1482. {
  1483. sprintf (errortext, "failed to get csn generator's state; csn error - %d", rc);
  1484. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1485. "_replica_update_entry: %s\n", errortext);
  1486. return -1;
  1487. }
  1488. val = slapi_value_new_berval(slapi_mod_get_first_value(&smod));
  1489. rc = slapi_entry_add_value (e, slapi_mod_get_type (&smod), val);
  1490. slapi_value_free(&val);
  1491. slapi_mod_done (&smod);
  1492. if (rc != 0)
  1493. {
  1494. sprintf (errortext, "failed to update replica entry");
  1495. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1496. "_replica_update_entry: %s\n", errortext);
  1497. return -1;
  1498. }
  1499. /* add attribute that stores replica name */
  1500. rc = slapi_entry_add_string (e, attr_replicaName, r->repl_name);
  1501. if (rc != 0)
  1502. {
  1503. sprintf (errortext, "failed to update replica entry");
  1504. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1505. "_replica_update_entry: %s\n", errortext);
  1506. return -1;
  1507. }
  1508. else
  1509. r->new_name = PR_FALSE;
  1510. return 0;
  1511. }
  1512. /* DN format: cn=replica,cn=\"<root>\",cn=mapping tree,cn=config */
  1513. static char*
  1514. _replica_get_config_dn (const Slapi_DN *root)
  1515. {
  1516. char *dn;
  1517. const char *mp_base = slapi_get_mapping_tree_config_root ();
  1518. int len;
  1519. PR_ASSERT (root);
  1520. len = strlen (REPLICA_RDN) + strlen (slapi_sdn_get_dn (root)) +
  1521. strlen (mp_base) + 8; /* 8 = , + cn= + \" + \" + , + \0 */
  1522. dn = (char*)slapi_ch_malloc (len);
  1523. sprintf (dn, "%s,cn=\"%s\",%s", REPLICA_RDN, slapi_sdn_get_dn (root), mp_base);
  1524. return dn;
  1525. }
  1526. /* This function retrieves RUV from the root of the replicated tree.
  1527. * The attribute can be missing if
  1528. * (1) this replica is the first supplier and replica generation has not been assigned
  1529. * or
  1530. * (2) this is a consumer that has not been yet initialized
  1531. * In either case, replica_set_ruv should be used to further initialize the replica.
  1532. * Returns 0 on success, -1 on failure. If 0 is returned, the RUV is present in the replica.
  1533. */
  1534. static int
  1535. _replica_configure_ruv (Replica *r, PRBool isLocked)
  1536. {
  1537. Slapi_PBlock *pb = NULL;
  1538. char *attrs[2];
  1539. int rc;
  1540. int return_value = -1;
  1541. Slapi_Entry **entries = NULL;
  1542. Slapi_Attr *attr;
  1543. RUV *ruv = NULL;
  1544. CSN *csn = NULL;
  1545. ReplicaId rid = 0;
  1546. char ebuf[BUFSIZ];
  1547. /* read ruv state from the ruv tombstone entry */
  1548. pb = slapi_pblock_new();
  1549. attrs[0] = (char*)type_ruvElement;
  1550. attrs[1] = NULL;
  1551. slapi_search_internal_set_pb(
  1552. pb,
  1553. slapi_sdn_get_dn(r->repl_root),
  1554. LDAP_SCOPE_BASE,
  1555. "objectclass=*",
  1556. attrs,
  1557. 0, /* attrsonly */
  1558. NULL, /* controls */
  1559. RUV_STORAGE_ENTRY_UNIQUEID,
  1560. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  1561. OP_FLAG_REPLICATED); /* flags */
  1562. slapi_search_internal_pb (pb);
  1563. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1564. if (rc == LDAP_SUCCESS)
  1565. {
  1566. /* get RUV attributes and construct the RUV */
  1567. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1568. if (NULL == entries || NULL == entries[0])
  1569. {
  1570. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1571. "_replica_configure_ruv: replica ruv tombstone entry for "
  1572. "replica %s not found\n",
  1573. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  1574. goto done;
  1575. }
  1576. rc = slapi_entry_attr_find(entries[0], type_ruvElement, &attr);
  1577. if (rc != 0) /* ruv attribute is missing - this not allowed */
  1578. {
  1579. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1580. "_replica_configure_ruv: replica ruv tombstone entry for "
  1581. "replica %s does not contain %s\n",
  1582. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf), type_ruvElement);
  1583. goto done;
  1584. }
  1585. /* Check in the tombstone we have retrieved if the local purl is
  1586. already present:
  1587. rid == 0: the local purl is not present
  1588. rid != 0: the local purl is present ==> nothing to do
  1589. */
  1590. ruv_init_from_slapi_attr_and_check_purl (attr, &ruv, &rid);
  1591. if (ruv)
  1592. {
  1593. char *generation = NULL;
  1594. generation = ruv_get_replica_generation(ruv);
  1595. if (NULL != generation)
  1596. {
  1597. r->repl_ruv = object_new((void*)ruv, (FNFree)ruv_destroy);
  1598. /* Is the local purl in the ruv? (the port or the host could have
  1599. changed)
  1600. */
  1601. /* A consumer only doesn't have its purl in its ruv */
  1602. if (r->repl_type == REPLICA_TYPE_UPDATABLE)
  1603. {
  1604. int need_update = 0;
  1605. if (rid == 0)
  1606. {
  1607. /* We can not have more than 1 ruv with the same rid
  1608. so we replace it */
  1609. const char *purl = NULL;
  1610. purl = multimaster_get_local_purl();
  1611. ruv_delete_replica(ruv, r->repl_rid);
  1612. ruv_add_index_replica(ruv, r->repl_rid, purl, 1);
  1613. need_update = 1; /* ruv changed, so write tombstone */
  1614. }
  1615. else /* bug 540844: make sure the local supplier rid is first in the ruv */
  1616. {
  1617. /* make sure local supplier is first in list */
  1618. ReplicaId first_rid = 0;
  1619. char *first_purl = NULL;
  1620. ruv_get_first_id_and_purl(ruv, &first_rid, &first_purl);
  1621. /* if the local supplier is not first in the list . . . */
  1622. if (rid != first_rid)
  1623. {
  1624. /* . . . move the local supplier to the beginning of the list */
  1625. ruv_move_local_supplier_to_first(ruv, rid);
  1626. need_update = 1; /* must update tombstone also */
  1627. }
  1628. }
  1629. /* Update also the directory entry */
  1630. if (need_update) {
  1631. /* richm 20010821 bug 556498
  1632. replica_replace_ruv_tombstone acquires the repl_lock, so release
  1633. the lock then reacquire it if locked */
  1634. if (isLocked) PR_Unlock(r->repl_lock);
  1635. replica_replace_ruv_tombstone(r);
  1636. if (isLocked) PR_Lock(r->repl_lock);
  1637. }
  1638. }
  1639. slapi_ch_free((void **)&generation);
  1640. return_value = 0;
  1641. }
  1642. else
  1643. {
  1644. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1645. "RUV for replica %s is missing replica generation\n",
  1646. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  1647. goto done;
  1648. }
  1649. }
  1650. else
  1651. {
  1652. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1653. "Unable to convert %s attribute in entry %s to a replica update vector.\n",
  1654. type_ruvElement, escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  1655. goto done;
  1656. }
  1657. }
  1658. else /* search failed */
  1659. {
  1660. if (LDAP_NO_SUCH_OBJECT == rc)
  1661. {
  1662. /* The entry doesn't exist: create it */
  1663. rc = replica_create_ruv_tombstone(r);
  1664. if (LDAP_SUCCESS != rc)
  1665. {
  1666. /*
  1667. * XXXggood - the following error appears on startup if we try
  1668. * to initialize replica RUVs before the backend instance is up.
  1669. * It's alarming to see this error, and we should suppress it
  1670. * (or avoid trying to configure it) if the backend instance is
  1671. * not yet online.
  1672. */
  1673. /*
  1674. * XXXrichm - you can also get this error when the backend is in
  1675. * read only mode c.f. bug 539782
  1676. */
  1677. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1678. "_replica_configure_ruv: failed to create replica ruv tombstone "
  1679. "entry (%s); LDAP error - %d\n",
  1680. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf), rc);
  1681. goto done;
  1682. }
  1683. else
  1684. {
  1685. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1686. "_replica_configure_ruv: No ruv tombstone found for replica %s. "
  1687. "Created a new one\n",
  1688. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  1689. return_value = 0;
  1690. }
  1691. }
  1692. else
  1693. {
  1694. /* see if the suffix is disabled */
  1695. char *state = slapi_mtn_get_state(r->repl_root);
  1696. if (state && !strcasecmp(state, "disabled"))
  1697. {
  1698. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1699. "_replica_configure_ruv: replication disabled for "
  1700. "entry (%s); LDAP error - %d\n",
  1701. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf), rc);
  1702. slapi_ch_free_string(&state);
  1703. goto done;
  1704. }
  1705. else if (!r->repl_ruv) /* other error */
  1706. {
  1707. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1708. "_replica_configure_ruv: replication broken for "
  1709. "entry (%s); LDAP error - %d\n",
  1710. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf), rc);
  1711. slapi_ch_free_string(&state);
  1712. goto done;
  1713. }
  1714. else /* some error but continue anyway? */
  1715. {
  1716. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1717. "_replica_configure_ruv: Error %d reading tombstone for replica %s.\n",
  1718. rc, escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  1719. return_value = 0;
  1720. }
  1721. slapi_ch_free_string(&state);
  1722. }
  1723. }
  1724. if (NULL != r->min_csn_pl)
  1725. {
  1726. csnplFree (&r->min_csn_pl);
  1727. }
  1728. /* create pending list for min csn if necessary */
  1729. if (ruv_get_smallest_csn_for_replica ((RUV*)object_get_data (r->repl_ruv),
  1730. r->repl_rid, &csn) == RUV_SUCCESS)
  1731. {
  1732. csn_free (&csn);
  1733. r->min_csn_pl = NULL;
  1734. }
  1735. else
  1736. {
  1737. /*
  1738. * The local replica has not generated any of its own CSNs yet.
  1739. * We need to watch CSNs being generated and note the first
  1740. * locally-generated CSN that's committed. Once that event occurs,
  1741. * the RUV is suitable for iteration over locally generated
  1742. * changes.
  1743. */
  1744. r->min_csn_pl = csnplNew();
  1745. }
  1746. done:
  1747. if (NULL != pb)
  1748. {
  1749. slapi_free_search_results_internal(pb);
  1750. slapi_pblock_destroy (pb);
  1751. }
  1752. if (return_value != 0)
  1753. {
  1754. if (ruv)
  1755. ruv_destroy (&ruv);
  1756. }
  1757. return return_value;
  1758. }
  1759. /* NOTE - this is the only non-api function that performs locking because
  1760. it is called by the event queue */
  1761. static void
  1762. _replica_update_state (time_t when, void *arg)
  1763. {
  1764. int rc;
  1765. const char *replica_name = (const char *)arg;
  1766. Object *replica_object = NULL;
  1767. Replica *r;
  1768. Slapi_Mod smod;
  1769. LDAPMod *mods[3];
  1770. Slapi_PBlock *pb = NULL;
  1771. char *dn = NULL;
  1772. if (NULL == replica_name)
  1773. return;
  1774. /*
  1775. * replica_get_by_name() will acquire the replica object
  1776. * and that could prevent the replica from being destroyed
  1777. * until the object_release is called.
  1778. */
  1779. replica_object = replica_get_by_name(replica_name);
  1780. if (NULL == replica_object)
  1781. {
  1782. return;
  1783. }
  1784. /* We have a reference, so replica won't vanish on us. */
  1785. r = (Replica *)object_get_data(replica_object);
  1786. if (NULL == r)
  1787. {
  1788. goto done;
  1789. }
  1790. PR_Lock(r->repl_lock);
  1791. /* replica state is currently being updated
  1792. or no CSN was assigned - bail out */
  1793. if (r->state_update_inprogress)
  1794. {
  1795. PR_Unlock(r->repl_lock);
  1796. goto done;
  1797. }
  1798. /* This might be a consumer */
  1799. if (!r->repl_csn_assigned)
  1800. {
  1801. /* EY: the consumer needs to flush ruv to disk. */
  1802. PR_Unlock(r->repl_lock);
  1803. replica_write_ruv(r);
  1804. goto done;
  1805. }
  1806. /* ONREPL update csn generator state of an updatable replica only */
  1807. /* ONREPL state always changes because we update time every second and
  1808. we write state to the disk less frequently */
  1809. rc = csngen_get_state ((CSNGen*)object_get_data (r->repl_csngen), &smod);
  1810. if (rc != 0)
  1811. {
  1812. PR_Unlock(r->repl_lock);
  1813. goto done;
  1814. }
  1815. r->state_update_inprogress = PR_TRUE;
  1816. r->repl_csn_assigned = PR_FALSE;
  1817. dn = _replica_get_config_dn (r->repl_root);
  1818. pb = slapi_pblock_new();
  1819. mods[0] = (LDAPMod*)slapi_mod_get_ldapmod_byref(&smod);
  1820. /* we don't want to held lock during operations since it causes lock contention
  1821. and sometimes deadlock. So releasing lock here */
  1822. PR_Unlock(r->repl_lock);
  1823. /* replica repl_name and new_name attributes do not get changed once
  1824. the replica is configured - so it is ok that they are outside replica lock */
  1825. /* write replica name if it has not been written before */
  1826. if (r->new_name)
  1827. {
  1828. struct berval *vals[2];
  1829. struct berval val;
  1830. LDAPMod mod;
  1831. mods[1] = &mod;
  1832. mod.mod_op = LDAP_MOD_REPLACE;
  1833. mod.mod_type = (char*)attr_replicaName;
  1834. mod.mod_bvalues = vals;
  1835. vals [0] = &val;
  1836. vals [1] = NULL;
  1837. val.bv_val = r->repl_name;
  1838. val.bv_len = strlen (val.bv_val);
  1839. mods[2] = NULL;
  1840. }
  1841. else
  1842. {
  1843. mods[1] = NULL;
  1844. }
  1845. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  1846. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  1847. slapi_modify_internal_pb (pb);
  1848. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1849. if (rc != LDAP_SUCCESS)
  1850. {
  1851. char ebuf[BUFSIZ];
  1852. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "_replica_update_state: "
  1853. "failed to update state of csn generator for replica %s: LDAP "
  1854. "error - %d\n", escape_string(slapi_sdn_get_dn(r->repl_root),ebuf), rc);
  1855. }
  1856. else
  1857. {
  1858. r->new_name = PR_FALSE;
  1859. }
  1860. /* update RUV - performs its own locking */
  1861. replica_write_ruv (r);
  1862. /* since this is the only place this value is changed and we are
  1863. guaranteed that only one thread enters the function, its ok
  1864. to change it outside replica lock */
  1865. r->state_update_inprogress = PR_FALSE;
  1866. slapi_ch_free ((void**)&dn);
  1867. slapi_pblock_destroy (pb);
  1868. slapi_mod_done (&smod);
  1869. done:
  1870. if (replica_object)
  1871. object_release (replica_object);
  1872. }
  1873. void
  1874. replica_write_ruv (Replica *r)
  1875. {
  1876. int rc;
  1877. Slapi_Mod smod;
  1878. Slapi_Mod smod_last_modified;
  1879. LDAPMod *mods [3];
  1880. Slapi_PBlock *pb;
  1881. PR_ASSERT(r);
  1882. PR_Lock(r->repl_lock);
  1883. if (!r->repl_ruv_dirty)
  1884. {
  1885. PR_Unlock(r->repl_lock);
  1886. return;
  1887. }
  1888. PR_ASSERT (r->repl_ruv);
  1889. ruv_to_smod ((RUV*)object_get_data(r->repl_ruv), &smod);
  1890. ruv_last_modified_to_smod ((RUV*)object_get_data(r->repl_ruv), &smod_last_modified);
  1891. PR_Unlock (r->repl_lock);
  1892. mods [0] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod);
  1893. mods [1] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod_last_modified);
  1894. mods [2] = NULL;
  1895. pb = slapi_pblock_new();
  1896. /* replica name never changes so it is ok to reference it outside the lock */
  1897. slapi_modify_internal_set_pb(
  1898. pb,
  1899. slapi_sdn_get_dn(r->repl_root), /* only used to select be */
  1900. mods,
  1901. NULL, /* controls */
  1902. RUV_STORAGE_ENTRY_UNIQUEID,
  1903. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  1904. /* Add OP_FLAG_TOMBSTONE_ENTRY so that this doesn't get logged in the Retro ChangeLog */
  1905. OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | OP_FLAG_TOMBSTONE_ENTRY);
  1906. slapi_modify_internal_pb (pb);
  1907. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1908. /* ruv does not exist - create one */
  1909. PR_Lock(r->repl_lock);
  1910. if (rc == LDAP_SUCCESS)
  1911. {
  1912. r->repl_ruv_dirty = PR_FALSE;
  1913. }
  1914. else if (rc == LDAP_NO_SUCH_OBJECT)
  1915. {
  1916. /* this includes an internal operation - but since this only happens
  1917. during server startup - its ok that we have lock around it */
  1918. rc = _replica_configure_ruv (r, PR_TRUE);
  1919. if (rc == 0)
  1920. r->repl_ruv_dirty = PR_FALSE;
  1921. }
  1922. else /* error */
  1923. {
  1924. char ebuf[BUFSIZ];
  1925. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1926. "replica_write_ruv: failed to update RUV tombstone for %s; "
  1927. "LDAP error - %d\n",
  1928. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf), rc);
  1929. PR_ASSERT (0);
  1930. }
  1931. PR_Unlock(r->repl_lock);
  1932. slapi_mod_done (&smod);
  1933. slapi_mod_done (&smod_last_modified);
  1934. slapi_pblock_destroy (pb);
  1935. }
  1936. const CSN *
  1937. _get_deletion_csn(Slapi_Entry *e)
  1938. {
  1939. const CSN *deletion_csn = NULL;
  1940. PR_ASSERT(NULL != e);
  1941. if (NULL != e)
  1942. {
  1943. Slapi_Attr *oc_attr = NULL;
  1944. if (entry_attr_find_wsi(e, SLAPI_ATTR_OBJECTCLASS, &oc_attr) == ATTRIBUTE_PRESENT)
  1945. {
  1946. Slapi_Value *tombstone_value = NULL;
  1947. struct berval v;
  1948. v.bv_val = SLAPI_ATTR_VALUE_TOMBSTONE;
  1949. v.bv_len = strlen(SLAPI_ATTR_VALUE_TOMBSTONE);
  1950. if (attr_value_find_wsi(oc_attr, &v, &tombstone_value) == VALUE_PRESENT)
  1951. {
  1952. deletion_csn = value_get_csn(tombstone_value, CSN_TYPE_VALUE_UPDATED);
  1953. }
  1954. }
  1955. }
  1956. return deletion_csn;
  1957. }
  1958. static void
  1959. _delete_tombstone(const char *tombstone_dn, const char *uniqueid)
  1960. {
  1961. PR_ASSERT(NULL != tombstone_dn && NULL != uniqueid);
  1962. if (NULL == tombstone_dn || NULL == uniqueid)
  1963. {
  1964. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "_delete_tombstone: "
  1965. "NULL tombstone_dn or uniqueid provided.\n");
  1966. }
  1967. else
  1968. {
  1969. int ldaprc;
  1970. Slapi_PBlock *pb = slapi_pblock_new();
  1971. slapi_delete_internal_set_pb(pb, tombstone_dn, NULL, /* controls */
  1972. uniqueid, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  1973. OP_FLAG_TOMBSTONE_ENTRY);
  1974. slapi_delete_internal_pb(pb);
  1975. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ldaprc);
  1976. if (LDAP_SUCCESS != ldaprc)
  1977. {
  1978. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1979. "_delete_tombstone: unable to delete tombstone %s, "
  1980. "uniqueid %s: %s.\n", tombstone_dn, uniqueid,
  1981. ldap_err2string(ldaprc));
  1982. }
  1983. slapi_pblock_destroy(pb);
  1984. }
  1985. }
  1986. static
  1987. void get_reap_result (int rc, void *cb_data)
  1988. {
  1989. PR_ASSERT (cb_data);
  1990. ((reap_callback_data*)cb_data)->rc = rc;
  1991. }
  1992. static
  1993. int process_reap_entry (Slapi_Entry *entry, void *cb_data)
  1994. {
  1995. char ebuf[BUFSIZ];
  1996. char deletion_csn_str[CSN_STRSIZE];
  1997. char purge_csn_str[CSN_STRSIZE];
  1998. unsigned long *num_entriesp = &((reap_callback_data *)cb_data)->num_entries;
  1999. unsigned long *num_purged_entriesp = &((reap_callback_data *)cb_data)->num_purged_entries;
  2000. CSN *purge_csn = ((reap_callback_data *)cb_data)->purge_csn;
  2001. /* this is a pointer into the actual value in the Replica object - so that
  2002. if the value is set in the replica, we will know about it immediately */
  2003. PRBool *tombstone_reap_stop = ((reap_callback_data *)cb_data)->tombstone_reap_stop;
  2004. const CSN *deletion_csn = NULL;
  2005. /* abort reaping if we've been told to stop or we're shutting down */
  2006. if (*tombstone_reap_stop || g_get_shutdown()) {
  2007. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2008. "_replica_reap_tombstones: the tombstone reap process "
  2009. " has been stopped\n");
  2010. return -1;
  2011. }
  2012. /* we only ask for the objectclass in the search - the deletion csn is in the
  2013. objectclass attribute values - if we need more attributes returned by the
  2014. search in the future, see _replica_reap_tombstones below and add more to the
  2015. attrs array */
  2016. deletion_csn = _get_deletion_csn(entry);
  2017. if ((NULL == deletion_csn || csn_compare(deletion_csn, purge_csn) < 0) &&
  2018. (!is_ruv_tombstone_entry(entry))) {
  2019. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2020. "_replica_reap_tombstones: removing tombstone %s "
  2021. "because its deletion csn (%s) is less than the "
  2022. "purge csn (%s).\n",
  2023. escape_string(slapi_entry_get_dn(entry), ebuf),
  2024. csn_as_string(deletion_csn, PR_FALSE, deletion_csn_str),
  2025. csn_as_string(purge_csn, PR_FALSE, purge_csn_str));
  2026. _delete_tombstone(slapi_entry_get_dn(entry),
  2027. slapi_entry_get_uniqueid(entry));
  2028. (*num_purged_entriesp)++;
  2029. }
  2030. else {
  2031. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2032. "_replica_reap_tombstones: NOT removing tombstone "
  2033. "%s\n", escape_string(slapi_entry_get_dn(entry),ebuf));
  2034. }
  2035. (*num_entriesp)++;
  2036. return 0;
  2037. }
  2038. /* This does the actual work of searching for tombstones and deleting them.
  2039. This must be called in a separate thread because it may take a long time.
  2040. */
  2041. static void
  2042. _replica_reap_tombstones(void *arg)
  2043. {
  2044. const char *replica_name = (const char *)arg;
  2045. Slapi_PBlock *pb = NULL;
  2046. Object *replica_object = NULL;
  2047. Replica *replica = NULL;
  2048. CSN *purge_csn = NULL;
  2049. char ebuf[BUFSIZ];
  2050. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2051. "Info: Beginning tombstone reap for replica %s.\n",
  2052. replica_name ? replica_name : "(null)");
  2053. if (NULL == replica_name)
  2054. {
  2055. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2056. "Warning: Replica name is null in tombstone reap\n");
  2057. goto done;
  2058. }
  2059. /*
  2060. * replica_get_by_name() will acquire the replica object
  2061. * and that could prevent the replica from being destroyed
  2062. * until the object_release is called.
  2063. */
  2064. replica_object = replica_get_by_name(replica_name);
  2065. if (NULL == replica_object)
  2066. {
  2067. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2068. "Warning: Replica object %s is null in tombstone reap\n", replica_name);
  2069. goto done;
  2070. }
  2071. /* We have a reference, so replica won't vanish on us. */
  2072. replica = (Replica *)object_get_data(replica_object);
  2073. if (NULL == replica)
  2074. {
  2075. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2076. "Warning: Replica %s is null in tombstone reap\n", replica_name);
  2077. goto done;
  2078. }
  2079. if (replica->tombstone_reap_stop)
  2080. {
  2081. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2082. "Info: Replica %s reap stop flag is set for tombstone reap\n", replica_name);
  2083. goto done;
  2084. }
  2085. purge_csn = replica_get_purge_csn(replica);
  2086. if (NULL != purge_csn)
  2087. {
  2088. LDAPControl **ctrls;
  2089. int oprc;
  2090. reap_callback_data cb_data;
  2091. char **attrs = NULL;
  2092. /* we just need the objectclass - for the deletion csn
  2093. and the dn and nsuniqueid - for possible deletion
  2094. saves time to return only 2 attrs
  2095. */
  2096. charray_add(&attrs, slapi_ch_strdup("objectclass"));
  2097. charray_add(&attrs, slapi_ch_strdup("nsuniqueid"));
  2098. ctrls = (LDAPControl **)slapi_ch_calloc (3, sizeof (LDAPControl *));
  2099. ctrls[0] = create_managedsait_control();
  2100. ctrls[1] = create_backend_control(replica->repl_root);
  2101. ctrls[2] = NULL;
  2102. pb = slapi_pblock_new();
  2103. slapi_search_internal_set_pb(pb, slapi_sdn_get_dn(replica->repl_root),
  2104. LDAP_SCOPE_SUBTREE, "(&(objectclass=nstombstone)(nscpentrydn=*))",
  2105. attrs, 0, ctrls, NULL,
  2106. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  2107. cb_data.rc = 0;
  2108. cb_data.num_entries = 0UL;
  2109. cb_data.num_purged_entries = 0UL;
  2110. cb_data.purge_csn = purge_csn;
  2111. /* set the cb data pointer to point to the actual memory address in
  2112. the actual Replica object - so that when the value in the Replica
  2113. is set, the reap process will know about it immediately */
  2114. cb_data.tombstone_reap_stop = &(replica->tombstone_reap_stop);
  2115. slapi_search_internal_callback_pb (pb, &cb_data /* callback data */,
  2116. get_reap_result /* result callback */,
  2117. process_reap_entry /* entry callback */,
  2118. NULL /* referral callback*/);
  2119. charray_free(attrs);
  2120. oprc = cb_data.rc;
  2121. if (LDAP_SUCCESS != oprc)
  2122. {
  2123. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2124. "_replica_reap_tombstones: failed when searching for "
  2125. "tombstones in replica %s: %s. Will try again in %d "
  2126. "seconds.\n", escape_string(slapi_sdn_get_dn(replica->repl_root),ebuf),
  2127. ldap_err2string(oprc), replica->tombstone_reap_interval);
  2128. }
  2129. else
  2130. {
  2131. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2132. "_replica_reap_tombstones: purged %d of %d tombstones "
  2133. "in replica %s. Will try again in %d "
  2134. "seconds.\n", cb_data.num_purged_entries, cb_data.num_entries,
  2135. escape_string(slapi_sdn_get_dn(replica->repl_root),ebuf),
  2136. replica->tombstone_reap_interval);
  2137. }
  2138. }
  2139. else
  2140. {
  2141. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2142. "Info: No purge CSN for tombstone reap for replica %s.\n",
  2143. replica_name ? replica_name : "(null)");
  2144. }
  2145. done:
  2146. PR_Lock(replica->repl_lock);
  2147. replica->tombstone_reap_active = PR_FALSE;
  2148. PR_Unlock(replica->repl_lock);
  2149. if (NULL != purge_csn)
  2150. {
  2151. csn_free(&purge_csn);
  2152. }
  2153. if (NULL != pb)
  2154. {
  2155. slapi_free_search_results_internal(pb);
  2156. slapi_pblock_destroy(pb);
  2157. }
  2158. if (NULL != replica_object)
  2159. {
  2160. object_release(replica_object);
  2161. replica_object = NULL;
  2162. replica = NULL;
  2163. }
  2164. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2165. "Info: Finished tombstone reap for replica %s.\n",
  2166. replica_name ? replica_name : "(null)");
  2167. }
  2168. /*
  2169. We don't want to run the reaper function directly from the event
  2170. queue since it may hog the event queue, starving other events.
  2171. See bug 604441
  2172. The function eq_cb_reap_tombstones will fire off the actual thread
  2173. that does the real work.
  2174. */
  2175. static void
  2176. eq_cb_reap_tombstones(time_t when, void *arg)
  2177. {
  2178. const char *replica_name = (const char *)arg;
  2179. Object *replica_object = NULL;
  2180. Replica *replica = NULL;
  2181. if (NULL != replica_name)
  2182. {
  2183. /*
  2184. * replica_get_by_name() will acquire the replica object
  2185. * and that could prevent the replica from being destroyed
  2186. * until the object_release is called.
  2187. */
  2188. replica_object = replica_get_by_name(replica_name);
  2189. if (NULL != replica_object)
  2190. {
  2191. /* We have a reference, so replica won't vanish on us. */
  2192. replica = (Replica *)object_get_data(replica_object);
  2193. if (replica)
  2194. {
  2195. PR_Lock(replica->repl_lock);
  2196. /* No action if purge is disabled or the previous purge is not done yet */
  2197. if (replica->tombstone_reap_interval != 0 &&
  2198. replica->tombstone_reap_active == PR_FALSE)
  2199. {
  2200. /* set the flag here to minimize race conditions */
  2201. replica->tombstone_reap_active = PR_TRUE;
  2202. if (PR_CreateThread(PR_USER_THREAD,
  2203. _replica_reap_tombstones, (void *)replica_name,
  2204. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD,
  2205. SLAPD_DEFAULT_THREAD_STACKSIZE) == NULL)
  2206. {
  2207. replica->tombstone_reap_active = PR_FALSE;
  2208. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2209. "Error: unable to create the tombstone reap thread for replica %s. "
  2210. "Possible system resources problem\n",
  2211. replica_name);
  2212. }
  2213. }
  2214. /* reap thread will wait until this lock is released */
  2215. PR_Unlock(replica->repl_lock);
  2216. }
  2217. object_release(replica_object);
  2218. replica_object = NULL;
  2219. replica = NULL;
  2220. }
  2221. }
  2222. }
  2223. static char *
  2224. _replica_type_as_string (const Replica *r)
  2225. {
  2226. switch (r->repl_type)
  2227. {
  2228. case REPLICA_TYPE_PRIMARY: return "primary";
  2229. case REPLICA_TYPE_READONLY: return "read-only";
  2230. case REPLICA_TYPE_UPDATABLE: return "updatable";
  2231. default: return "unknown";
  2232. }
  2233. }
  2234. static const char *root_glue =
  2235. "dn: %s\n"
  2236. "objectclass: top\n"
  2237. "objectclass: nsTombstone\n"
  2238. "objectclass: extensibleobject\n"
  2239. "nsuniqueid: %s\n";
  2240. static int
  2241. replica_create_ruv_tombstone(Replica *r)
  2242. {
  2243. int return_value = LDAP_LOCAL_ERROR;
  2244. char *root_entry_str;
  2245. Slapi_Entry *e;
  2246. const char *purl = NULL;
  2247. RUV *ruv;
  2248. struct berval **bvals = NULL;
  2249. Slapi_PBlock *pb = NULL;
  2250. int rc;
  2251. char ebuf[BUFSIZ];
  2252. PR_ASSERT(NULL != r && NULL != r->repl_root);
  2253. root_entry_str = slapi_ch_malloc(strlen(root_glue) +
  2254. slapi_sdn_get_ndn_len(r->repl_root) +
  2255. strlen(RUV_STORAGE_ENTRY_UNIQUEID) + 1);
  2256. sprintf(root_entry_str, root_glue, slapi_sdn_get_ndn(r->repl_root),
  2257. RUV_STORAGE_ENTRY_UNIQUEID);
  2258. e = slapi_str2entry(root_entry_str, SLAPI_STR2ENTRY_TOMBSTONE_CHECK);
  2259. if (e == NULL)
  2260. goto done;
  2261. /* Add ruv */
  2262. if (r->repl_ruv == NULL)
  2263. {
  2264. CSNGen *gen;
  2265. CSN *csn;
  2266. char csnstr [CSN_STRSIZE];
  2267. /* first attempt to write RUV tombstone - need to create RUV */
  2268. gen = (CSNGen *)object_get_data(r->repl_csngen);
  2269. PR_ASSERT (gen);
  2270. if (csngen_new_csn(gen, &csn, PR_FALSE /* notify */) == CSN_SUCCESS)
  2271. {
  2272. (void)csn_as_string(csn, PR_FALSE, csnstr);
  2273. csn_free(&csn);
  2274. /* if this is an updateable replica - add its own
  2275. element to the RUV so that referrals work correctly */
  2276. if (r->repl_type == REPLICA_TYPE_UPDATABLE)
  2277. purl = multimaster_get_local_purl();
  2278. if (ruv_init_new(csnstr, r->repl_rid, purl, &ruv) == RUV_SUCCESS)
  2279. {
  2280. r->repl_ruv = object_new((void*)ruv, (FNFree)ruv_destroy);
  2281. r->repl_ruv_dirty = PR_TRUE;
  2282. return_value = LDAP_SUCCESS;
  2283. }
  2284. else
  2285. {
  2286. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2287. "Cannot create new replica update vector for %s\n",
  2288. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  2289. goto done;
  2290. }
  2291. }
  2292. else
  2293. {
  2294. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2295. "Cannot obtain CSN for new replica update vector for %s\n",
  2296. escape_string(slapi_sdn_get_dn(r->repl_root),ebuf));
  2297. goto done;
  2298. }
  2299. }
  2300. else /* failed to write the entry because DB was not initialized - retry */
  2301. {
  2302. ruv = (RUV*) object_get_data (r->repl_ruv);
  2303. PR_ASSERT (ruv);
  2304. }
  2305. PR_ASSERT (r->repl_ruv);
  2306. rc = ruv_to_bervals(ruv, &bvals);
  2307. if (rc != RUV_SUCCESS)
  2308. {
  2309. goto done;
  2310. }
  2311. /* ONREPL this is depricated function but there is currently no better API to use */
  2312. rc = slapi_entry_add_values(e, type_ruvElement, bvals);
  2313. if (rc != 0)
  2314. {
  2315. goto done;
  2316. }
  2317. pb = slapi_pblock_new();
  2318. slapi_add_entry_internal_set_pb(
  2319. pb,
  2320. e,
  2321. NULL /* controls */,
  2322. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  2323. OP_FLAG_TOMBSTONE_ENTRY | OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP);
  2324. slapi_add_internal_pb(pb);
  2325. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
  2326. if (return_value == LDAP_SUCCESS)
  2327. r->repl_ruv_dirty = PR_FALSE;
  2328. done:
  2329. if (return_value != LDAP_SUCCESS)
  2330. {
  2331. slapi_entry_free (e);
  2332. }
  2333. if (bvals)
  2334. ber_bvecfree(bvals);
  2335. if (pb)
  2336. slapi_pblock_destroy(pb);
  2337. slapi_ch_free((void **) &root_entry_str);
  2338. return return_value;
  2339. }
  2340. static void
  2341. assign_csn_callback(const CSN *csn, void *data)
  2342. {
  2343. Replica *r = (Replica *)data;
  2344. Object *ruv_obj;
  2345. RUV *ruv;
  2346. PR_ASSERT(NULL != csn);
  2347. PR_ASSERT(NULL != r);
  2348. ruv_obj = replica_get_ruv (r);
  2349. PR_ASSERT (ruv_obj);
  2350. ruv = (RUV*)object_get_data (ruv_obj);
  2351. PR_ASSERT (ruv);
  2352. PR_Lock(r->repl_lock);
  2353. r->repl_csn_assigned = PR_TRUE;
  2354. if (NULL != r->min_csn_pl)
  2355. {
  2356. if (csnplInsert(r->min_csn_pl, csn) != 0)
  2357. {
  2358. char ebuf[BUFSIZ];
  2359. char csn_str[CSN_STRSIZE]; /* For logging only */
  2360. /* Ack, we can't keep track of min csn. Punt. */
  2361. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "assign_csn_callback: "
  2362. "failed to insert csn %s for replica %s\n",
  2363. csn_as_string(csn, PR_FALSE, csn_str),
  2364. escape_string(slapi_sdn_get_dn(r->repl_root), ebuf));
  2365. csnplFree(&r->min_csn_pl);
  2366. }
  2367. }
  2368. ruv_add_csn_inprogress (ruv, csn);
  2369. PR_Unlock(r->repl_lock);
  2370. object_release (ruv_obj);
  2371. }
  2372. static void
  2373. abort_csn_callback(const CSN *csn, void *data)
  2374. {
  2375. Replica *r = (Replica *)data;
  2376. Object *ruv_obj;
  2377. RUV *ruv;
  2378. int rc;
  2379. PR_ASSERT(NULL != csn);
  2380. PR_ASSERT(NULL != data);
  2381. ruv_obj = replica_get_ruv (r);
  2382. PR_ASSERT (ruv_obj);
  2383. ruv = (RUV*)object_get_data (ruv_obj);
  2384. PR_ASSERT (ruv);
  2385. PR_Lock(r->repl_lock);
  2386. if (NULL != r->min_csn_pl)
  2387. {
  2388. rc = csnplRemove(r->min_csn_pl, csn);
  2389. PR_ASSERT(rc == 0);
  2390. }
  2391. ruv_cancel_csn_inprogress (ruv, csn);
  2392. PR_Unlock(r->repl_lock);
  2393. object_release (ruv_obj);
  2394. }
  2395. static CSN *
  2396. _replica_get_purge_csn_nolock(const Replica *r)
  2397. {
  2398. CSN *purge_csn = NULL;
  2399. CSN **csns = NULL;
  2400. RUV *ruv;
  2401. int i;
  2402. if (r->repl_purge_delay > 0)
  2403. {
  2404. /* get a sorted list of all maxcsns in ruv in ascend order */
  2405. object_acquire(r->repl_ruv);
  2406. ruv = object_get_data(r->repl_ruv);
  2407. csns = cl5BuildCSNList (ruv, NULL);
  2408. object_release(r->repl_ruv);
  2409. if (csns == NULL)
  2410. return NULL;
  2411. /* locate the most recent maxcsn in the csn list */
  2412. for (i = 0; csns[i]; i++);
  2413. purge_csn = csn_dup (csns[i-1]);
  2414. /* set purge_csn to the most recent maxcsn - purge_delay */
  2415. if ((csn_get_time(purge_csn) - r->repl_purge_delay) > 0)
  2416. csn_set_time(purge_csn, csn_get_time(purge_csn) - r->repl_purge_delay);
  2417. }
  2418. if (csns)
  2419. cl5DestroyCSNList (&csns);
  2420. return purge_csn;
  2421. }
  2422. static void
  2423. replica_get_referrals_nolock (const Replica *r, char ***referrals)
  2424. {
  2425. if(referrals!=NULL)
  2426. {
  2427. int hint;
  2428. int i= 0;
  2429. Slapi_Value *v= NULL;
  2430. if (NULL == r->repl_referral)
  2431. {
  2432. *referrals = NULL;
  2433. }
  2434. else
  2435. {
  2436. /* richm: +1 for trailing NULL */
  2437. *referrals= (char**)slapi_ch_calloc(sizeof(char*),1+slapi_valueset_count(r->repl_referral));
  2438. hint= slapi_valueset_first_value( r->repl_referral, &v );
  2439. while(v!=NULL)
  2440. {
  2441. const char *s= slapi_value_get_string(v);
  2442. if(s!=NULL && s[0]!='\0')
  2443. {
  2444. (*referrals)[i]= slapi_ch_strdup(s);
  2445. i++;
  2446. }
  2447. hint= slapi_valueset_next_value( r->repl_referral, hint, &v);
  2448. }
  2449. (*referrals)[i] = NULL;
  2450. }
  2451. }
  2452. }
  2453. static void
  2454. replica_clear_legacy_referrals(const Slapi_DN *repl_root_sdn,
  2455. char **referrals, const char *state)
  2456. {
  2457. repl_set_mtn_state_and_referrals(repl_root_sdn, state, NULL, NULL, referrals);
  2458. }
  2459. static void
  2460. replica_remove_legacy_attr (const Slapi_DN *repl_root_sdn, const char *attr)
  2461. {
  2462. Slapi_PBlock *pb;
  2463. Slapi_Mods smods;
  2464. LDAPControl **ctrls;
  2465. int rc;
  2466. pb = slapi_pblock_new ();
  2467. slapi_mods_init(&smods, 1);
  2468. slapi_mods_add(&smods, LDAP_MOD_DELETE, attr, 0, NULL);
  2469. ctrls = (LDAPControl**)slapi_ch_malloc (2 * sizeof (LDAPControl*));
  2470. ctrls[0] = create_managedsait_control ();
  2471. ctrls[1] = NULL;
  2472. /* remove copiedFrom/copyingFrom first */
  2473. slapi_modify_internal_set_pb (pb, slapi_sdn_get_dn (repl_root_sdn),
  2474. slapi_mods_get_ldapmods_passout (&smods), ctrls,
  2475. NULL /*uniqueid */,
  2476. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION) ,
  2477. 0 /* operation_flags */);
  2478. slapi_modify_internal_pb (pb);
  2479. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2480. if (rc != LDAP_SUCCESS)
  2481. {
  2482. char ebuf[BUFSIZ];
  2483. /* this is not a fatal error because the attribute may not be there */
  2484. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_remove_legacy_attr: "
  2485. "failed to remove legacy attribute %s for replica %s; LDAP error - %d\n",
  2486. attr, escape_string(slapi_sdn_get_dn(repl_root_sdn),ebuf), rc);
  2487. }
  2488. slapi_mods_done (&smods);
  2489. slapi_pblock_destroy (pb);
  2490. }
  2491. static int
  2492. replica_log_ruv_elements_nolock (const Replica *r)
  2493. {
  2494. int rc = 0;
  2495. slapi_operation_parameters op_params;
  2496. RUV *ruv;
  2497. char *repl_gen;
  2498. CSN *csn = NULL;
  2499. ruv = (RUV*) object_get_data (r->repl_ruv);
  2500. PR_ASSERT (ruv);
  2501. if ((ruv_get_min_csn(ruv, &csn) == RUV_SUCCESS) && csn)
  2502. {
  2503. /* we log it as a delete operation to have the least number of fields
  2504. to set. the entry can be identified by a special target uniqueid and
  2505. special target dn */
  2506. memset (&op_params, 0, sizeof (op_params));
  2507. op_params.operation_type = SLAPI_OPERATION_DELETE;
  2508. op_params.target_address.dn = START_ITERATION_ENTRY_DN;
  2509. op_params.target_address.uniqueid = START_ITERATION_ENTRY_UNIQUEID;
  2510. op_params.csn = csn;
  2511. repl_gen = ruv_get_replica_generation (ruv);
  2512. rc = cl5WriteOperation(r->repl_name, repl_gen, &op_params, PR_FALSE);
  2513. if (rc == CL5_SUCCESS)
  2514. rc = 0;
  2515. else
  2516. rc = -1;
  2517. slapi_ch_free ((void**)&repl_gen);
  2518. csn_free (&csn);
  2519. }
  2520. return rc;
  2521. }
  2522. void
  2523. replica_set_purge_delay(Replica *r, PRUint32 purge_delay)
  2524. {
  2525. PR_ASSERT(r);
  2526. PR_Lock(r->repl_lock);
  2527. r->repl_purge_delay = purge_delay;
  2528. PR_Unlock(r->repl_lock);
  2529. }
  2530. void
  2531. replica_set_tombstone_reap_interval (Replica *r, long interval)
  2532. {
  2533. char *repl_name;
  2534. PR_Lock(r->repl_lock);
  2535. /*
  2536. * Leave the event there to purge the existing tombstones
  2537. * if we are about to turn off tombstone creation
  2538. */
  2539. if (interval > 0 && r->repl_eqcxt_tr && r->tombstone_reap_interval != interval)
  2540. {
  2541. int found;
  2542. repl_name = slapi_eq_get_arg (r->repl_eqcxt_tr);
  2543. slapi_ch_free ((void**)&repl_name);
  2544. found = slapi_eq_cancel (r->repl_eqcxt_tr);
  2545. slapi_log_error (SLAPI_LOG_REPL, NULL,
  2546. "tombstone_reap event (interval=%d) was %s\n",
  2547. r->tombstone_reap_interval, (found ? "cancelled" : "not found"));
  2548. r->repl_eqcxt_tr = NULL;
  2549. }
  2550. r->tombstone_reap_interval = interval;
  2551. if ( interval > 0 && r->repl_eqcxt_tr == NULL )
  2552. {
  2553. repl_name = slapi_ch_strdup (r->repl_name);
  2554. r->repl_eqcxt_tr = slapi_eq_repeat (eq_cb_reap_tombstones, repl_name,
  2555. current_time() + r->tombstone_reap_interval,
  2556. 1000 * r->tombstone_reap_interval);
  2557. slapi_log_error (SLAPI_LOG_REPL, NULL,
  2558. "tombstone_reap event (interval=%d) was %s\n",
  2559. r->tombstone_reap_interval, (r->repl_eqcxt_tr ? "scheduled" : "not scheduled successfully"));
  2560. }
  2561. PR_Unlock(r->repl_lock);
  2562. }
  2563. /* Update the tombstone entry to reflect the content of the ruv */
  2564. static void
  2565. replica_replace_ruv_tombstone(Replica *r)
  2566. {
  2567. Slapi_PBlock *pb = NULL;
  2568. char *dn;
  2569. int rc;
  2570. Slapi_Mod smod;
  2571. Slapi_Mod smod_last_modified;
  2572. LDAPMod *mods [3];
  2573. PR_ASSERT(NULL != r && NULL != r->repl_root);
  2574. PR_Lock(r->repl_lock);
  2575. PR_ASSERT (r->repl_ruv);
  2576. ruv_to_smod ((RUV*)object_get_data(r->repl_ruv), &smod);
  2577. ruv_last_modified_to_smod ((RUV*)object_get_data(r->repl_ruv), &smod_last_modified);
  2578. dn = _replica_get_config_dn (r->repl_root);
  2579. mods[0] = (LDAPMod*)slapi_mod_get_ldapmod_byref(&smod);
  2580. mods[1] = (LDAPMod*)slapi_mod_get_ldapmod_byref(&smod_last_modified);
  2581. PR_Unlock (r->repl_lock);
  2582. mods [2] = NULL;
  2583. pb = slapi_pblock_new();
  2584. slapi_modify_internal_set_pb(
  2585. pb,
  2586. (char*)slapi_sdn_get_dn (r->repl_root), /* only used to select be */
  2587. mods,
  2588. NULL, /* controls */
  2589. RUV_STORAGE_ENTRY_UNIQUEID,
  2590. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  2591. OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP);
  2592. slapi_modify_internal_pb (pb);
  2593. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2594. if (rc != LDAP_SUCCESS)
  2595. {
  2596. if ((rc != LDAP_NO_SUCH_OBJECT) || !replica_is_state_flag_set(r, REPLICA_IN_USE))
  2597. {
  2598. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_replace_ruv_tombstone: "
  2599. "failed to update replication update vector for replica %s: LDAP "
  2600. "error - %d\n", (char*)slapi_sdn_get_dn (r->repl_root), rc);
  2601. }
  2602. }
  2603. slapi_ch_free ((void**)&dn);
  2604. slapi_pblock_destroy (pb);
  2605. slapi_mod_done (&smod);
  2606. slapi_mod_done (&smod_last_modified);
  2607. }
  2608. void
  2609. replica_update_ruv_consumer(Replica *r, RUV *supplier_ruv)
  2610. {
  2611. ReplicaId supplier_id = 0;
  2612. char *supplier_purl = NULL;
  2613. if ( ruv_get_first_id_and_purl(supplier_ruv, &supplier_id, &supplier_purl) == RUV_SUCCESS )
  2614. {
  2615. RUV *local_ruv = NULL;
  2616. PR_Lock(r->repl_lock);
  2617. local_ruv = (RUV*)object_get_data (r->repl_ruv);
  2618. PR_ASSERT (local_ruv);
  2619. if ( ruv_local_contains_supplier(local_ruv, supplier_id) == 0 )
  2620. {
  2621. if ( r->repl_type == REPLICA_TYPE_UPDATABLE )
  2622. {
  2623. /* Add the new ruv right after the consumer own purl */
  2624. ruv_add_index_replica(local_ruv, supplier_id, supplier_purl, 2);
  2625. }
  2626. else
  2627. {
  2628. /* This is a consumer only, add it first */
  2629. ruv_add_index_replica(local_ruv, supplier_id, supplier_purl, 1);
  2630. }
  2631. }
  2632. else
  2633. {
  2634. /* Replace it */
  2635. ruv_replace_replica_purl(local_ruv, supplier_id, supplier_purl);
  2636. }
  2637. PR_Unlock(r->repl_lock);
  2638. /* Update also the directory entry */
  2639. replica_replace_ruv_tombstone(r);
  2640. }
  2641. }
  2642. void
  2643. replica_set_ruv_dirty(Replica *r)
  2644. {
  2645. PR_ASSERT(r);
  2646. PR_Lock(r->repl_lock);
  2647. r->repl_ruv_dirty = PR_TRUE;
  2648. PR_Unlock(r->repl_lock);
  2649. }
  2650. PRBool
  2651. replica_is_state_flag_set(Replica *r, PRInt32 flag)
  2652. {
  2653. PR_ASSERT(r);
  2654. if (r)
  2655. return (r->repl_state_flags & flag);
  2656. else
  2657. return PR_FALSE;
  2658. }
  2659. void
  2660. replica_set_state_flag (Replica *r, PRUint32 flag, PRBool clear)
  2661. {
  2662. if (r == NULL)
  2663. return;
  2664. PR_Lock(r->repl_lock);
  2665. if (clear)
  2666. {
  2667. r->repl_state_flags &= ~flag;
  2668. }
  2669. else
  2670. {
  2671. r->repl_state_flags |= flag;
  2672. }
  2673. PR_Unlock(r->repl_lock);
  2674. }
  2675. /**
  2676. * Use this to tell the tombstone reap process to stop. This will
  2677. * typically be used when we (consumer) get a request to do a
  2678. * total update.
  2679. */
  2680. void
  2681. replica_set_tombstone_reap_stop(Replica *r, PRBool val)
  2682. {
  2683. if (r == NULL)
  2684. return;
  2685. PR_Lock(r->repl_lock);
  2686. r->tombstone_reap_stop = val;
  2687. PR_Unlock(r->repl_lock);
  2688. }
  2689. /* replica just came back online, probably after data was reloaded */
  2690. void
  2691. replica_enable_replication (Replica *r)
  2692. {
  2693. int rc;
  2694. PR_ASSERT(r);
  2695. /* prevent creation of new agreements until the replica is enabled */
  2696. PR_Lock(r->agmt_lock);
  2697. /* retrieve new ruv */
  2698. rc = replica_reload_ruv (r);
  2699. if (rc) {
  2700. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_enable_replication: "
  2701. "reloading ruv failed\n");
  2702. /* What to do ? */
  2703. }
  2704. /* Replica came back online, Check if the total update was terminated.
  2705. If flag is still set, it was not terminated, therefore the data is
  2706. very likely to be incorrect, and we should not restart Replication threads...
  2707. */
  2708. if (!replica_is_state_flag_set(r, REPLICA_TOTAL_IN_PROGRESS)){
  2709. /* restart outbound replication */
  2710. start_agreements_for_replica (r, PR_TRUE);
  2711. /* enable ruv state update */
  2712. replica_set_enabled (r, PR_TRUE);
  2713. }
  2714. /* mark the replica as being available for updates */
  2715. replica_relinquish_exclusive_access(r, 0, 0);
  2716. replica_set_state_flag(r, REPLICA_AGREEMENTS_DISABLED, PR_TRUE /* clear */);
  2717. PR_Unlock(r->agmt_lock);
  2718. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_enable_replication: "
  2719. "replica %s is relinquished\n",
  2720. slapi_sdn_get_ndn (replica_get_root (r)));
  2721. }
  2722. /* replica is about to be taken offline */
  2723. void
  2724. replica_disable_replication (Replica *r, Object *r_obj)
  2725. {
  2726. char *current_purl = NULL;
  2727. char *p_locking_purl = NULL;
  2728. char *locking_purl = NULL;
  2729. int junkrc;
  2730. ReplicaId junkrid;
  2731. PRBool isInc = PR_FALSE; /* get exclusive access, but not for inc update */
  2732. RUV *repl_ruv = NULL;
  2733. /* prevent creation of new agreements until the replica is disabled */
  2734. PR_Lock(r->agmt_lock);
  2735. /* stop ruv update */
  2736. replica_set_enabled (r, PR_FALSE);
  2737. /* disable outbound replication */
  2738. start_agreements_for_replica (r, PR_FALSE);
  2739. /* close the corresponding changelog file */
  2740. /* close_changelog_for_replica (r_obj); */
  2741. /* mark the replica as being unavailable for updates */
  2742. /* If an incremental update is in progress, we want to wait until it is
  2743. finished until we get exclusive access to the replica, because we have
  2744. to make sure no operations are in progress - it messes up replication
  2745. when a restore is in progress but we are still adding replicated entries
  2746. from a supplier
  2747. */
  2748. repl_ruv = (RUV*) object_get_data (r->repl_ruv);
  2749. junkrc = ruv_get_first_id_and_purl(repl_ruv, &junkrid, &p_locking_purl);
  2750. locking_purl = slapi_ch_strdup(p_locking_purl);
  2751. p_locking_purl = NULL;
  2752. repl_ruv = NULL;
  2753. while (!replica_get_exclusive_access(r, &isInc, 0, 0, "replica_disable_replication",
  2754. &current_purl)) {
  2755. if (!isInc) /* already locked, but not by inc update - break */
  2756. break;
  2757. isInc = PR_FALSE;
  2758. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2759. "replica_disable_replication: "
  2760. "replica %s is already locked by (%s) for incoming "
  2761. "incremental update; sleeping 100ms\n",
  2762. slapi_sdn_get_ndn (replica_get_root (r)),
  2763. current_purl ? current_purl : "unknown");
  2764. slapi_ch_free_string(&current_purl);
  2765. DS_Sleep(PR_MillisecondsToInterval(100));
  2766. }
  2767. slapi_ch_free_string(&current_purl);
  2768. slapi_ch_free_string(&locking_purl);
  2769. replica_set_state_flag(r, REPLICA_AGREEMENTS_DISABLED, PR_FALSE);
  2770. PR_Unlock(r->agmt_lock);
  2771. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_disable_replication: "
  2772. "replica %s is acquired\n",
  2773. slapi_sdn_get_ndn (replica_get_root (r)));
  2774. }
  2775. static void
  2776. start_agreements_for_replica (Replica *r, PRBool start)
  2777. {
  2778. Object *agmt_obj;
  2779. Repl_Agmt *agmt;
  2780. agmt_obj = agmtlist_get_first_agreement_for_replica (r);
  2781. while (agmt_obj)
  2782. {
  2783. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  2784. PR_ASSERT (agmt);
  2785. if (start)
  2786. agmt_start (agmt);
  2787. else /* stop */
  2788. agmt_stop (agmt);
  2789. agmt_obj = agmtlist_get_next_agreement_for_replica (r, agmt_obj);
  2790. }
  2791. }
  2792. int replica_start_agreement(Replica *r, Repl_Agmt *ra)
  2793. {
  2794. int ret = 0;
  2795. if (r == NULL) return -1;
  2796. PR_Lock(r->agmt_lock);
  2797. if (!replica_is_state_flag_set(r, REPLICA_AGREEMENTS_DISABLED)) {
  2798. ret = agmt_start(ra); /* Start the replication agreement */
  2799. }
  2800. PR_Unlock(r->agmt_lock);
  2801. return ret;
  2802. }
  2803. /*
  2804. * A callback function registed as op->o_csngen_handler and
  2805. * called by backend ops to generate opcsn.
  2806. */
  2807. CSN *
  2808. replica_generate_next_csn ( Slapi_PBlock *pb, const CSN *basecsn )
  2809. {
  2810. CSN *opcsn = NULL;
  2811. Object *replica_obj;
  2812. replica_obj = replica_get_replica_for_op (pb);
  2813. if (NULL != replica_obj)
  2814. {
  2815. Replica *replica = (Replica*) object_get_data (replica_obj);
  2816. if ( NULL != replica )
  2817. {
  2818. Slapi_Operation *op;
  2819. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  2820. if ( replica->repl_type != REPLICA_TYPE_READONLY ||
  2821. operation_is_flag_set (op, OP_FLAG_LEGACY_REPLICATION_DN ))
  2822. {
  2823. Object *gen_obj = replica_get_csngen (replica);
  2824. if (NULL != gen_obj)
  2825. {
  2826. CSNGen *gen = (CSNGen*) object_get_data (gen_obj);
  2827. if (NULL != gen)
  2828. {
  2829. /* The new CSN should be greater than the base CSN */
  2830. csngen_new_csn (gen, &opcsn, PR_FALSE /* don't notify */);
  2831. if (csn_compare (opcsn, basecsn) <= 0)
  2832. {
  2833. char opcsnstr[CSN_STRSIZE], basecsnstr[CSN_STRSIZE];
  2834. char opcsn2str[CSN_STRSIZE];
  2835. csn_as_string (opcsn, PR_FALSE, opcsnstr);
  2836. csn_as_string (basecsn, PR_FALSE, basecsnstr);
  2837. csn_free ( &opcsn );
  2838. csngen_adjust_time (gen, basecsn);
  2839. csngen_new_csn (gen, &opcsn, PR_FALSE /* don't notify */);
  2840. csn_as_string (opcsn, PR_FALSE, opcsn2str);
  2841. slapi_log_error (SLAPI_LOG_FATAL, NULL,
  2842. "replica_generate_next_csn: "
  2843. "opcsn=%s <= basecsn=%s, adjusted opcsn=%s\n",
  2844. opcsnstr, basecsnstr, opcsn2str);
  2845. }
  2846. /*
  2847. * Insert opcsn into the csn pending list.
  2848. * This is the notify effect in csngen_new_csn().
  2849. */
  2850. assign_csn_callback (opcsn, (void *)replica);
  2851. }
  2852. object_release (gen_obj);
  2853. }
  2854. }
  2855. }
  2856. object_release (replica_obj);
  2857. }
  2858. return opcsn;
  2859. }
  2860. /*
  2861. * A callback function registed as op->o_replica_attr_handler and
  2862. * called by backend ops to get replica attributes.
  2863. */
  2864. int
  2865. replica_get_attr ( Slapi_PBlock *pb, const char* type, void *value )
  2866. {
  2867. int rc = -1;
  2868. Object *replica_obj;
  2869. replica_obj = replica_get_replica_for_op (pb);
  2870. if (NULL != replica_obj)
  2871. {
  2872. Replica *replica = (Replica*) object_get_data (replica_obj);
  2873. if ( NULL != replica )
  2874. {
  2875. if (strcasecmp (type, type_replicaTombstonePurgeInterval) == 0)
  2876. {
  2877. *((int*)value) = replica->tombstone_reap_interval;
  2878. rc = 0;
  2879. }
  2880. else if (strcasecmp (type, type_replicaPurgeDelay) == 0)
  2881. {
  2882. *((int*)value) = replica->repl_purge_delay;
  2883. rc = 0;
  2884. }
  2885. }
  2886. object_release (replica_obj);
  2887. }
  2888. return rc;
  2889. }