repl5_replica.c 93 KB

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