| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496 |
- /** BEGIN COPYRIGHT BLOCK
- * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
- * Copyright (C) 2005 Red Hat, Inc.
- * Copyright (C) 2010 Hewlett-Packard Development Company, L.P.
- * All rights reserved.
- *
- * License: GPL (version 3 or any later version).
- * See LICENSE for details.
- * END COPYRIGHT BLOCK **/
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- /* repl5_ruv.c - implementation of replica update vector */
- /*
- * The replica update vector is stored in the nsds50ruv attribute. The LDIF
- * representation of the ruv is:
- * nsds50ruv: {replicageneration} <gen-id-for-this-replica>
- * nsds50ruv: {replica <rid>[ <url>]}[ <mincsn> <maxcsn>]
- * nsds50ruv: {replica <rid>[ <url>]}[ <mincsn> <maxcsn>]
- * ...
- * nsds50ruv: {replica <rid>[ <url>]}[ <mincsn> <maxcsn>]
- *
- * nsruvReplicaLastModified: {replica <rid>[ <url>]} lastModifiedTime
- * nsruvReplicaLastModified: {replica <rid>[ <url>]} lastModifiedTime
- * ...
- *
- * For readability, ruv_dump appends nsruvReplicaLastModified to nsds50ruv:
- * nsds50ruv: {replica <rid>[ <url>]}[ <mincsn> <maxcsn> [<lastModifiedTime>]]
- */
- #include <string.h>
- #include <ctype.h> /* For isdigit() */
- #include "csnpl.h"
- #include "repl5_ruv.h"
- #include "repl_shared.h"
- #include "repl5.h"
- #define RIDSTR_SIZE 16 /* string large enough to hold replicaid*/
- #define RUVSTR_SIZE 256 /* string large enough to hold ruv and lastmodifiedtime */
- /* Data Structures */
- /* replica */
- typedef struct ruvElement
- {
- ReplicaId rid; /* replica id for this element */
- CSN *csn; /* largest csn that we know about that originated at the master */
- CSN *min_csn; /* smallest csn that originated at the master */
- char *replica_purl; /* Partial URL for replica */
- CSNPL *csnpl; /* list of operations in progress */
- time_t last_modified; /* timestamp the modification of csn */
- } RUVElement;
- /* replica update vector */
- struct _ruv
- {
- char *replGen; /* replicated area generation: identifies replica
- in space and in time */
- DataList *elements; /* replicas */
- Slapi_RWLock *lock; /* concurrency control */
- };
- /* forward declarations */
- static int ruvInit (RUV **ruv, int initCount);
- static void ruvFreeReplica (void **data);
- static RUVElement* ruvGetReplica (const RUV *ruv, ReplicaId rid);
- static RUVElement* ruvAddReplica (RUV *ruv, const CSN *csn, const char *replica_purl);
- static RUVElement* ruvAddReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl);
- static RUVElement* ruvAddIndexReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl, int index);
- static int ruvReplicaCompare (const void *el1, const void *el2);
- static RUVElement *get_ruvelement_from_berval(const struct berval *bval);
- static char *get_replgen_from_berval(const struct berval *bval);
- static const char * const prefix_replicageneration = "{replicageneration}";
- static const char * const prefix_ruvcsn = "{replica "; /* intentionally missing '}' */
- /* API implementation */
- /*
- * Allocate a new ruv and set its replica generation to the given generation.
- */
- int
- ruv_init_new(const char *replGen, ReplicaId rid, const char *purl, RUV **ruv)
- {
- int rc;
- RUVElement *replica;
- if (ruv == NULL || replGen == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_init_new: NULL argument\n");
- return RUV_BAD_DATA;
- }
- rc = ruvInit (ruv, 0);
- if (rc != RUV_SUCCESS)
- return rc;
- (*ruv)->replGen = slapi_ch_strdup (replGen);
- /* we want to add the local writable replica to the RUV before any csns are created */
- /* this is so that it can be referred to even before it accepted any changes */
- if (purl)
- {
- replica = ruvAddReplicaNoCSN (*ruv, rid, purl);
- if (replica == NULL)
- return RUV_MEMORY_ERROR;
- }
- return RUV_SUCCESS;
- }
- int ruv_private_new(RUV **ruv, RUV *clone )
- {
-
- int rc;
- rc = ruvInit (ruv, dl_get_count(clone->elements) );
- if (rc != RUV_SUCCESS)
- return rc;
- (*ruv)->replGen = slapi_ch_strdup (clone->replGen);
- return RUV_SUCCESS;
- }
- /*
- * Create a new RUV and initialize its contents from the provided Slapi_Attr.
- * Returns:
- * RUV_BAD_DATA if the values in the attribute were malformed.
- * RUV_SUCCESS if all went well
- */
- int
- ruv_init_from_slapi_attr(Slapi_Attr *attr, RUV **ruv)
- {
- ReplicaId dummy = 0;
- return (ruv_init_from_slapi_attr_and_check_purl(attr, ruv, &dummy));
- }
- /*
- * Create a new RUV and initialize its contents from the provided Slapi_Attr.
- * Returns:
- * RUV_BAD_DATA if the values in the attribute were malformed.
- * RUV_SUCCESS if all went well
- * contain_purl is 0 if the ruv doesn't contain the local purl
- * contain_purl is != 0 if the ruv contains the local purl (contains the rid)
- */
- int
- ruv_init_from_slapi_attr_and_check_purl(Slapi_Attr *attr, RUV **ruv, ReplicaId *contain_purl)
- {
- int return_value;
- PR_ASSERT(NULL != attr && NULL != ruv);
- if (NULL == ruv || NULL == attr)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "ruv_init_from_slapi_attr: NULL argument\n");
- return_value = RUV_BAD_DATA;
- }
- else
- {
- int rc;
- int numvalues;
- slapi_attr_get_numvalues(attr, &numvalues);
- if ((rc = ruvInit(ruv, numvalues)) != RUV_SUCCESS)
- {
- return_value = rc;
- }
- else
- {
- int hint;
- Slapi_Value *value;
- const struct berval *bval;
- const char *purl = NULL;
- char *localhost = get_localhost_DNS();
- size_t localhostlen = localhost ? strlen(localhost) : 0;
- int port = config_get_port();
- return_value = RUV_SUCCESS;
- purl = multimaster_get_local_purl();
- *contain_purl = 0;
- for (hint = slapi_attr_first_value(attr, &value);
- hint != -1; hint = slapi_attr_next_value(attr, hint, &value))
- {
- bval = slapi_value_get_berval(value);
- if (NULL != bval && NULL != bval->bv_val)
- {
- if (strncmp(bval->bv_val, prefix_replicageneration, strlen(prefix_replicageneration)) == 0) {
- if (NULL == (*ruv)->replGen)
- {
- (*ruv)->replGen = get_replgen_from_berval(bval);
- } else {
- /* Twice replicageneration is wrong, just log and ignore */
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "ruv_init_from_slapi_attr: %s is present more than once\n",
- prefix_replicageneration);
- }
- }
- else
- {
- RUVElement *ruve = get_ruvelement_from_berval(bval);
- if (NULL != ruve)
- {
- char *ptr;
- /* Is the local purl already in the ruv ? */
- if ( (*contain_purl==0) && ruve->replica_purl && purl && (strncmp(ruve->replica_purl, purl, strlen(purl))==0) )
- {
- *contain_purl = ruve->rid;
- }
- /* ticket 47362 - nsslapd-port: 0 causes replication to break */
- else if ((*contain_purl==0) && ruve->replica_purl && (port == 0) && localhost &&
- (ptr = strstr(ruve->replica_purl, localhost)) && (ptr != ruve->replica_purl) &&
- (*(ptr - 1) == '/') && (*(ptr+localhostlen) == ':'))
- {
- /* same hostname, but port number may have been temporarily set to 0
- * just allow it with whatever port number is already in the replica_purl
- * do not reset the port number, do not tell the configure_ruv code that there
- * is anything wrong
- */
- *contain_purl = ruve->rid;
- }
- dl_add ((*ruv)->elements, ruve);
- }
- }
- }
- }
- slapi_ch_free_string(&localhost);
- }
- }
- return return_value;
- }
-
- /*
- * Same as ruv_init_from_slapi_attr, but takes an array of pointers to bervals.
- * I wish this wasn't a cut-n-paste of the above function, but I don't see a
- * clean way to define one API in terms of the other.
- */
- int
- ruv_init_from_bervals(struct berval **vals, RUV **ruv)
- {
- int return_value;
- PR_ASSERT(NULL != vals && NULL != ruv);
- if (NULL == ruv || NULL == vals)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "ruv_init_from_slapi_value: NULL argument\n");
- return_value = RUV_BAD_DATA;
- }
- else
- {
- int i, rc;
- i = 0;
- while (vals[i] != NULL)
- {
- i++;
- }
- if ((rc = ruvInit (ruv, i)) != RUV_SUCCESS)
- {
- return_value = rc;
- }
- else
- {
- return_value = RUV_SUCCESS;
- for (i = 0; NULL != vals[i]; i++)
- {
- if (NULL != vals[i]->bv_val)
- {
- if (strncmp(vals[i]->bv_val, prefix_replicageneration, strlen(prefix_replicageneration)) == 0) {
- if (NULL == (*ruv)->replGen)
- {
- (*ruv)->replGen = get_replgen_from_berval(vals[i]);
- } else {
- /* Twice replicageneration is wrong, just log and ignore */
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "ruv_init_from_slapi_value: %s is present more than once\n",
- prefix_replicageneration);
- }
- }
- else
- {
- RUVElement *ruve = get_ruvelement_from_berval(vals[i]);
- if (NULL != ruve)
- {
- dl_add ((*ruv)->elements, ruve);
- }
- }
- }
- }
- }
- }
- return return_value;
- }
- RUV*
- ruv_dup (const RUV *ruv)
- {
- int rc;
- RUVElement *replica, *dupReplica;
- int cookie;
- RUV *dupRUV = NULL;
- if (ruv == NULL)
- return NULL;
- slapi_rwlock_rdlock (ruv->lock);
- rc = ruvInit (&dupRUV, dl_get_count (ruv->elements));
- if (rc != RUV_SUCCESS || dupRUV == NULL)
- goto done;
- dupRUV->replGen = slapi_ch_strdup (ruv->replGen);
- for (replica = dl_get_first (ruv->elements, &cookie); replica;
- replica = dl_get_next (ruv->elements, &cookie))
- {
- dupReplica = (RUVElement *)slapi_ch_calloc (1, sizeof (*dupReplica));
- dupReplica->rid = replica->rid;
- if (replica->csn)
- dupReplica->csn = csn_dup (replica->csn);
- if (replica->min_csn)
- dupReplica->min_csn = csn_dup (replica->min_csn);
- if (replica->replica_purl)
- dupReplica->replica_purl = slapi_ch_strdup (replica->replica_purl);
- dupReplica->last_modified = replica->last_modified;
- /* ONREPL - we don't make copy of the pernding list. For now
- we don't need it. */
- dl_add (dupRUV->elements, dupReplica);
- }
- done:
- slapi_rwlock_unlock (ruv->lock);
-
- return dupRUV;
- }
- void
- ruv_destroy (RUV **ruv)
- {
- if (ruv != NULL && *ruv != NULL)
- {
- if ((*ruv)->elements)
- {
- dl_cleanup ((*ruv)->elements, ruvFreeReplica);
- dl_free (&(*ruv)->elements);
- }
- /* slapi_ch_free accepts NULL pointer */
- slapi_ch_free ((void **)&((*ruv)->replGen));
- if ((*ruv)->lock)
- {
- slapi_destroy_rwlock ((*ruv)->lock);
- }
-
- slapi_ch_free ((void**)ruv);
- }
- }
- /*
- * [610948]
- * copy elements in srcruv to destruv
- * destruv is the live wrapper, which could be referred by other threads.
- * srcruv is cleaned up after copied.
- */
- void
- ruv_copy_and_destroy (RUV **srcruv, RUV **destruv)
- {
- DataList *elemp = NULL;
- char *replgp = NULL;
- if (NULL == srcruv || NULL == *srcruv || NULL == destruv)
- {
- return;
- }
- if (NULL == *destruv)
- {
- *destruv = *srcruv;
- *srcruv = NULL;
- }
- else
- {
- slapi_rwlock_wrlock((*destruv)->lock);
- elemp = (*destruv)->elements;
- (*destruv)->elements = (*srcruv)->elements;
- if (elemp)
- {
- dl_cleanup (elemp, ruvFreeReplica);
- dl_free (&elemp);
- }
- /* slapi_ch_free accepts NULL pointer */
- replgp = (*destruv)->replGen;
- (*destruv)->replGen = (*srcruv)->replGen;
- slapi_ch_free ((void **)&replgp);
- if ((*srcruv)->lock)
- {
- slapi_destroy_rwlock ((*srcruv)->lock);
- }
- slapi_ch_free ((void**)srcruv);
- slapi_rwlock_unlock((*destruv)->lock);
- }
- PR_ASSERT (*destruv != NULL && *srcruv == NULL);
- }
- int
- ruv_delete_replica (RUV *ruv, ReplicaId rid)
- {
- int return_value;
- if (ruv == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_delete_replica: NULL argument\n");
- return_value = RUV_BAD_DATA;
- }
- else
- {
- /* check for duplicates */
- slapi_rwlock_wrlock (ruv->lock);
- dl_delete (ruv->elements, (const void*)&rid, ruvReplicaCompare, ruvFreeReplica);
- slapi_rwlock_unlock (ruv->lock);
- return_value = RUV_SUCCESS;
- }
- return return_value;
- }
- int
- ruv_add_replica (RUV *ruv, ReplicaId rid, const char *replica_purl)
- {
- RUVElement* replica;
- PR_ASSERT (ruv && replica_purl);
- slapi_rwlock_wrlock (ruv->lock);
- replica = ruvGetReplica (ruv, rid);
- if (replica == NULL){
- replica = ruvAddReplicaNoCSN (ruv, rid, replica_purl);
- } else {
- if(strcasecmp(replica->replica_purl, replica_purl )){ /* purls are different - replace it */
- ruv_replace_replica_purl_nolock(ruv, rid, replica_purl, RUV_DONT_LOCK);
- }
- }
- slapi_rwlock_unlock (ruv->lock);
- if (replica)
- return RUV_SUCCESS;
- else
- return RUV_MEMORY_ERROR;
- }
- int
- ruv_replace_replica_purl (RUV *ruv, ReplicaId rid, const char *replica_purl)
- {
- return ruv_replace_replica_purl_nolock(ruv, rid, replica_purl, RUV_LOCK);
- }
- int
- ruv_replace_replica_purl_nolock(RUV *ruv, ReplicaId rid, const char *replica_purl, int lock)
- {
- RUVElement* replica;
- int rc = RUV_NOTFOUND;
- PR_ASSERT (ruv && replica_purl);
- if(lock)
- slapi_rwlock_wrlock (ruv->lock);
- replica = ruvGetReplica (ruv, rid);
- if (replica != NULL)
- {
- if (replica->replica_purl == NULL || strcmp(replica->replica_purl, replica_purl)) { /* purl updated */
- /* Replace replica_purl in RUV since supplier has been updated. */
- slapi_ch_free_string(&replica->replica_purl);
- replica->replica_purl = slapi_ch_strdup(replica_purl);
- /* Also, reset csn and min_csn. */
- replica->csn = replica->min_csn = NULL;
- }
- rc = RUV_SUCCESS;
- }
- if(lock)
- slapi_rwlock_unlock (ruv->lock);
- return rc;
- }
- int
- ruv_add_index_replica (RUV *ruv, ReplicaId rid, const char *replica_purl, int index)
- {
- RUVElement* replica;
- PR_ASSERT (ruv && replica_purl);
- slapi_rwlock_wrlock (ruv->lock);
- replica = ruvGetReplica (ruv, rid);
- if (replica == NULL)
- {
- replica = ruvAddIndexReplicaNoCSN (ruv, rid, replica_purl, index);
- }
- slapi_rwlock_unlock (ruv->lock);
- if (replica)
- return RUV_SUCCESS;
- else
- return RUV_MEMORY_ERROR;
- }
- PRBool
- ruv_contains_replica (const RUV *ruv, ReplicaId rid)
- {
- RUVElement *replica;
- if (ruv == NULL)
- return PR_FALSE;
- slapi_rwlock_rdlock (ruv->lock);
- replica = ruvGetReplica (ruv, rid);
- slapi_rwlock_unlock (ruv->lock);
- return replica != NULL;
- }
- #define GET_LARGEST_CSN 231
- #define GET_SMALLEST_CSN 232
- static int
- get_csn_internal(const RUV *ruv, ReplicaId rid, CSN **csn, int whichone)
- {
- RUVElement *replica;
- int return_value = RUV_SUCCESS;
- if (ruv == NULL || csn == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_get_largest_csn_for_replica: NULL argument\n");
- return_value = RUV_BAD_DATA;
- }
- else
- {
- *csn = NULL;
- /* prevent element from being destroyed while we get its data */
- slapi_rwlock_rdlock (ruv->lock);
- replica = ruvGetReplica (ruv, rid);
- /* replica without min csn is treated as a non-existent replica */
- if (replica == NULL || replica->min_csn == NULL)
- {
- return_value = RUV_NOTFOUND;
- }
- else
- {
- switch (whichone)
- {
- case GET_LARGEST_CSN:
- *csn = replica->csn ? csn_dup (replica->csn) : NULL;
- break;
- case GET_SMALLEST_CSN:
- *csn = replica->min_csn ? csn_dup (replica->min_csn) : NULL;
- break;
- default:
- *csn = NULL;
- }
- }
- slapi_rwlock_unlock (ruv->lock);
- }
- return return_value;
- }
- int
- ruv_get_largest_csn_for_replica(const RUV *ruv, ReplicaId rid, CSN **csn)
- {
- return get_csn_internal(ruv, rid, csn, GET_LARGEST_CSN);
- }
- int
- ruv_get_smallest_csn_for_replica(const RUV *ruv, ReplicaId rid, CSN **csn)
- {
- return get_csn_internal(ruv, rid, csn, GET_SMALLEST_CSN);
- }
- const char *
- ruv_get_purl_for_replica(const RUV *ruv, ReplicaId rid)
- {
- RUVElement *replica;
- const char *return_value = NULL;
- slapi_rwlock_rdlock (ruv->lock);
- replica = ruvGetReplica (ruv, rid);
- if (replica != NULL)
- {
- return_value = replica->replica_purl;
- }
- slapi_rwlock_unlock (ruv->lock);
- return return_value;
- }
- static int
- set_min_csn_nolock(RUV *ruv, const CSN *min_csn, const char *replica_purl)
- {
- int return_value;
- ReplicaId rid = csn_get_replicaid (min_csn);
- RUVElement *replica = ruvGetReplica (ruv, rid);
- if (NULL == replica)
- {
- replica = ruvAddReplica (ruv, min_csn, replica_purl);
- if (replica)
- return_value = RUV_SUCCESS;
- else
- return_value = RUV_MEMORY_ERROR;
- }
- else
- {
- if (replica->min_csn == NULL || csn_compare (min_csn, replica->min_csn) < 0)
- {
- csn_free(&replica->min_csn);
- replica->min_csn = csn_dup(min_csn);
- }
- return_value = RUV_SUCCESS;
- }
- return return_value;
- }
- static int
- set_max_csn_nolock_ext(RUV *ruv, const CSN *max_csn, const char *replica_purl, PRBool must_be_greater)
- {
- int return_value = RUV_SUCCESS;
- ReplicaId rid = csn_get_replicaid (max_csn);
- RUVElement *replica = ruvGetReplica (ruv, rid);
- if (NULL == replica) {
- replica = ruvAddReplica (ruv, max_csn, replica_purl);
- if (replica)
- return_value = RUV_SUCCESS;
- else
- return_value = RUV_MEMORY_ERROR;
- } else {
- if (replica_purl && replica->replica_purl == NULL)
- replica->replica_purl = slapi_ch_strdup (replica_purl);
- if (!must_be_greater || (csn_compare(replica->csn, max_csn) < 0)) {
- csn_free(&replica->csn);
- replica->csn = csn_dup(max_csn);
- replica->last_modified = current_time();
- } else {
- char csn1[CSN_STRSIZE+1];
- char csn2[CSN_STRSIZE+1];
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "set_max_csn_nolock_ext: new CSN [%s] for replica ID [%d] "
- "is less than the existing max CSN [%s] - ignoring\n",
- csn_as_string(max_csn, PR_FALSE, csn1), rid,
- csn_as_string(replica->csn, PR_FALSE, csn2));
- return_value = RUV_COVERS_CSN;
- }
- }
- return return_value;
- }
- int
- ruv_set_min_csn(RUV *ruv, const CSN *min_csn, const char *replica_purl)
- {
- int return_value;
- slapi_rwlock_wrlock (ruv->lock);
- return_value = set_min_csn_nolock(ruv, min_csn, replica_purl);
- slapi_rwlock_unlock (ruv->lock);
- return return_value;
- }
- int
- ruv_set_max_csn(RUV *ruv, const CSN *max_csn, const char *replica_purl)
- {
- return ruv_set_max_csn_ext(ruv, max_csn, replica_purl, PR_FALSE);
- }
- int
- ruv_set_max_csn_ext(RUV *ruv, const CSN *max_csn, const char *replica_purl, PRBool must_be_greater)
- {
- int return_value;
- slapi_rwlock_wrlock (ruv->lock);
- return_value = set_max_csn_nolock_ext(ruv, max_csn, replica_purl, must_be_greater);
- slapi_rwlock_unlock (ruv->lock);
- return return_value;
- }
- int
- ruv_set_csns(RUV *ruv, const CSN *csn, const char *replica_purl)
- {
- RUVElement *replica;
- ReplicaId rid;
- int return_value;
-
- if (ruv == NULL || csn == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_set_csns: NULL argument\n");
- return_value = RUV_BAD_DATA;
- }
- else
- {
- rid = csn_get_replicaid (csn);
- /* prevent element from being destroyed while we get its data */
- slapi_rwlock_wrlock (ruv->lock);
- replica = ruvGetReplica (ruv, rid);
- if (replica == NULL) /* add new replica */
- {
- replica = ruvAddReplica (ruv, csn, replica_purl);
- if (replica)
- return_value = RUV_SUCCESS;
- else
- return_value = RUV_MEMORY_ERROR;
- }
- else
- {
- if (csn_compare (csn, replica->csn) > 0)
- {
- if (replica->csn != NULL)
- {
- csn_init_by_csn ( replica->csn, csn );
- }
- else
- {
- replica->csn = csn_dup(csn);
- }
- replica->last_modified = current_time();
- if (replica_purl && (NULL == replica->replica_purl ||
- strcmp(replica->replica_purl, replica_purl) != 0))
- {
- /* slapi_ch_free accepts NULL pointer */
- slapi_ch_free((void **)&replica->replica_purl);
- replica->replica_purl = slapi_ch_strdup(replica_purl);
- }
- }
- /* XXXggood only need to worry about this if real min csn not committed to changelog yet */
- if (csn_compare (csn, replica->min_csn) < 0)
- {
- csn_free(&replica->min_csn);
- replica->min_csn = csn_dup(csn);
- }
- return_value = RUV_SUCCESS;
- }
- slapi_rwlock_unlock (ruv->lock);
- }
- return return_value;
- }
- /* This function, for each replica keeps the smallest CSN its seen so far.
- Used for initial setup of changelog purge vector */
- int
- ruv_set_csns_keep_smallest(RUV *ruv, const CSN *csn)
- {
- RUVElement *replica;
- ReplicaId rid;
- int return_value;
-
- if (ruv == NULL || csn == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "ruv_set_csns_keep_smallest: NULL argument\n");
- return_value = RUV_BAD_DATA;
- }
- else
- {
- rid = csn_get_replicaid (csn);
- /* prevent element from being destroyed while we get its data */
- slapi_rwlock_wrlock (ruv->lock);
- replica = ruvGetReplica (ruv, rid);
- if (replica == NULL) /* add new replica */
- {
- replica = ruvAddReplica (ruv, csn, NULL);
- if (replica)
- return_value = RUV_SUCCESS;
- else
- return_value = RUV_MEMORY_ERROR;
- }
- else
- {
- if (csn_compare (csn, replica->csn) < 0)
- {
- csn_free(&replica->csn);
- replica->csn = csn_dup(csn);
- replica->last_modified = current_time();
- }
-
- return_value = RUV_SUCCESS;
- }
- slapi_rwlock_unlock (ruv->lock);
- }
- return return_value;
- }
- void
- ruv_set_replica_generation(RUV *ruv, const char *csnstr)
- {
- if (NULL != csnstr && NULL != ruv)
- {
- slapi_rwlock_wrlock (ruv->lock);
- if (NULL != ruv->replGen)
- {
- slapi_ch_free((void **)&ruv->replGen);
- }
- ruv->replGen = slapi_ch_strdup(csnstr);
-
- slapi_rwlock_unlock (ruv->lock);
- }
- }
- char *
- ruv_get_replica_generation(const RUV *ruv)
- {
- char *return_str = NULL;
- if (!ruv) {
- return return_str;
- }
- slapi_rwlock_rdlock (ruv->lock);
- if (ruv != NULL && ruv->replGen != NULL)
- {
- return_str = slapi_ch_strdup(ruv->replGen);
- }
- slapi_rwlock_unlock (ruv->lock);
- return return_str;
- }
- static PRBool
- ruv_covers_csn_internal(const RUV *ruv, const CSN *csn, PRBool strict)
- {
- RUVElement *replica;
- ReplicaId rid;
- PRBool return_value;
- if (ruv == NULL || csn == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn_internal - NULL argument\n");
- return_value = PR_FALSE;
- }
- else
- {
- rid = csn_get_replicaid(csn);
- replica = ruvGetReplica (ruv, rid);
- if (replica == NULL)
- {
- /*
- * We don't know anything about this replica change in the cl, mark it to be zapped.
- * This could of been a previously cleaned ruv, but the server was restarted before
- * the change could be trimmed.
- *
- * Only the change log trimming calls this function with "strict" set. So we'll return success
- * if strict is set.
- */
- if(strict){
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn_internal - Replica for id %d not found.\n", rid);
- return_value = PR_TRUE;
- } else {
- slapi_log_error(SLAPI_LOG_ERR, repl_plugin_name, "ruv_covers_csn_internal - Replica for id %d not found.\n", rid);
- return_value = PR_FALSE;
- }
- }
- else
- {
- if (strict)
- {
- return_value = (csn_compare (csn, replica->csn) < 0);
- }
- else
- {
- return_value = (csn_compare (csn, replica->csn) <= 0);
- }
- }
- }
- return return_value;
- }
- PRBool
- ruv_covers_csn(const RUV *ruv, const CSN *csn)
- {
- PRBool rc;
- slapi_rwlock_rdlock (ruv->lock);
- rc = ruv_covers_csn_internal(ruv, csn, PR_FALSE);
- slapi_rwlock_unlock (ruv->lock);
- return rc;
- }
- PRBool
- ruv_covers_csn_strict(const RUV *ruv, const CSN *csn)
- {
- PRBool rc;
- slapi_rwlock_rdlock (ruv->lock);
- rc = ruv_covers_csn_internal(ruv, csn, PR_TRUE);
- slapi_rwlock_unlock (ruv->lock);
- return rc;
- }
- /*
- * Used by the cleanallruv task
- *
- * We want to return TRUE if replica is NULL,
- * and we want to use "csn_compare() <="
- */
- PRBool
- ruv_covers_csn_cleanallruv(const RUV *ruv, const CSN *csn)
- {
- RUVElement *replica;
- ReplicaId rid;
- PRBool return_value;
- if (ruv == NULL || csn == NULL){
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn_cleanallruv - NULL argument\n");
- return_value = PR_FALSE;
- } else {
- rid = csn_get_replicaid(csn);
- replica = ruvGetReplica (ruv, rid);
- if (replica == NULL){
- /* already cleaned */
- return_value = PR_TRUE;
- } else {
- return_value = (csn_compare (csn, replica->csn) <= 0);
- }
- }
- return return_value;
- }
- /*
- * The function gets min{maxcsns of all ruv elements} if get_the_max=0,
- * or max{maxcsns of all ruv elements} if get_the_max != 0.
- */
- static int
- ruv_get_min_or_max_csn(const RUV *ruv, CSN **csn, int get_the_max, ReplicaId rid, int ignore_cleaned_rid)
- {
- int return_value = RUV_SUCCESS;
- if (ruv == NULL || csn == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_get_min_or_max_csn - NULL argument\n");
- return_value = RUV_BAD_DATA;
- }
- else
- {
- CSN *found = NULL;
- RUVElement *replica;
- int cookie;
- slapi_rwlock_rdlock (ruv->lock);
- for (replica = dl_get_first (ruv->elements, &cookie); replica;
- replica = dl_get_next (ruv->elements, &cookie))
- {
- /*
- * Skip replica whose maxcsn is NULL otherwise
- * the code will return different min_csn if
- * the sequence of the replicas is altered.
- *
- * don't use READ_ONLY replicas for computing the value of
- * "found", as they seem to have NULL csn and min_csn
- */
- if (replica->csn == NULL || replica->rid == READ_ONLY_REPLICA_ID)
- {
- continue;
- }
- if(ignore_cleaned_rid && is_cleaned_rid(replica->rid)){
- continue;
- }
- if(rid){ /* we are only interested in this rid's maxcsn */
- if(replica->rid == rid){
- found = replica->csn;
- break;
- }
- } else {
- if (found == NULL ||
- (!get_the_max && csn_compare(found, replica->csn)>0) ||
- ( get_the_max && csn_compare(found, replica->csn)<0))
- {
- found = replica->csn;
- }
- }
- }
- if (found == NULL)
- {
- *csn = NULL;
- }
- else
- {
- *csn = csn_dup (found);
- }
- slapi_rwlock_unlock (ruv->lock);
- }
- return return_value;
- }
- int
- ruv_get_rid_max_csn(const RUV *ruv, CSN **csn, ReplicaId rid)
- {
- return ruv_get_rid_max_csn_ext(ruv, csn, rid, 0);
- }
- int
- ruv_get_rid_max_csn_ext(const RUV *ruv, CSN **csn, ReplicaId rid, int ignore_cleaned_rid)
- {
- return ruv_get_min_or_max_csn(ruv, csn, 1 /* get the max */, rid, ignore_cleaned_rid);
- }
- int
- ruv_get_max_csn(const RUV *ruv, CSN **csn)
- {
- return ruv_get_max_csn_ext(ruv, csn, 0);
- }
- int
- ruv_get_max_csn_ext(const RUV *ruv, CSN **csn, int ignore_cleaned_rid)
- {
- return ruv_get_min_or_max_csn(ruv, csn, 1 /* get the max */, 0 /* rid */, ignore_cleaned_rid);
- }
- int
- ruv_get_min_csn(const RUV *ruv, CSN **csn)
- {
- return ruv_get_min_csn_ext(ruv, csn, 0);
- }
- int
- ruv_get_min_csn_ext(const RUV *ruv, CSN **csn, int ignore_cleaned_rid)
- {
- return ruv_get_min_or_max_csn(ruv, csn, 0 /* get the min */, 0 /* rid */, ignore_cleaned_rid);
- }
- int
- ruv_enumerate_elements (const RUV *ruv, FNEnumRUV fn, void *arg)
- {
- int cookie;
- RUVElement *elem;
- int rc = 0;
- ruv_enum_data enum_data = {0};
- if (ruv == NULL || fn == NULL)
- {
- /* ONREPL - log error */
- return -1;
- }
- slapi_rwlock_rdlock (ruv->lock);
- for (elem = (RUVElement*)dl_get_first (ruv->elements, &cookie); elem;
- elem = (RUVElement*)dl_get_next (ruv->elements, &cookie))
- {
- /* we only return elements that contains both minimal and maximal CSNs */
- if (elem->csn && elem->min_csn)
- {
- enum_data.csn = elem->csn;
- enum_data.min_csn = elem->min_csn;
- rc = fn (&enum_data, arg);
- if (rc != 0)
- break;
- }
- }
-
- slapi_rwlock_unlock (ruv->lock);
- return rc;
- }
- void
- ruv_element_to_string(RUVElement *ruvelem, struct berval *bv, char *buf, size_t bufsize)
- {
- char csnStr1[CSN_STRSIZE];
- char csnStr2[CSN_STRSIZE];
- const char *fmtstr = "%s%d%s%s}%s%s%s%s";
- if (buf && bufsize) {
- PR_snprintf(buf, bufsize, fmtstr,
- prefix_ruvcsn, ruvelem->rid,
- ruvelem->replica_purl == NULL ? "" : " ",
- ruvelem->replica_purl == NULL ? "" : ruvelem->replica_purl,
- ruvelem->min_csn == NULL ? "" : " ",
- ruvelem->min_csn == NULL ? "" : csn_as_string (ruvelem->min_csn, PR_FALSE, csnStr1),
- ruvelem->csn == NULL ? "" : " ",
- ruvelem->csn == NULL ? "" : csn_as_string (ruvelem->csn, PR_FALSE, csnStr2));
- } else {
- bv->bv_val = slapi_ch_smprintf(fmtstr,
- prefix_ruvcsn, ruvelem->rid,
- ruvelem->replica_purl == NULL ? "" : " ",
- ruvelem->replica_purl == NULL ? "" : ruvelem->replica_purl,
- ruvelem->min_csn == NULL ? "" : " ",
- ruvelem->min_csn == NULL ? "" : csn_as_string (ruvelem->min_csn, PR_FALSE, csnStr1),
- ruvelem->csn == NULL ? "" : " ",
- ruvelem->csn == NULL ? "" : csn_as_string (ruvelem->csn, PR_FALSE, csnStr2));
- bv->bv_len = strlen(bv->bv_val);
- }
- }
- /*
- * Convert a replica update vector to a NULL-terminated array
- * of bervals. The caller is responsible for freeing the bervals.
- */
- int
- ruv_to_bervals(const RUV *ruv, struct berval ***bvals)
- {
- struct berval **returned_bervals = NULL;
- int return_value;
- if (ruv == NULL || bvals == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_bervals - NULL argument\n");
- return_value = RUV_BAD_DATA;
- }
- else
- {
- int count;
- int i;
- RUVElement *replica;
- int cookie;
- slapi_rwlock_rdlock (ruv->lock);
- count = dl_get_count (ruv->elements) + 2;
- returned_bervals = (struct berval **)slapi_ch_malloc(sizeof(struct berval *) * count);
- returned_bervals[count - 1] = NULL;
- returned_bervals[0] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
- returned_bervals[0]->bv_val = slapi_ch_smprintf("%s %s",
- prefix_replicageneration, ruv->replGen);
- returned_bervals[0]->bv_len = strlen(returned_bervals[0]->bv_val);
- for (i = 1, replica = dl_get_first (ruv->elements, &cookie); replica;
- i++, replica = dl_get_next (ruv->elements, &cookie))
- {
- returned_bervals[i] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
- ruv_element_to_string(replica, returned_bervals[i], NULL, 0);
- }
- slapi_rwlock_unlock (ruv->lock);
- return_value = RUV_SUCCESS;
- *bvals = returned_bervals;
- }
- return return_value;
- }
- void
- ruv_get_cleaned_rids(RUV *ruv, ReplicaId *rids)
- {
- RUVElement *replica;
- int cookie;
- int i = 0;
- for (replica = dl_get_first (ruv->elements, &cookie); replica;
- replica = dl_get_next (ruv->elements, &cookie))
- {
- if(is_cleaned_rid(replica->rid)){
- rids[i++] = replica->rid;
- }
- }
- }
- /*
- * This routine dump the ruv (last modified time) into a value array
- * the call must free the returned value array
- */
- Slapi_Value **
- ruv_last_modified_to_valuearray(RUV *ruv)
- {
- RUVElement *ruv_e;
- int cookie;
- Slapi_Value *value;
- Slapi_Value **values = NULL;
- char *buffer;
- /* Acquire the ruv lock */
- slapi_rwlock_rdlock(ruv->lock);
- /* Now loop for each RUVElement and store its string value into the valueset*/
- for (ruv_e = dl_get_first(ruv->elements, &cookie);
- NULL != ruv_e;
- ruv_e = dl_get_next(ruv->elements, &cookie)) {
-
- buffer = slapi_ch_smprintf("%s%d%s%s} %08lx", prefix_ruvcsn, ruv_e->rid,
- ruv_e->replica_purl == NULL ? "" : " ",
- ruv_e->replica_purl == NULL ? "" : ruv_e->replica_purl,
- ruv_e->last_modified);
- value = slapi_value_new_string_passin(buffer);
- valuearray_add_value(&values, value);
- slapi_value_free(&value);
- }
-
- slapi_rwlock_unlock(ruv->lock);
- return (values);
- }
- /*
- * This routine dump the ruv (replicageneration and ruvElements) into a value array
- * the call must free the returned value array
- */
- Slapi_Value **
- ruv_to_valuearray(RUV *ruv)
- {
- RUVElement *ruv_e;
- int cookie;
- Slapi_Value *value;
- Slapi_Value **values = NULL;
- struct berval bv;
- char *buffer;
- /* Acquire the ruv lock */
- slapi_rwlock_rdlock(ruv->lock);
-
- /* dump the replicageneration */
- buffer = slapi_ch_smprintf("%s %s", prefix_replicageneration, ruv->replGen);
- value = slapi_value_new_string_passin(buffer);
- valuearray_add_value(&values, value);
- slapi_value_free(&value);
- /* Now loop for each RUVElement and store its string value into the valueset*/
- for (ruv_e = dl_get_first(ruv->elements, &cookie);
- NULL != ruv_e;
- ruv_e = dl_get_next(ruv->elements, &cookie)) {
- ruv_element_to_string(ruv_e, &bv, NULL, 0);
- value = slapi_value_new_berval(&bv);
- slapi_ber_bvdone(&bv);
- valuearray_add_value(&values, value);
- slapi_value_free(&value);
- }
-
- slapi_rwlock_unlock(ruv->lock);
- return (values);
- }
- int
- ruv_to_smod(const RUV *ruv, Slapi_Mod *smod)
- {
- int return_value;
- if (ruv == NULL || smod == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_smod - NULL argument\n");
- return_value = RUV_BAD_DATA;
- }
- else
- {
- struct berval val;
- RUVElement *replica;
- int cookie;
- #define B_SIZ 1024
- char buf[B_SIZ];
- slapi_rwlock_rdlock (ruv->lock);
- slapi_mod_init (smod, dl_get_count (ruv->elements) + 1);
- slapi_mod_set_type (smod, type_ruvElement);
- slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
- PR_snprintf(buf, sizeof(buf), "%s %s", prefix_replicageneration, ruv->replGen);
- val.bv_val = buf;
- val.bv_len = strlen(buf);
- slapi_mod_add_value(smod, &val);
- for (replica = dl_get_first (ruv->elements, &cookie); replica;
- replica = dl_get_next (ruv->elements, &cookie))
- {
- ruv_element_to_string(replica, NULL, buf, sizeof(buf));
- val.bv_val = buf;
- val.bv_len = strlen(buf);
- slapi_mod_add_value(smod, &val);
- }
- slapi_rwlock_unlock (ruv->lock);
- return_value = RUV_SUCCESS;
- }
- return return_value;
- }
- int
- ruv_last_modified_to_smod(const RUV *ruv, Slapi_Mod *smod)
- {
- int return_value;
- if (ruv == NULL || smod == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_last_modified_to_smod - NULL argument\n");
- return_value = RUV_BAD_DATA;
- }
- else
- {
- struct berval val;
- RUVElement *replica;
- int cookie;
- char buf[B_SIZ];
- slapi_rwlock_rdlock (ruv->lock);
- slapi_mod_init (smod, dl_get_count (ruv->elements));
- slapi_mod_set_type (smod, type_ruvElementUpdatetime);
- slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
- val.bv_val = buf;
- for (replica = dl_get_first (ruv->elements, &cookie); replica;
- replica = dl_get_next (ruv->elements, &cookie))
- {
- PR_snprintf(buf, B_SIZ, "%s%d%s%s} %08lx", prefix_ruvcsn, replica->rid,
- replica->replica_purl == NULL ? "" : " ",
- replica->replica_purl == NULL ? "" : replica->replica_purl,
- replica->last_modified);
- val.bv_len = strlen(buf);
- slapi_mod_add_value(smod, &val);
- }
- slapi_rwlock_unlock (ruv->lock);
- return_value = RUV_SUCCESS;
- }
- return return_value;
- }
- /*
- * XXXggood do we need "ruv_covers_ruv_strict" ???? */
- PRBool
- ruv_covers_ruv(const RUV *covering_ruv, const RUV *covered_ruv)
- {
- PRBool return_value = PR_TRUE;
- RUVElement *replica;
- int cookie;
- /* compare replica generations first */
- if (covering_ruv->replGen == NULL || covered_ruv->replGen == NULL)
- return PR_FALSE;
-
- if (strcasecmp (covered_ruv->replGen, covering_ruv->replGen))
- return PR_FALSE;
- /* replica generation is the same, now compare element by element */
- for (replica = dl_get_first (covered_ruv->elements, &cookie);
- NULL != replica;
- replica = dl_get_next (covered_ruv->elements, &cookie))
- {
- if (replica->csn &&
- (ruv_covers_csn(covering_ruv, replica->csn) == PR_FALSE))
- {
- return_value = PR_FALSE;
- /* Don't break here - may leave something referenced? */
- }
- }
- return return_value;
- }
- /*
- * This compares two ruvs to see if they are compatible. This is
- * used, for example, when the data is reloaded, to see if the ruv
- * from the database is compatible with the ruv from the changelog.
- * If the replica generation is empty or does not match, the data
- * is not compatible.
- * If the maxcsns are not compatible, the ruvs are not compatible.
- * However, if the first ruv has replica IDs that the second RUV
- * does not have, and this is the only difference, the application
- * may allow that with a warning.
- */
- int
- ruv_compare_ruv(const RUV *ruv1, const char *ruv1name, const RUV *ruv2, const char *ruv2name, int strict, int loglevel)
- {
- int rc = 0;
- int ii = 0;
- const RUV *ruvalist[] = {ruv1, ruv2};
- const RUV *ruvblist[] = {ruv2, ruv1};
- int missinglist[2] = {0, 0};
- const char *ruvanames[] = {ruv1name, ruv2name};
- const char *ruvbnames[] = {ruv2name, ruv1name};
- const int nitems = 2;
- if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
- ruv_dump(ruv1, (char *)ruv1name, NULL);
- ruv_dump(ruv2, (char *)ruv2name, NULL);
- }
- /* compare replica generations first */
- if (ruv1->replGen == NULL || ruv2->replGen == NULL) {
- slapi_log_error(loglevel,
- repl_plugin_name,
- "ruv_compare_ruv - RUV [%s] is missing the replica generation\n",
- ruv1->replGen ? ruv2name : ruv1name);
- return RUV_COMP_NO_GENERATION;
- }
-
- if (strcasecmp (ruv1->replGen, ruv2->replGen)) {
- slapi_log_error(loglevel,
- repl_plugin_name,
- "ruv_compare_ruv - RUV [%s] replica generation [%s] does not match RUV [%s] [%s]\n",
- ruv1name, ruv1->replGen, ruv2name, ruv2->replGen);
- return RUV_COMP_GENERATION_DIFFERS;
- }
- /* replica generation is the same, now compare element by element */
- for (ii = 0; ii < nitems; ++ii) {
- int cookie;
- const RUV *ruva = ruvalist[ii];
- const RUV *ruvb = ruvblist[ii];
- int *missing = &missinglist[ii];
- const char *ruvaname = ruvanames[ii];
- const char *ruvbname = ruvbnames[ii];
- RUVElement *replicab;
- for (replicab = dl_get_first (ruvb->elements, &cookie);
- NULL != replicab;
- replicab = dl_get_next (ruvb->elements, &cookie)) {
- if (replicab->csn) {
- ReplicaId rid = csn_get_replicaid(replicab->csn);
- RUVElement *replicaa = ruvGetReplica(ruva, rid);
- char csnstra[CSN_STRSIZE];
- char csnstrb[CSN_STRSIZE];
- char ruvelem[1024];
- ruv_element_to_string(replicab, NULL, ruvelem, sizeof(ruvelem));
- csn_as_string(replicab->csn, PR_FALSE, csnstrb);
- if (replicaa == NULL) {
- (*missing)++;
- slapi_log_error(loglevel, repl_plugin_name,
- "ruv_compare_ruv - RUV [%s] does not contain element [%s] "
- "which is present in RUV [%s]\n",
- ruvaname, ruvelem, ruvbname);
- } else if (strict && (csn_compare (replicab->csn, replicaa->csn) >= 0)) {
- csn_as_string(replicaa->csn, PR_FALSE, csnstra);
- slapi_log_error(loglevel, repl_plugin_name,
- "ruv_compare_ruv - The max CSN [%s] from RUV [%s] is larger "
- "than or equal to the max CSN [%s] from RUV [%s] for element [%s]\n",
- csnstrb, ruvbname, csnstra, ruvaname, ruvelem);
- rc = RUV_COMP_CSN_DIFFERS;
- } else if (csn_compare (replicab->csn, replicaa->csn) > 0) {
- csn_as_string(replicaa->csn, PR_FALSE, csnstra);
- slapi_log_error(loglevel, repl_plugin_name,
- "ruv_compare_ruv - The max CSN [%s] from RUV [%s] is larger "
- "than the max CSN [%s] from RUV [%s] for element [%s]\n",
- csnstrb, ruvbname, csnstra, ruvaname, ruvelem);
- rc = RUV_COMP_CSN_DIFFERS;
- } else {
- csn_as_string(replicaa->csn, PR_FALSE, csnstra);
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "ruv_compare_ruv - The max CSN [%s] from RUV [%s] is less than "
- "or equal to the max CSN [%s] from RUV [%s] for element [%s]\n",
- csnstrb, ruvbname, csnstra, ruvaname, ruvelem);
- }
- } else {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "ruv_compare_ruv - RUV [%s] has an empty CSN\n",
- ruvbname);
- }
- }
- }
- if (!rc) {
- if (missinglist[0]) {
- rc = RUV_COMP_RUV1_MISSING;
- } else if (missinglist[1]) {
- rc = RUV_COMP_RUV2_MISSING;
- }
- }
- return rc;
- }
- PRInt32
- ruv_replica_count (const RUV *ruv)
- {
- if (ruv == NULL)
- return 0;
- else
- {
- int count;
- slapi_rwlock_rdlock (ruv->lock);
- count = dl_get_count (ruv->elements);
- slapi_rwlock_unlock (ruv->lock);
-
- return count;
- }
- }
- /*
- * Extract all the referral URL's from the RUV (but self URL),
- * returning them in an array of strings, that
- * the caller must free.
- */
- char **
- ruv_get_referrals(const RUV *ruv)
- {
- char **r= NULL;
- int n;
- const char *mypurl = multimaster_get_local_purl();
-
- slapi_rwlock_rdlock (ruv->lock);
- n = ruv_replica_count(ruv);
- if(n>0)
- {
- RUVElement *replica;
- int cookie;
- int i= 0;
- r= (char**)slapi_ch_calloc(sizeof(char*),n+1);
- for (replica = dl_get_first (ruv->elements, &cookie); replica;
- replica = dl_get_next (ruv->elements, &cookie))
- {
- /* Add URL into referrals if doesn't match self URL */
- if((replica->replica_purl!=NULL) &&
- (slapi_utf8casecmp((unsigned char *)replica->replica_purl,
- (unsigned char *)mypurl) != 0))
- {
- r[i]= slapi_ch_strdup(replica->replica_purl);
- i++;
- }
- }
- }
- slapi_rwlock_unlock (ruv->lock);
- return r; /* Caller must free this */
- }
- void
- ruv_dump(const RUV *ruv, char *ruv_name, PRFileDesc *prFile)
- {
- RUVElement *replica;
- int cookie;
- char csnstr1[CSN_STRSIZE];
- char csnstr2[CSN_STRSIZE];
- char buff[RUVSTR_SIZE];
- int len = sizeof (buff);
- PR_ASSERT(NULL != ruv);
- if (!slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
- return;
- }
- slapi_rwlock_rdlock (ruv->lock);
- PR_snprintf (buff, len, "%s: {replicageneration} %s\n",
- ruv_name ? ruv_name : type_ruvElement,
- ruv->replGen == NULL ? "" : ruv->replGen);
- if (prFile)
- {
- slapi_write_buffer (prFile, buff, strlen(buff));
- }
- else
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "%s", buff);
- }
- for (replica = dl_get_first (ruv->elements, &cookie); replica;
- replica = dl_get_next (ruv->elements, &cookie))
- {
- /* prefix_ruvcsn = "{replica " */
- PR_snprintf (buff, len, "%s: %s%d%s%s}%s%s%s%s\n",
- ruv_name ? ruv_name : type_ruvElement,
- prefix_ruvcsn, replica->rid,
- replica->replica_purl == NULL ? "" : " ",
- replica->replica_purl == NULL ? "" : replica->replica_purl,
- replica->min_csn == NULL ? "" : " ",
- csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
- replica->csn == NULL ? "" : " ",
- csn_as_string(replica->csn, PR_FALSE, csnstr2));
- if (strlen (csnstr1) > 0) {
- PR_snprintf (buff + strlen(buff) - 1, len - strlen(buff), " %08lx\n",
- replica->last_modified);
- }
- if (prFile)
- {
- slapi_write_buffer (prFile, buff, strlen(buff));
- }
- else
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "%s", buff);
- }
- }
- slapi_rwlock_unlock (ruv->lock);
- }
- /* this function notifies the ruv that there are operations in progress so that
- they can be added to the pending list for the appropriate client. */
- int ruv_add_csn_inprogress (RUV *ruv, const CSN *csn)
- {
- RUVElement* replica;
- char csn_str[CSN_STRSIZE];
- int rc = RUV_SUCCESS;
- int rid = csn_get_replicaid (csn);
- PR_ASSERT (ruv && csn);
- /* locate ruvElement */
- slapi_rwlock_wrlock (ruv->lock);
- if(is_cleaned_rid(rid)){
- /* return success because we want to consume the update, but not perform it */
- rc = RUV_COVERS_CSN;
- goto done;
- }
- replica = ruvGetReplica (ruv, rid);
- if (replica == NULL)
- {
- replica = ruvAddReplicaNoCSN (ruv, rid, NULL/*purl*/);
- if (replica == NULL)
- {
- if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress - Failed to add replica"
- " that created csn %s\n", csn_as_string (csn, PR_FALSE, csn_str));
- }
- rc = RUV_MEMORY_ERROR;
- goto done;
- }
- }
- /* check first that this csn is not already covered by this RUV */
- if (ruv_covers_csn_internal(ruv, csn, PR_FALSE))
- {
- if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress - "
- "The csn %s has already be seen - ignoring\n",
- csn_as_string (csn, PR_FALSE, csn_str));
- }
- rc = RUV_COVERS_CSN;
- goto done;
- }
- rc = csnplInsert (replica->csnpl, csn);
- if (rc == 1) /* we already seen this csn */
- {
- if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress - "
- "The csn %s has already be seen - ignoring\n",
- csn_as_string (csn, PR_FALSE, csn_str));
- }
- rc = RUV_COVERS_CSN;
- }
- else if(rc != 0)
- {
- if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress - Failed to insert csn %s"
- " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
- }
- rc = RUV_UNKNOWN_ERROR;
- }
- else
- {
- if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress - Successfully inserted csn %s"
- " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
- }
- rc = RUV_SUCCESS;
- }
-
- done:
- slapi_rwlock_unlock (ruv->lock);
- return rc;
- }
- int ruv_cancel_csn_inprogress (RUV *ruv, const CSN *csn)
- {
- RUVElement* replica;
- int rc = RUV_SUCCESS;
- PR_ASSERT (ruv && csn);
- /* locate ruvElement */
- slapi_rwlock_wrlock (ruv->lock);
- replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
- if (replica == NULL)
- {
- /* ONREPL - log error */
- rc = RUV_NOTFOUND;
- goto done;
- }
- rc = csnplRemove (replica->csnpl, csn);
- if (rc != 0)
- rc = RUV_NOTFOUND;
- else
- rc = RUV_SUCCESS;
-
- done:
- slapi_rwlock_unlock (ruv->lock);
- return rc;
- }
- int ruv_update_ruv (RUV *ruv, const CSN *csn, const char *replica_purl, PRBool isLocal)
- {
- int rc=RUV_SUCCESS;
- char csn_str[CSN_STRSIZE];
- CSN *max_csn;
- CSN *first_csn = NULL;
- RUVElement *replica;
-
- PR_ASSERT (ruv && csn);
- slapi_rwlock_wrlock (ruv->lock);
- replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
- if (replica == NULL)
- {
- /* we should have a ruv element at this point because it would have
- been added by ruv_add_inprogress function */
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv - "
- "Can't locate RUV element for replica %d\n", csn_get_replicaid (csn));
- goto done;
- }
- if (csnplCommit(replica->csnpl, csn) != 0)
- {
- slapi_log_error(SLAPI_LOG_ERR, repl_plugin_name, "ruv_update_ruv - Cannot commit csn %s\n",
- csn_as_string(csn, PR_FALSE, csn_str));
- rc = RUV_CSNPL_ERROR;
- goto done;
- }
- else
- {
- if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv - "
- "Successfully committed csn %s\n", csn_as_string(csn, PR_FALSE, csn_str));
- }
- }
- if ((max_csn = csnplRollUp(replica->csnpl, &first_csn)) != NULL)
- {
- #ifdef DEBUG
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv - Rolled up to csn %s\n",
- csn_as_string(max_csn, PR_FALSE, csn_str)); /* XXXggood remove debugging */
- #endif
- /* replica object sets min csn for local replica */
- if (!isLocal && replica->min_csn == NULL) {
- /* bug 559223 - it seems that, under huge stress, a server might pass
- * through this code when more than 1 change has already been sent and commited into
- * the pending lists... Therefore, as we are trying to set the min_csn ever
- * generated by this replica, we need to set the first_csn as the min csn in the
- * ruv */
- set_min_csn_nolock(ruv, first_csn, replica_purl);
- }
- /* only update the max_csn in the RUV if it is greater than the existing one */
- rc = set_max_csn_nolock_ext(ruv, max_csn, replica_purl, PR_TRUE /* must be greater */);
- /* It is possible that first_csn points to max_csn.
- We need to free it once */
- if (max_csn != first_csn) {
- csn_free(&first_csn);
- }
- csn_free(&max_csn);
- }
- done:
- slapi_rwlock_unlock (ruv->lock);
- return rc;
- }
- /* Helper functions */
- static int
- ruvInit (RUV **ruv, int initCount)
- {
- PR_ASSERT (ruv);
- if (ruv == NULL) {
- return RUV_NSPR_ERROR;
- }
- /* allocate new RUV */
- *ruv = (RUV *)slapi_ch_calloc (1, sizeof (RUV));
- /* allocate elements */
- (*ruv)->elements = dl_new (); /* never returns NULL */
- dl_init ((*ruv)->elements, initCount);
- /* create lock */
- (*ruv)->lock = slapi_new_rwlock();
- if ((*ruv)->lock == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "ruvInit - Failed to create lock\n");
- dl_free(&(*ruv)->elements);
- slapi_ch_free((void**)ruv);
- return RUV_NSPR_ERROR;
- }
- return RUV_SUCCESS;
- }
- static void
- ruvFreeReplica (void **data)
- {
- RUVElement *element = *(RUVElement**)data;
- if (NULL != element)
- {
- if (NULL != element->csn)
- {
- csn_free (&element->csn);
- }
- if (NULL != element->min_csn)
- {
- csn_free (&element->min_csn);
- }
- /* slapi_ch_free accepts NULL pointer */
- slapi_ch_free((void **)&element->replica_purl);
- if (element->csnpl)
- {
- csnplFree (&(element->csnpl));
- }
- slapi_ch_free ((void **)&element);
- }
- }
- static RUVElement*
- ruvAddReplica (RUV *ruv, const CSN *csn, const char *replica_purl)
- {
- RUVElement *replica;
- PR_ASSERT (NULL != ruv);
- PR_ASSERT (NULL != csn);
-
- replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
- if (replica == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "ruvAddReplica: memory allocation failed\n");
- return NULL;
- }
-
- replica->rid = csn_get_replicaid (csn);
- /* PR_ASSERT(replica->rid != READ_ONLY_REPLICA_ID); */
-
- replica->csn = csn_dup (csn);
- replica->last_modified = current_time();
- replica->min_csn = csn_dup (csn);
- replica->replica_purl = slapi_ch_strdup(replica_purl);
- replica->csnpl = csnplNew ();
-
- dl_add (ruv->elements, replica);
- return replica;
- }
- static RUVElement*
- ruvAddReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl)
- {
- RUVElement *replica;
- PR_ASSERT (NULL != ruv);
- /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
-
- replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
- if (replica == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "ruvAddReplicaNoCSN - Memory allocation failed\n");
- return NULL;
- }
- replica->rid = rid;
- replica->replica_purl = slapi_ch_strdup(replica_purl);
- replica->csnpl = csnplNew ();
-
- dl_add (ruv->elements, replica);
- return replica;
- }
- static RUVElement*
- ruvAddIndexReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl, int index)
- {
- RUVElement *replica;
- PR_ASSERT (NULL != ruv);
- /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
-
- replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
- if (replica == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "ruvAddIndexReplicaNoCSN - Memory allocation failed\n");
- return NULL;
- }
- replica->rid = rid;
- replica->replica_purl = slapi_ch_strdup(replica_purl);
- replica->csnpl = csnplNew ();
-
- dl_add_index (ruv->elements, replica, index);
- return replica;
- }
- static RUVElement *
- ruvGetReplica (const RUV *ruv, ReplicaId rid)
- {
- PR_ASSERT (ruv /* && rid >= 0 -- rid can't be negative */);
- return (RUVElement *)dl_get (ruv->elements, (const void*)&rid, ruvReplicaCompare);
- }
- static int
- ruvReplicaCompare (const void *el1, const void *el2)
- {
- RUVElement *replica = (RUVElement*)el1;
- ReplicaId *rid1 = (ReplicaId*) el2;
- if (replica == NULL || rid1 == NULL)
- return -1;
- if (*rid1 == replica->rid)
- return 0;
-
- if (*rid1 < replica->rid)
- return -1;
- else
- return 1;
- }
- /*
- * Given a berval that points to a string of the form:
- * "{dbgen} generation-id", return a copy of the
- * "generation-id" part in a null-terminated string.
- * Returns NULL if the berval is malformed.
- */
- static char *
- get_replgen_from_berval(const struct berval *bval)
- {
- char *ret_string = NULL;
- if (NULL != bval && NULL != bval->bv_val &&
- (bval->bv_len > strlen(prefix_replicageneration)) &&
- strncasecmp(bval->bv_val, prefix_replicageneration,
- strlen(prefix_replicageneration)) == 0)
- {
- unsigned int index = strlen(prefix_replicageneration);
- /* Skip any whitespace */
- while (index++ < bval->bv_len && bval->bv_val[index] == ' ');
- if (index < bval->bv_len)
- {
- unsigned int ret_len = bval->bv_len - index;
- ret_string = slapi_ch_malloc(ret_len + 1);
- memcpy(ret_string, &bval->bv_val[index], ret_len);
- ret_string[ret_len] = '\0';
- }
- }
- return ret_string;
- }
- /*
- * Given a berval that points to a string of the form:
- * "{replica ldap[s]//host:port} <min_csn> <csn>", parse out the
- * partial URL and the CSNs into an RUVElement, and return
- * a pointer to the copy. Returns NULL if the berval is
- * malformed.
- */
- static RUVElement *
- get_ruvelement_from_berval(const struct berval *bval)
- {
- RUVElement *ret_ruve = NULL;
- char *purl = NULL;
- ReplicaId rid = 0;
- char ridbuff [RIDSTR_SIZE];
- int i;
- if (NULL == bval || NULL == bval->bv_val ||
- bval->bv_len <= strlen(prefix_ruvcsn) ||
- strncasecmp(bval->bv_val, prefix_ruvcsn, strlen(prefix_ruvcsn)) != 0)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "get_ruvelement_from_berval - Invalid berval\n");
- goto loser;
- }
- {
- unsigned int urlbegin = strlen(prefix_ruvcsn);
- unsigned int urlend;
- unsigned int mincsnbegin;
- /* replica id must be here */
- i = 0;
- while (isdigit (bval->bv_val[urlbegin]))
- {
- ridbuff [i] = bval->bv_val[urlbegin];
- i++;
- urlbegin ++;
- }
- if (i == 0) /* replicaid is missing */
- goto loser;
- ridbuff[i] = '\0';
- rid = atoi (ridbuff);
- if (bval->bv_val[urlbegin] == '}')
- {
- /* No purl in this value */
- purl = NULL;
- mincsnbegin = urlbegin + 1;
- }
- else
- {
- while (urlbegin++ < bval->bv_len && bval->bv_val[urlbegin] == ' ');
- urlend = urlbegin;
- while (urlend++ < bval->bv_len && bval->bv_val[urlend] != '}');
- purl = slapi_ch_malloc(urlend - urlbegin + 1);
- memcpy(purl, &bval->bv_val[urlbegin], urlend - urlbegin);
- purl[urlend - urlbegin] = '\0';
- mincsnbegin = urlend;
- }
- /* Skip any whitespace before the first (minimum) CSN */
- while (mincsnbegin++ < (bval->bv_len-1) && bval->bv_val[mincsnbegin] == ' ');
- /* Now, mincsnbegin should contain the index of the beginning of the first csn */
- if (mincsnbegin >= bval->bv_len)
- {
- /* Missing the entire content*/
- if (purl == NULL)
- goto loser;
- else /* we have just purl - no changes from the replica has been seen */
- {
- ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
- ret_ruve->rid = rid;
- ret_ruve->replica_purl = purl;
- }
- }
- else
- {
- if ((bval->bv_len - mincsnbegin != (_CSN_VALIDCSN_STRLEN * 2) + 1)
- && (bval->bv_len - mincsnbegin != (_CSN_VALIDCSN_STRLEN * 2) + 10))
- {
- /* Malformed - incorrect length for 2 CSNs + space AND
- * incorrect length for 2 CSNs + 2 spaces +
- * last_modified (see ruv_dump) */
- goto loser;
- }
- else
- {
- char mincsnstr[CSN_STRSIZE];
- char maxcsnstr[CSN_STRSIZE];
- memset(mincsnstr, '\0', CSN_STRSIZE);
- memset(maxcsnstr, '\0', CSN_STRSIZE);
- memcpy(mincsnstr, &bval->bv_val[mincsnbegin], _CSN_VALIDCSN_STRLEN);
- memcpy(maxcsnstr, &bval->bv_val[mincsnbegin + _CSN_VALIDCSN_STRLEN + 1], _CSN_VALIDCSN_STRLEN);
- ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
- ret_ruve->min_csn = csn_new_by_string(mincsnstr);
- ret_ruve->csn = csn_new_by_string(maxcsnstr);
- ret_ruve->rid = rid;
- ret_ruve->replica_purl = purl;
- if (NULL == ret_ruve->min_csn || NULL == ret_ruve->csn)
- {
- goto loser;
- }
- }
- }
- }
- /* initialize csn pending list */
- ret_ruve->csnpl = csnplNew ();
- if (ret_ruve->csnpl == NULL)
- {
- slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
- "get_ruvelement_from_berval - Failed to create csn pending list\n");
- goto loser;
- }
- return ret_ruve;
- loser:
- /* slapi_ch_free accepts NULL pointer */
- slapi_ch_free((void **)&purl);
- if (NULL != ret_ruve)
- {
- if (NULL != ret_ruve->min_csn)
- {
- csn_free(&ret_ruve->min_csn);
- }
- if (NULL != ret_ruve->csn)
- {
- csn_free(&ret_ruve->csn);
- }
- slapi_ch_free((void **)&ret_ruve);
- }
- return NULL;
- }
- int
- ruv_move_local_supplier_to_first(RUV *ruv, ReplicaId aRid)
- {
- RUVElement * elem = NULL;
- int rc = RUV_NOTFOUND;
-
- PR_ASSERT(ruv);
- slapi_rwlock_wrlock (ruv->lock);
- elem = (RUVElement *)dl_delete(ruv->elements,(const void*)&aRid, ruvReplicaCompare, 0);
- if (elem) {
- dl_add_index(ruv->elements, elem, 1);
- rc = RUV_SUCCESS;
- }
- slapi_rwlock_unlock (ruv->lock);
-
- return rc;
- }
- int
- ruv_get_first_id_and_purl(RUV *ruv, ReplicaId *rid, char **replica_purl )
- {
- RUVElement * first = NULL;
- int cookie;
- int rc;
-
- PR_ASSERT(ruv);
- slapi_rwlock_rdlock (ruv->lock);
- first = dl_get_first(ruv->elements, &cookie);
- if ( first == NULL )
- {
- rc = RUV_MEMORY_ERROR;
- }
- else
- {
- *rid = first->rid;
- *replica_purl = first->replica_purl;
- rc = RUV_SUCCESS;
- }
- slapi_rwlock_unlock (ruv->lock);
- return rc;
- }
- int ruv_local_contains_supplier(RUV *ruv, ReplicaId rid)
- {
- int cookie;
- RUVElement *elem = NULL;
- PR_ASSERT(ruv);
- slapi_rwlock_rdlock (ruv->lock);
- for (elem = dl_get_first (ruv->elements, &cookie);
- elem;
- elem = dl_get_next (ruv->elements, &cookie))
- {
- if (elem->rid == rid){
- slapi_rwlock_unlock (ruv->lock);
- return 1;
- }
- }
- slapi_rwlock_unlock (ruv->lock);
- return 0;
- }
- PRBool ruv_has_csns(const RUV *ruv)
- {
- PRBool retval = PR_TRUE;
- CSN *mincsn = NULL;
- CSN *maxcsn = NULL;
- ruv_get_min_csn(ruv, &mincsn);
- ruv_get_max_csn(ruv, &maxcsn);
- if (mincsn) {
- csn_free(&mincsn);
- csn_free(&maxcsn);
- } else if (maxcsn) {
- csn_free(&maxcsn);
- } else {
- retval = PR_FALSE; /* both min and max are false */
- }
- return retval;
- }
- PRBool ruv_has_both_csns(const RUV *ruv)
- {
- PRBool retval = PR_TRUE;
- CSN *mincsn = NULL;
- CSN *maxcsn = NULL;
- ruv_get_min_csn(ruv, &mincsn);
- ruv_get_max_csn(ruv, &maxcsn);
- if (mincsn) {
- csn_free(&mincsn);
- csn_free(&maxcsn);
- } else if (maxcsn) {
- csn_free(&maxcsn);
- retval = PR_FALSE; /* it has a maxcsn but no mincsn */
- } else {
- retval = PR_FALSE; /* both min and max are false */
- }
- return retval;
- }
- /* Check if the first ruv is newer than the second one */
- PRBool
- ruv_is_newer (Object *sruvobj, Object *cruvobj)
- {
- RUV *sruv, *cruv;
- RUVElement *sreplica, *creplica;
- int scookie, ccookie;
- int is_newer = PR_FALSE;
- if ( sruvobj == NULL ) {
- return 0;
- }
- if ( cruvobj == NULL ) {
- return 1;
- }
- sruv = (RUV *) object_get_data ( sruvobj );
- cruv = (RUV *) object_get_data ( cruvobj );
- for (sreplica = dl_get_first (sruv->elements, &scookie); sreplica;
- sreplica = dl_get_next (sruv->elements, &scookie))
- {
- /* A hub may have a dummy ruv with rid 65535 */
- if ( sreplica->csn == NULL ) continue;
- if ( cruv->elements == NULL )
- {
- slapi_log_error(SLAPI_LOG_ERR, repl_plugin_name,
- "ruv_is_newer, consumer RUV has no elements\n");
- is_newer=PR_FALSE;
- break;
- }
- for (creplica = dl_get_first (cruv->elements, &ccookie); creplica;
- creplica = dl_get_next (cruv->elements, &ccookie))
- {
- if ( sreplica->rid == creplica->rid ) {
- if ( csn_compare ( sreplica->csn, creplica->csn ) > 0 ) {
- is_newer = PR_TRUE;
- }
- break;
- }
- }
- if ( creplica == NULL || is_newer ) {
- is_newer = PR_TRUE;
- break;
- }
- }
- return is_newer;
- }
- /*
- * This routine is called after a disordely shutdown
- * The Database RUV was found late compare to the changelog RUV
- */
- void
- ruv_force_csn_update_from_ruv(RUV *src_ruv, RUV *tgt_ruv, char *msg, int logLevel) {
- RUVElement *replica = NULL;
- int cookie;
- slapi_rwlock_rdlock(src_ruv->lock);
-
- for (replica = dl_get_first(src_ruv->elements, &cookie);
- NULL != replica;
- replica = dl_get_next(src_ruv->elements, &cookie)) {
- /*
- * In case the DB RUV (tgt_ruv) is behind the CL RUV (src_ruv)
- * updates the DB RUV.
- */
- if (!ruv_covers_csn(tgt_ruv, replica->csn)) {
- char csnStr[CSN_STRSIZE];
- ruv_force_csn_update(tgt_ruv, replica->csn);
- csn_as_string(replica->csn, PR_FALSE, csnStr);
- slapi_log_error(logLevel, repl_plugin_name, "%s %s\n", msg, csnStr);
- }
- }
- slapi_rwlock_unlock(src_ruv->lock);
-
- }
- void
- ruv_force_csn_update (RUV *ruv, CSN *csn)
- {
- CSN *max = NULL;
-
- if (ruv != NULL)
- {
- ruv_get_max_csn(ruv, &max);
- if (csn_compare(max, csn))
- {
- ruv_set_max_csn(ruv, csn, NULL);
- }
- csn_free(&max);
- }
- }
- /* This routine is used to iterate the elements in a RUV and set each vector's
- + * minimal CSN to a dummy with just the rid set, e.g. 00000000000000010000 */
- void
- ruv_insert_dummy_min_csn (RUV *ruv)
- {
- RUVElement *r;
- int cookie;
- for (r = dl_get_first (ruv->elements, &cookie); r;
- r = dl_get_next (ruv->elements, &cookie)) {
- if (r->csn && !r->min_csn) {
- CSN *dummycsn = csn_new();
- csn_init(dummycsn);
- csn_set_replicaid(dummycsn, csn_get_replicaid(r->csn));
- r->min_csn = dummycsn;
- }
- }
- }
- #ifdef TESTING /* Some unit tests for code in this file */
- static void
- ruv_dump_internal(RUV *ruv)
- {
- RUVElement *replica;
- int cookie;
- char csnstr1[CSN_STRSIZE];
- char csnstr2[CSN_STRSIZE];
- PR_ASSERT(NULL != ruv);
- printf("{replicageneration} %s\n", ruv->replGen == NULL ? "NULL" : ruv->replGen);
- for (replica = dl_get_first (ruv->elements, &cookie); replica;
- replica = dl_get_next (ruv->elements, &cookie))
- {
- printf("{replica%s%s} %s %s\n",
- replica->replica_purl == NULL ? "" : " ",
- replica->replica_purl == NULL ? "" : replica->replica_purl,
- csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
- csn_as_string(replica->csn, PR_FALSE, csnstr2));
- }
- }
-
- void
- ruv_test()
- {
- const struct berval *vals[5];
- struct berval val0, val1, val2, val3;
- RUV *ruv;
- Slapi_Attr *attr;
- Slapi_Value *sv0, *sv1, *sv2, *sv3;
- int rc;
- char csnstr[CSN_STRSIZE];
- char *gen;
- CSN *newcsn;
- ReplicaId *ids;
- int nids;
- Slapi_Mod smods;
- PRBool covers;
- vals[0] = &val0;
- vals[1] = &val1;
- vals[2] = &val2;
- vals[3] = &val3;
- vals[4] = NULL;
- val0.bv_val = "{replicageneration} 0440FDC0A33F";
- val0.bv_len = strlen(val0.bv_val);
- val1.bv_val = "{replica ldap://ggood.mcom.com:389} 12345670000000FE0000 12345671000000FE0000";
- val1.bv_len = strlen(val1.bv_val);
- val2.bv_val = "{replica ldaps://an-impossibly-long-host-name-that-drags-on-forever-and-forever.mcom.com:389} 11112110000000FF0000 11112111000000FF0000";
- val2.bv_len = strlen(val2.bv_val);
- val3.bv_val = "{replica} 12345672000000FD0000 12345673000000FD0000";
- val3.bv_len = strlen(val3.bv_val);
- rc = ruv_init_from_bervals(vals, &ruv);
- ruv_dump_internal(ruv);
- attr = slapi_attr_new();
- attr = slapi_attr_init(attr, "ruvelement");
- sv0 = slapi_value_new();
- sv1 = slapi_value_new();
- sv2 = slapi_value_new();
- sv3 = slapi_value_new();
- slapi_value_init_berval(sv0, &val0);
- slapi_value_init_berval(sv1, &val1);
- slapi_value_init_berval(sv2, &val2);
- slapi_value_init_berval(sv3, &val3);
- slapi_attr_add_value(attr, sv0);
- slapi_attr_add_value(attr, sv1);
- slapi_attr_add_value(attr, sv2);
- slapi_attr_add_value(attr, sv3);
- rc = ruv_init_from_slapi_attr(attr, &ruv);
- ruv_dump_internal(ruv);
-
- rc = ruv_delete_replica(ruv, 0xFF);
- /* Should delete one replica */
- ruv_dump_internal(ruv);
- rc = ruv_delete_replica(ruv, 0xAA);
- /* No such replica - should not do anything */
- ruv_dump_internal(ruv);
- rc = ruv_get_largest_csn_for_replica(ruv, 0xFE, &newcsn);
- if (NULL != newcsn)
- {
- csn_as_string(newcsn, PR_FALSE, csnstr);
- printf("Replica 0x%X has largest csn \"%s\"\n", 0xFE, csnstr);
- }
- else
- {
- printf("BAD - can't get largest CSN for replica 0x%X\n", 0xFE);
- }
- rc = ruv_get_smallest_csn_for_replica(ruv, 0xFE, &newcsn);
- if (NULL != newcsn)
- {
- csn_as_string(newcsn, PR_FALSE, csnstr);
- printf("Replica 0x%X has smallest csn \"%s\"\n", 0xFE, csnstr);
- }
- else
- {
- printf("BAD - can't get smallest CSN for replica 0x%X\n", 0xFE);
- }
- rc = ruv_get_largest_csn_for_replica(ruv, 0xAA, &newcsn);
- printf("ruv_get_largest_csn_for_replica on non-existent replica ID returns %d\n", rc);
- rc = ruv_get_smallest_csn_for_replica(ruv, 0xAA, &newcsn);
- printf("ruv_get_smallest_csn_for_replica on non-existent replica ID returns %d\n", rc);
- newcsn = csn_new_by_string("12345674000000FE0000"); /* Old replica 0xFE */
- rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
- /* Should update replica FE's CSN */
- ruv_dump_internal(ruv);
- newcsn = csn_new_by_string("12345675000000FB0000"); /* New replica 0xFB */
- rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
- /* Should get a new replica in the list with min == max csn */
- ruv_dump_internal(ruv);
- newcsn = csn_new_by_string("12345676000000FD0000"); /* Old replica 0xFD */
- rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
- /* Should update replica 0xFD so new CSN is newer than min CSN */
- ruv_dump_internal(ruv);
- gen = ruv_get_replica_generation(ruv);
- printf("replica generation is \"%s\"\n", gen);
- newcsn = csn_new_by_string("12345673000000FE0000"); /* Old replica 0xFE */
- covers = ruv_covers_csn(ruv, newcsn); /* should say "true" */
- newcsn = csn_new_by_string("12345675000000FE0000"); /* Old replica 0xFE */
- covers = ruv_covers_csn(ruv, newcsn); /* Should say "false" */
- newcsn = csn_new_by_string("123456700000000A0000"); /* New replica 0A */
- rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
- ruv_dump_internal(ruv);
- newcsn = csn_new_by_string("123456710000000A0000"); /* New replica 0A */
- rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
- ruv_dump_internal(ruv);
- newcsn = csn_new_by_string("123456700000000B0000"); /* New replica 0B */
- rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
- ruv_dump_internal(ruv);
- newcsn = csn_new_by_string("123456710000000B0000"); /* New replica 0B */
- rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
- ruv_dump_internal(ruv);
- /* ONREPL test ruv enumeration */
- rc = ruv_to_smod(ruv, &smods);
- ruv_destroy(&ruv);
- }
- #endif /* TESTING */
|