repl5_replica.c 116 KB

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