repl5_replica.c 109 KB

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