repl5_replica.c 96 KB

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