repl5_replica.c 95 KB

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