repl5_replica.c 130 KB

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