repl5_replica.c 119 KB

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