repl5_replica.c 97 KB

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