repl5_inc_protocol.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* repl5_inc_protocol.c */
  42. /*
  43. The Prot_Incremental object implements the DS 5.0 multi-master incremental
  44. replication protocol.
  45. Stuff to do:
  46. - Need to figure out how asynchronous events end up in here. They are:
  47. - entry updated in replicated area.
  48. - backoff timeout
  49. - enter/leave.
  50. Perhaps these events should be properties of the main protocol.
  51. */
  52. #include <plstr.h>
  53. #include "repl.h"
  54. #include "repl5.h"
  55. #include "repl5_ruv.h"
  56. #include "repl5_prot_private.h"
  57. #include "cl5_api.h"
  58. extern int slapi_log_urp;
  59. /*** from proto-slap.h ***/
  60. void ava_done(struct ava *ava);
  61. typedef struct repl5_inc_private
  62. {
  63. char *ruv; /* RUV on remote replica (use diff type for this? - ggood */
  64. Backoff_Timer *backoff;
  65. Repl_Protocol *rp;
  66. PRLock *lock;
  67. PRUint32 eventbits;
  68. } repl5_inc_private;
  69. /* Structures used to communicate with the result reading thread */
  70. #ifndef UIDSTR_SIZE
  71. #define UIDSTR_SIZE 35 /* size of the string representation of the id */
  72. #endif
  73. typedef struct repl5_inc_operation
  74. {
  75. int ldap_message_id;
  76. unsigned long operation_type;
  77. char csn_str[CSN_STRSIZE];
  78. char uniqueid[UIDSTR_SIZE+1];
  79. ReplicaId replica_id;
  80. struct repl5_inc_operation *next;
  81. } repl5_inc_operation;
  82. typedef struct result_data
  83. {
  84. Private_Repl_Protocol *prp;
  85. int rc;
  86. PRLock *lock; /* Lock to protect access to this structure, the message id list and to force memory barriers */
  87. PRThread *result_tid; /* The async result thread */
  88. repl5_inc_operation *operation_list_head; /* List of IDs for outstanding operations */
  89. repl5_inc_operation *operation_list_tail; /* List of IDs for outstanding operations */
  90. int abort; /* Flag used to tell the sending thread asyncronously that it should abort (because an error came up in a result) */
  91. PRUint32 num_changes_sent;
  92. int stop_result_thread; /* Flag used to tell the result thread to exit */
  93. int last_message_id_sent;
  94. int last_message_id_received;
  95. int result; /* The UPDATE_TRANSIENT_ERROR etc */
  96. } result_data;
  97. /* Various states the incremental protocol can pass through */
  98. #define STATE_START 0 /* ONREPL - should we rename this - we don't use it just to start up? */
  99. #define STATE_WAIT_WINDOW_OPEN 1
  100. #define STATE_WAIT_CHANGES 2
  101. #define STATE_READY_TO_ACQUIRE 3
  102. #define STATE_BACKOFF_START 4 /* ONREPL - can we combine BACKOFF_START and BACKOFF states? */
  103. #define STATE_BACKOFF 5
  104. #define STATE_SENDING_UPDATES 6
  105. #define STATE_STOP_FATAL_ERROR 7
  106. #define STATE_STOP_FATAL_ERROR_PART2 8
  107. #define STATE_STOP_NORMAL_TERMINATION 9
  108. /* Events (synchronous and asynchronous; these are bits) */
  109. #define EVENT_WINDOW_OPENED 1
  110. #define EVENT_WINDOW_CLOSED 2
  111. #define EVENT_TRIGGERING_CRITERIA_MET 4 /* ONREPL - should we rename this to EVENT_CHANGE_AVAILABLE */
  112. #define EVENT_BACKOFF_EXPIRED 8
  113. #define EVENT_REPLICATE_NOW 16
  114. #define EVENT_PROTOCOL_SHUTDOWN 32
  115. #define EVENT_AGMT_CHANGED 64
  116. #define UPDATE_NO_MORE_UPDATES 201
  117. #define UPDATE_TRANSIENT_ERROR 202
  118. #define UPDATE_FATAL_ERROR 203
  119. #define UPDATE_SCHEDULE_WINDOW_CLOSED 204
  120. #define UPDATE_CONNECTION_LOST 205
  121. #define UPDATE_TIMEOUT 206
  122. #define UPDATE_YIELD 207
  123. /* Return codes from examine_update_vector */
  124. #define EXAMINE_RUV_PRISTINE_REPLICA 401
  125. #define EXAMINE_RUV_GENERATION_MISMATCH 402
  126. #define EXAMINE_RUV_REPLICA_TOO_OLD 403
  127. #define EXAMINE_RUV_OK 404
  128. #define EXAMINE_RUV_PARAM_ERROR 405
  129. #define MAX_CHANGES_PER_SESSION 10000
  130. /*
  131. * Maximum time to wait between replication sessions. If we
  132. * don't see any updates for a period equal to this interval,
  133. * we go ahead and start a replication session, just to be safe
  134. */
  135. #define MAX_WAIT_BETWEEN_SESSIONS PR_SecondsToInterval(60 * 5) /* 5 minutes */
  136. /*
  137. * tests if the protocol has been shutdown and we need to quit
  138. * event_occurred resets the bits in the bit flag, so whoever tests for shutdown
  139. * resets the flags, so the next one who tests for shutdown won't get it, so we
  140. * also look at the terminate flag
  141. */
  142. #define PROTOCOL_IS_SHUTDOWN(prp) (event_occurred(prp, EVENT_PROTOCOL_SHUTDOWN) || prp->terminate)
  143. /* mods should be LDAPMod **mods */
  144. #define MODS_ARE_EMPTY(mods) ((mods == NULL) || (mods[0] == NULL))
  145. /* Forward declarations */
  146. static PRUint32 event_occurred(Private_Repl_Protocol *prp, PRUint32 event);
  147. static void reset_events (Private_Repl_Protocol *prp);
  148. static void protocol_sleep(Private_Repl_Protocol *prp, PRIntervalTime duration);
  149. static int send_updates(Private_Repl_Protocol *prp, RUV *ruv, PRUint32 *num_changes_sent);
  150. static void repl5_inc_backoff_expired(time_t timer_fire_time, void *arg);
  151. static int examine_update_vector(Private_Repl_Protocol *prp, RUV *ruv);
  152. static PRBool ignore_error_and_keep_going(int error);
  153. static const char* state2name (int state);
  154. static const char* event2name (int event);
  155. static const char* op2string (int op);
  156. static int repl5_inc_update_from_op_result(Private_Repl_Protocol *prp, ConnResult replay_crc, int connection_error, char *csn_str, char *uniqueid, ReplicaId replica_id, int* finished, PRUint32 *num_changes_sent);
  157. /* Push a newly sent operation onto the tail of the list */
  158. static void repl5_int_push_operation(result_data *rd, repl5_inc_operation *it)
  159. {
  160. repl5_inc_operation *tail = NULL;
  161. PR_Lock(rd->lock);
  162. tail = rd->operation_list_tail;
  163. if (tail)
  164. {
  165. tail->next = it;
  166. }
  167. if (NULL == rd->operation_list_head)
  168. {
  169. rd->operation_list_head = it;
  170. }
  171. rd->operation_list_tail = it;
  172. PR_Unlock(rd->lock);
  173. }
  174. /* Pop the next operation in line to respond from the list */
  175. /* The caller is expected to free the operation item */
  176. static repl5_inc_operation *repl5_inc_pop_operation(result_data *rd)
  177. {
  178. repl5_inc_operation *head = NULL;
  179. repl5_inc_operation *ret = NULL;
  180. PR_Lock(rd->lock);
  181. head = rd->operation_list_head;
  182. if (head)
  183. {
  184. ret = head;
  185. rd->operation_list_head = head->next;
  186. if (rd->operation_list_tail == head)
  187. {
  188. rd->operation_list_tail = NULL;
  189. }
  190. }
  191. PR_Unlock(rd->lock);
  192. return ret;
  193. }
  194. static void
  195. repl5_inc_op_free(repl5_inc_operation *op)
  196. {
  197. slapi_ch_free((void**)&op);
  198. }
  199. static repl5_inc_operation *repl5_inc_operation_new()
  200. {
  201. repl5_inc_operation *ret = NULL;
  202. ret = (repl5_inc_operation *) slapi_ch_calloc(1,sizeof(repl5_inc_operation));
  203. return ret;
  204. }
  205. /* Called when in compatibility mode, to get the next result from the wire
  206. * The operation thread will not send a second operation until it has read the
  207. * previous result. */
  208. static int
  209. repl5_inc_get_next_result(result_data *rd)
  210. {
  211. ConnResult conres = 0;
  212. int message_id = 0;
  213. /* Wait on the next result */
  214. conres = conn_read_result(rd->prp->conn, &message_id);
  215. /* Return it to the caller */
  216. return conres;
  217. }
  218. #if NEEDED_FOR_DEBUGGING
  219. static void
  220. repl5_inc_log_operation_failure(int operation_code, int ldap_error, char* ldap_error_string, const char *agreement_name)
  221. {
  222. char *op_string = slapi_op_type_to_string(operation_code);
  223. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  224. "%s: Received error %d: %s for %s operation\n",
  225. agreement_name,
  226. ldap_error, ldap_error_string ? ldap_error_string : "NULL",
  227. op_string ? op_string : "NULL");
  228. }
  229. #endif
  230. /* Thread that collects results from async operations sent to the consumer */
  231. static void repl5_inc_result_threadmain(void *param)
  232. {
  233. result_data *rd = (result_data*) param;
  234. ConnResult conres = 0;
  235. Repl_Connection *conn = rd->prp->conn;
  236. int finished = 0;
  237. int message_id = 0;
  238. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain starting\n");
  239. while (!finished)
  240. {
  241. repl5_inc_operation *op = NULL;
  242. int connection_error = 0;
  243. char *csn_str = NULL;
  244. char *uniqueid = NULL;
  245. ReplicaId replica_id = 0;
  246. int operation_code = 0;
  247. char *ldap_error_string = NULL;
  248. time_t time_now = 0;
  249. time_t start_time = time( NULL );
  250. int backoff_time = 1;
  251. /* Read the next result */
  252. /* We call the get result function with a short timeout (non-blocking)
  253. * this is so we don't block here forever, and can stop this thread when
  254. * the time comes. However, we do need to implement blocking with timeout
  255. * semantics here instead.
  256. */
  257. while (!finished)
  258. {
  259. conres = conn_read_result_ex(conn, NULL, NULL, NULL, LDAP_RES_ANY, &message_id, 0);
  260. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain: read result for message_id %d\n", message_id);
  261. /* Timeout here means that we didn't block, not a real timeout */
  262. if (CONN_TIMEOUT == conres)
  263. {
  264. /* We need to a) check that the 'real' timeout hasn't expired and
  265. * b) implement a backoff sleep to avoid spinning */
  266. /* Did the connection's timeout expire ? */
  267. time_now = time( NULL );
  268. if (conn_get_timeout(conn) <= ( time_now - start_time ))
  269. {
  270. /* We timed out */
  271. conres = CONN_TIMEOUT;
  272. break;
  273. }
  274. /* Otherwise we backoff */
  275. DS_Sleep(PR_MillisecondsToInterval(backoff_time));
  276. if (backoff_time < 1000)
  277. {
  278. backoff_time <<= 1;
  279. }
  280. /* Should we stop ? */
  281. PR_Lock(rd->lock);
  282. if (rd->stop_result_thread)
  283. {
  284. finished = 1;
  285. }
  286. PR_Unlock(rd->lock);
  287. } else
  288. {
  289. /* Something other than a timeout, so we exit the loop */
  290. break;
  291. }
  292. }
  293. if (conres != CONN_TIMEOUT)
  294. {
  295. int return_value;
  296. int should_finish = 0;
  297. if (message_id)
  298. {
  299. rd->last_message_id_received = message_id;
  300. }
  301. /* Handle any error etc */
  302. /* Get the stored operation details from the queue, unless we timed out... */
  303. op = repl5_inc_pop_operation(rd);
  304. if (op)
  305. {
  306. csn_str = op->csn_str;
  307. replica_id = op->replica_id;
  308. uniqueid = op->uniqueid;
  309. }
  310. conn_get_error_ex(conn, &operation_code, &connection_error, &ldap_error_string);
  311. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain: result %d, %d, %d, %d, %s\n", operation_code,connection_error,conres,message_id,ldap_error_string);
  312. return_value = repl5_inc_update_from_op_result(rd->prp, conres, connection_error, csn_str, uniqueid, replica_id, &should_finish, &(rd->num_changes_sent));
  313. if (return_value || should_finish)
  314. {
  315. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain: got op result %d should finish %d\n", return_value, should_finish);
  316. /* If so then we need to take steps to abort the update process */
  317. PR_Lock(rd->lock);
  318. rd->result = return_value;
  319. rd->abort = 1;
  320. PR_Unlock(rd->lock);
  321. /* We also need to log the error, including details stored from when the operation was sent */
  322. /* we cannot finish yet - we still need to waitfor the pending results, then
  323. the main repl code will shut down this thread */
  324. /* we can finish if we have disconnected - in that case, there will be nothing
  325. to read */
  326. if (return_value == UPDATE_CONNECTION_LOST) {
  327. finished = 1;
  328. }
  329. } else {
  330. /* old semantics had result set outside of lock */
  331. rd->result = return_value;
  332. }
  333. }
  334. /* Should we stop ? */
  335. PR_Lock(rd->lock);
  336. if (rd->stop_result_thread)
  337. {
  338. finished = 1;
  339. }
  340. PR_Unlock(rd->lock);
  341. if (op)
  342. {
  343. repl5_inc_op_free(op);
  344. }
  345. }
  346. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain exiting\n");
  347. }
  348. static result_data *
  349. repl5_inc_rd_new(Private_Repl_Protocol *prp)
  350. {
  351. result_data *res = NULL;
  352. res = (result_data *) slapi_ch_calloc(1,sizeof(result_data));
  353. if (res) {
  354. res->prp = prp;
  355. res->lock = PR_NewLock();
  356. if (NULL == res->lock) {
  357. slapi_ch_free((void **)&res);
  358. res = NULL;
  359. }
  360. }
  361. return res;
  362. }
  363. static void
  364. repl5_inc_rd_list_destroy(repl5_inc_operation *op)
  365. {
  366. while (op) {
  367. repl5_inc_operation *next = op->next;
  368. repl5_inc_op_free(op);
  369. op = next;
  370. }
  371. }
  372. static void
  373. repl5_inc_rd_destroy(result_data **pres)
  374. {
  375. result_data *res = *pres;
  376. if (res->lock) {
  377. PR_DestroyLock(res->lock);
  378. }
  379. /* Delete the linked list if we have one */
  380. /* Begin at the head */
  381. repl5_inc_rd_list_destroy(res->operation_list_head);
  382. slapi_ch_free((void **)pres);
  383. }
  384. static int
  385. repl5_inc_create_async_result_thread(result_data *rd)
  386. {
  387. int retval = 0;
  388. PRThread *tid = NULL;
  389. /* Create a thread that reads results from the connection and stores status in the callback_data structure */
  390. tid = PR_CreateThread(PR_USER_THREAD,
  391. repl5_inc_result_threadmain, (void*)rd,
  392. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD,
  393. SLAPD_DEFAULT_THREAD_STACKSIZE);
  394. if (NULL == tid)
  395. {
  396. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  397. "repl5_tot_create_async_result_thread failed. "
  398. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  399. PR_GetError(), slapd_pr_strerror( PR_GetError() ));
  400. retval = -1;
  401. } else {
  402. rd->result_tid = tid;
  403. }
  404. return retval;
  405. }
  406. static int
  407. repl5_inc_destroy_async_result_thread(result_data *rd)
  408. {
  409. int retval = 0;
  410. PRThread *tid = rd->result_tid;
  411. if (tid) {
  412. PR_Lock(rd->lock);
  413. rd->stop_result_thread = 1;
  414. PR_Unlock(rd->lock);
  415. (void)PR_JoinThread(tid);
  416. }
  417. return retval;
  418. }
  419. static void
  420. repl5_inc_waitfor_async_results(result_data *rd)
  421. {
  422. int done = 0;
  423. int loops = 0;
  424. /* Keep pulling results off the LDAP connection until we catch up to the last message id stored in the rd */
  425. while (!done)
  426. {
  427. /* Lock the structure to force memory barrier */
  428. PR_Lock(rd->lock);
  429. /* Are we caught up ? */
  430. slapi_log_error(SLAPI_LOG_REPL, NULL,
  431. "repl5_inc_waitfor_async_results: %d %d\n",
  432. rd->last_message_id_received, rd->last_message_id_sent);
  433. if (rd->last_message_id_received >= rd->last_message_id_sent)
  434. {
  435. /* If so then we're done */
  436. done = 1;
  437. }
  438. if (rd->abort && (rd->result == UPDATE_CONNECTION_LOST))
  439. {
  440. done = 1; /* no connection == no more results */
  441. }
  442. PR_Unlock(rd->lock);
  443. /* If not then sleep a bit */
  444. DS_Sleep(PR_SecondsToInterval(1));
  445. loops++;
  446. /* If we sleep forever then we can conclude that something bad happened, and bail... */
  447. /* Arbitrary 30 second delay : basically we should only expect to wait as long as it takes to process a few operations, which should be on the order of a second at most */
  448. if (!done && (loops > 300))
  449. {
  450. /* Log a warning */
  451. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  452. "repl5_inc_waitfor_async_results timed out waiting for responses: %d %d\n",
  453. rd->last_message_id_received, rd->last_message_id_sent);
  454. done = 1;
  455. }
  456. }
  457. }
  458. /*
  459. * It's specifically ok to delete a protocol instance that
  460. * is currently running. The instance will be shut down, and
  461. * then resources will be freed. Since a graceful shutdown is
  462. * attempted, this function may take some time to complete.
  463. */
  464. static void
  465. repl5_inc_delete(Private_Repl_Protocol **prpp)
  466. {
  467. /* First, stop the protocol if it isn't already stopped */
  468. if (!(*prpp)->stopped) {
  469. (*prpp)->stopped = 1;
  470. (*prpp)->stop(*prpp);
  471. }
  472. /* Then, delete all resources used by the protocol */
  473. if ((*prpp)->lock) {
  474. PR_DestroyLock((*prpp)->lock);
  475. (*prpp)->lock = NULL;
  476. }
  477. if ((*prpp)->cvar) {
  478. PR_DestroyCondVar((*prpp)->cvar);
  479. (*prpp)->cvar = NULL;
  480. }
  481. slapi_ch_free((void **)&(*prpp)->private);
  482. slapi_ch_free((void **)prpp);
  483. }
  484. /* helper function */
  485. void
  486. set_pause_and_busy_time(long *pausetime, long *busywaittime)
  487. {
  488. /* If neither are set, set busy time to its default */
  489. if (!*pausetime && !*busywaittime)
  490. {
  491. *busywaittime = PROTOCOL_BUSY_BACKOFF_MINIMUM;
  492. }
  493. /* pause time must be at least 1 more than the busy backoff time */
  494. if (*pausetime && !*busywaittime)
  495. {
  496. /*
  497. * user specified a pause time but no busy wait time - must
  498. * set busy wait time to 1 less than pause time - if pause
  499. * time is 1, we must set it to 2
  500. */
  501. if (*pausetime < 2)
  502. {
  503. *pausetime = 2;
  504. }
  505. *busywaittime = *pausetime - 1;
  506. }
  507. else if (!*pausetime && *busywaittime)
  508. {
  509. /*
  510. * user specified a busy wait time but no pause time - must
  511. * set pause time to 1 more than busy wait time
  512. */
  513. *pausetime = *busywaittime + 1;
  514. }
  515. else if (*pausetime && *busywaittime && *pausetime <= *busywaittime)
  516. {
  517. /*
  518. * user specified both pause and busy wait times, but the pause
  519. * time was <= busy wait time - pause time must be at least
  520. * 1 more than the busy wait time
  521. */
  522. *pausetime = *busywaittime + 1;
  523. }
  524. }
  525. /*
  526. * Do the incremental protocol.
  527. *
  528. * What's going on here? This thing is a state machine. It has the
  529. * following states:
  530. *
  531. * State transition table:
  532. *
  533. * Curr State Condition/Event Next State
  534. * ---------- ------------ -----------
  535. * START schedule window is open ACQUIRE_REPLICA
  536. * schedule window is closed WAIT_WINDOW_OPEN
  537. * WAIT_WINDOW_OPEN schedule change START
  538. * replicate now ACQUIRE_REPLICA
  539. * schedule window opens ACQUIRE_REPLICA
  540. * ACQUIRE_REPLICA acquired replica SEND_CHANGES
  541. * failed to acquire - transient error START_BACKOFF
  542. * failed to acquire - fatal error STOP_FATAL_ERROR
  543. * SEND_CHANGES can't update CONSUMER_NEEDS_REINIT
  544. * no changes to send WAIT_CHANGES
  545. * can't send - thransient error START_BACKOF
  546. * can't send - window closed WAIT_WINDOW_OPEN
  547. * can'r send - fatal error STOP_FATAL_ERROR
  548. * START_BACKOF replicate now ACQUIRE_REPLICA
  549. * schedule changes START
  550. * schedule window closes WAIT_WINDOW_OPEN
  551. * backoff expires & can acquire SEND_CHANGES
  552. * backoff expires & can't acquire-trans BACKOFF
  553. * backoff expires & can't acquire-fatal STOP_FATAL_ERROR
  554. * BACKOF replicate now ACQUIRE_REPLICA
  555. * schedule changes START
  556. * schedule window closes WAIT_WINDOW_OPEN
  557. * backoff expires & can acquire SEND_CHANGES
  558. * backoff expires & can't acquire-trans BACKOFF
  559. * backoff expires & can't acquire-fatal STOP_FATAL_ERROR
  560. * WAIT_CHANGES schedule window closes WAIT_WINDOW_OPEN
  561. * replicate_now ACQUIRE_REPLICA
  562. * change available ACQUIRE_REPLICA
  563. * schedule_change START
  564. */
  565. /*
  566. * Main state machine for the incremental protocol. This routine will,
  567. * under normal circumstances, not return until the protocol is shut
  568. * down.
  569. */
  570. static void
  571. repl5_inc_run(Private_Repl_Protocol *prp)
  572. {
  573. int current_state = STATE_START;
  574. int next_state = STATE_START;
  575. repl5_inc_private *prp_priv = (repl5_inc_private *)prp->private;
  576. int done;
  577. int e1;
  578. RUV *ruv = NULL;
  579. CSN *cons_schema_csn;
  580. Replica *replica = NULL;
  581. int wait_change_timer_set = 0;
  582. time_t last_start_time;
  583. PRUint32 num_changes_sent;
  584. /* use a different backoff timer strategy for ACQUIRE_REPLICA_BUSY errors */
  585. PRBool use_busy_backoff_timer = PR_FALSE;
  586. long pausetime = 0;
  587. long busywaittime = 0;
  588. prp->stopped = 0;
  589. prp->terminate = 0;
  590. /* establish_protocol_callbacks(prp); */
  591. done = 0;
  592. do {
  593. int rc;
  594. /* Take action, based on current state, and compute new state. */
  595. switch (current_state)
  596. {
  597. case STATE_START:
  598. dev_debug("repl5_inc_run(STATE_START)");
  599. if (PROTOCOL_IS_SHUTDOWN(prp))
  600. {
  601. done = 1;
  602. break;
  603. }
  604. /*
  605. * Our initial state. See if we're in a schedule window. If
  606. * so, then we're ready to acquire the replica and see if it
  607. * needs any updates from us. If not, then wait for the window
  608. * to open.
  609. */
  610. if (agmt_schedule_in_window_now(prp->agmt))
  611. {
  612. next_state = STATE_READY_TO_ACQUIRE;
  613. }
  614. else
  615. {
  616. next_state = STATE_WAIT_WINDOW_OPEN;
  617. }
  618. /* we can get here from other states because some events happened and were
  619. not cleared. For instance when we wake up in STATE_WAIT_CHANGES state.
  620. Since this is a fresh start state, we should clear all events */
  621. /* ONREPL - this does not feel right - we should take another look
  622. at this state machine */
  623. reset_events (prp);
  624. /* Cancel any linger timer that might be in effect... */
  625. conn_cancel_linger(prp->conn);
  626. /* ... and disconnect, if currently connected */
  627. conn_disconnect(prp->conn);
  628. /* get the new pause time, if any */
  629. pausetime = agmt_get_pausetime(prp->agmt);
  630. /* get the new busy wait time, if any */
  631. busywaittime = agmt_get_busywaittime(prp->agmt);
  632. if (pausetime || busywaittime)
  633. {
  634. /* helper function to make sure they are set correctly */
  635. set_pause_and_busy_time(&pausetime, &busywaittime);
  636. }
  637. break;
  638. case STATE_WAIT_WINDOW_OPEN:
  639. /*
  640. * We're waiting for a schedule window to open. If one did,
  641. * or we receive a "replicate now" event, then start a protocol
  642. * session immediately. If the replication schedule changed, go
  643. * back to start. Otherwise, go back to sleep.
  644. */
  645. dev_debug("repl5_inc_run(STATE_WAIT_WINDOW_OPEN)");
  646. if (PROTOCOL_IS_SHUTDOWN(prp))
  647. {
  648. done = 1;
  649. break;
  650. }
  651. else if (event_occurred(prp, EVENT_WINDOW_OPENED))
  652. {
  653. next_state = STATE_READY_TO_ACQUIRE;
  654. }
  655. else if (event_occurred(prp, EVENT_REPLICATE_NOW))
  656. {
  657. next_state = STATE_READY_TO_ACQUIRE;
  658. }
  659. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  660. {
  661. next_state = STATE_START;
  662. conn_set_agmt_changed(prp->conn);
  663. }
  664. else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET)) /* change available */
  665. {
  666. /* just ignore it and go to sleep */
  667. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  668. }
  669. else if ((e1 = event_occurred(prp, EVENT_WINDOW_CLOSED)) ||
  670. event_occurred(prp, EVENT_BACKOFF_EXPIRED))
  671. {
  672. /* this events - should not occur - log a warning and go to sleep */
  673. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  674. "%s: Incremental protocol: "
  675. "event %s should not occur in state %s; going to sleep\n",
  676. agmt_get_long_name(prp->agmt),
  677. e1 ? event2name(EVENT_WINDOW_CLOSED) : event2name(EVENT_BACKOFF_EXPIRED),
  678. state2name(current_state));
  679. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  680. }
  681. else
  682. {
  683. /* wait until window opens or an event occurs */
  684. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  685. "%s: Incremental protocol: "
  686. "waiting for update window to open\n", agmt_get_long_name(prp->agmt));
  687. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  688. }
  689. break;
  690. case STATE_WAIT_CHANGES:
  691. /*
  692. * We're in a replication window, but we're waiting for more
  693. * changes to accumulate before we actually hook up and send
  694. * them.
  695. */
  696. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES)");
  697. if (PROTOCOL_IS_SHUTDOWN(prp))
  698. {
  699. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): PROTOCOL_IS_SHUTING_DOWN -> end repl5_inc_run\n");
  700. done = 1;
  701. break;
  702. }
  703. else if (event_occurred(prp, EVENT_REPLICATE_NOW))
  704. {
  705. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_REPLICATE_NOW received -> STATE_READY_TO_ACQUIRE\n");
  706. next_state = STATE_READY_TO_ACQUIRE;
  707. wait_change_timer_set = 0;
  708. }
  709. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  710. {
  711. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_AGMT_CHANGED received -> STATE_START\n");
  712. next_state = STATE_START;
  713. conn_set_agmt_changed(prp->conn);
  714. wait_change_timer_set = 0;
  715. }
  716. else if (event_occurred(prp, EVENT_WINDOW_CLOSED))
  717. {
  718. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_WINDOW_CLOSED received -> STATE_WAIT_WINDOW_OPEN\n");
  719. next_state = STATE_WAIT_WINDOW_OPEN;
  720. wait_change_timer_set = 0;
  721. }
  722. else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET))
  723. {
  724. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_TRIGGERING_CRITERIA_MET received -> STATE_READY_TO_ACQUIRE\n");
  725. next_state = STATE_READY_TO_ACQUIRE;
  726. wait_change_timer_set = 0;
  727. }
  728. else if ((e1 = event_occurred(prp, EVENT_WINDOW_OPENED)) ||
  729. event_occurred(prp, EVENT_BACKOFF_EXPIRED))
  730. {
  731. /* this events - should not occur - log a warning and clear the event */
  732. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: Incremental protocol: "
  733. "event %s should not occur in state %s\n",
  734. agmt_get_long_name(prp->agmt),
  735. e1 ? event2name(EVENT_WINDOW_OPENED) : event2name(EVENT_BACKOFF_EXPIRED),
  736. state2name(current_state));
  737. wait_change_timer_set = 0;
  738. }
  739. else
  740. {
  741. if (wait_change_timer_set)
  742. {
  743. /* We are here because our timer expired */
  744. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): wait_change_timer_set expired -> STATE_START\n");
  745. next_state = STATE_START;
  746. wait_change_timer_set = 0;
  747. }
  748. else
  749. {
  750. /* We are here because the last replication session
  751. * finished or aborted.
  752. */
  753. wait_change_timer_set = 1;
  754. protocol_sleep(prp, MAX_WAIT_BETWEEN_SESSIONS);
  755. }
  756. }
  757. break;
  758. case STATE_READY_TO_ACQUIRE:
  759. dev_debug("repl5_inc_run(STATE_READY_TO_ACQUIRE)");
  760. if (PROTOCOL_IS_SHUTDOWN(prp))
  761. {
  762. done = 1;
  763. break;
  764. }
  765. /* ONREPL - at this state we unconditionally acquire the replica
  766. ignoring all events. Not sure if this is good */
  767. object_acquire(prp->replica_object);
  768. rc = acquire_replica(prp, REPL_NSDS50_INCREMENTAL_PROTOCOL_OID, &ruv);
  769. use_busy_backoff_timer = PR_FALSE; /* default */
  770. if (rc == ACQUIRE_SUCCESS)
  771. {
  772. next_state = STATE_SENDING_UPDATES;
  773. }
  774. else if (rc == ACQUIRE_REPLICA_BUSY)
  775. {
  776. next_state = STATE_BACKOFF_START;
  777. use_busy_backoff_timer = PR_TRUE;
  778. }
  779. else if (rc == ACQUIRE_CONSUMER_WAS_UPTODATE)
  780. {
  781. next_state = STATE_WAIT_CHANGES;
  782. }
  783. else if (rc == ACQUIRE_TRANSIENT_ERROR)
  784. {
  785. next_state = STATE_BACKOFF_START;
  786. }
  787. else if (rc == ACQUIRE_FATAL_ERROR)
  788. {
  789. next_state = STATE_STOP_FATAL_ERROR;
  790. }
  791. if (rc != ACQUIRE_SUCCESS)
  792. {
  793. int optype, ldaprc;
  794. conn_get_error(prp->conn, &optype, &ldaprc);
  795. agmt_set_last_update_status(prp->agmt, ldaprc,
  796. prp->last_acquire_response_code, NULL);
  797. }
  798. object_release(prp->replica_object);
  799. break;
  800. case STATE_BACKOFF_START:
  801. dev_debug("repl5_inc_run(STATE_BACKOFF_START)");
  802. if (PROTOCOL_IS_SHUTDOWN(prp))
  803. {
  804. done = 1;
  805. break;
  806. }
  807. if (event_occurred(prp, EVENT_REPLICATE_NOW))
  808. {
  809. next_state = STATE_READY_TO_ACQUIRE;
  810. }
  811. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  812. {
  813. next_state = STATE_START;
  814. conn_set_agmt_changed(prp->conn);
  815. }
  816. else if (event_occurred (prp, EVENT_WINDOW_CLOSED))
  817. {
  818. next_state = STATE_WAIT_WINDOW_OPEN;
  819. }
  820. else if (event_occurred (prp, EVENT_TRIGGERING_CRITERIA_MET))
  821. {
  822. /* consume and ignore */
  823. }
  824. else if ((e1 = event_occurred (prp, EVENT_WINDOW_OPENED)) ||
  825. event_occurred (prp, EVENT_BACKOFF_EXPIRED))
  826. {
  827. /* This should never happen */
  828. /* this events - should not occur - log a warning and go to sleep */
  829. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  830. "%s: Incremental protocol: event %s should not occur in state %s\n",
  831. agmt_get_long_name(prp->agmt),
  832. e1 ? event2name(EVENT_WINDOW_OPENED) : event2name(EVENT_BACKOFF_EXPIRED),
  833. state2name(current_state));
  834. }
  835. else
  836. {
  837. /* Set up the backoff timer to wake us up at the appropriate time */
  838. if (use_busy_backoff_timer)
  839. {
  840. /* we received a busy signal from the consumer, wait for a while */
  841. if (!busywaittime)
  842. {
  843. busywaittime = PROTOCOL_BUSY_BACKOFF_MINIMUM;
  844. }
  845. prp_priv->backoff = backoff_new(BACKOFF_FIXED, busywaittime,
  846. busywaittime);
  847. }
  848. else
  849. {
  850. prp_priv->backoff = backoff_new(BACKOFF_EXPONENTIAL, PROTOCOL_BACKOFF_MINIMUM,
  851. PROTOCOL_BACKOFF_MAXIMUM);
  852. }
  853. next_state = STATE_BACKOFF;
  854. backoff_reset(prp_priv->backoff, repl5_inc_backoff_expired, (void *)prp);
  855. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  856. use_busy_backoff_timer = PR_FALSE;
  857. }
  858. break;
  859. case STATE_BACKOFF:
  860. /*
  861. * We're in a backoff state.
  862. */
  863. dev_debug("repl5_inc_run(STATE_BACKOFF)");
  864. if (PROTOCOL_IS_SHUTDOWN(prp))
  865. {
  866. if (prp_priv->backoff)
  867. backoff_delete(&prp_priv->backoff);
  868. done = 1;
  869. break;
  870. }
  871. else if (event_occurred(prp, EVENT_REPLICATE_NOW))
  872. {
  873. next_state = STATE_READY_TO_ACQUIRE;
  874. }
  875. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  876. {
  877. next_state = STATE_START;
  878. conn_set_agmt_changed(prp->conn);
  879. /* Destroy the backoff timer, since we won't need it anymore */
  880. if (prp_priv->backoff)
  881. backoff_delete(&prp_priv->backoff);
  882. }
  883. else if (event_occurred(prp, EVENT_WINDOW_CLOSED))
  884. {
  885. next_state = STATE_WAIT_WINDOW_OPEN;
  886. /* Destroy the backoff timer, since we won't need it anymore */
  887. if (prp_priv->backoff)
  888. backoff_delete(&prp_priv->backoff);
  889. }
  890. else if (event_occurred(prp, EVENT_BACKOFF_EXPIRED))
  891. {
  892. rc = acquire_replica(prp, REPL_NSDS50_INCREMENTAL_PROTOCOL_OID, &ruv);
  893. use_busy_backoff_timer = PR_FALSE;
  894. if (rc == ACQUIRE_SUCCESS)
  895. {
  896. next_state = STATE_SENDING_UPDATES;
  897. }
  898. else if (rc == ACQUIRE_REPLICA_BUSY)
  899. {
  900. next_state = STATE_BACKOFF;
  901. use_busy_backoff_timer = PR_TRUE;
  902. }
  903. else if (rc == ACQUIRE_CONSUMER_WAS_UPTODATE)
  904. {
  905. next_state = STATE_WAIT_CHANGES;
  906. }
  907. else if (rc == ACQUIRE_TRANSIENT_ERROR)
  908. {
  909. next_state = STATE_BACKOFF;
  910. }
  911. else if (rc == ACQUIRE_FATAL_ERROR)
  912. {
  913. next_state = STATE_STOP_FATAL_ERROR;
  914. }
  915. if (rc != ACQUIRE_SUCCESS)
  916. {
  917. int optype, ldaprc;
  918. conn_get_error(prp->conn, &optype, &ldaprc);
  919. agmt_set_last_update_status(prp->agmt, ldaprc,
  920. prp->last_acquire_response_code, NULL);
  921. }
  922. /*
  923. * We either need to step the backoff timer, or
  924. * destroy it if we don't need it anymore.
  925. */
  926. if (STATE_BACKOFF == next_state)
  927. {
  928. time_t next_fire_time;
  929. time_t now;
  930. /* Step the backoff timer */
  931. time(&now);
  932. next_fire_time = backoff_step(prp_priv->backoff);
  933. /* And go back to sleep */
  934. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  935. "%s: Replication session backing off for %ld seconds\n",
  936. agmt_get_long_name(prp->agmt),
  937. next_fire_time - now);
  938. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  939. }
  940. else
  941. {
  942. /* Destroy the backoff timer, since we won't need it anymore */
  943. backoff_delete(&prp_priv->backoff);
  944. }
  945. use_busy_backoff_timer = PR_FALSE;
  946. }
  947. else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET))
  948. {
  949. /* changes are available */
  950. if ( prp_priv->backoff == NULL || backoff_expired (prp_priv->backoff, 60) )
  951. {
  952. /*
  953. * Have seen cases that the agmt stuck here forever since
  954. * somehow the backoff timer was not in event queue anymore.
  955. * If the backoff timer has expired more than 60 seconds,
  956. * destroy it.
  957. */
  958. if ( prp_priv->backoff )
  959. backoff_delete(&prp_priv->backoff);
  960. next_state = STATE_READY_TO_ACQUIRE;
  961. }
  962. else
  963. {
  964. /* ignore changes and go to sleep */
  965. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  966. }
  967. }
  968. else if (event_occurred(prp, EVENT_WINDOW_OPENED))
  969. {
  970. /* this should never happen - log an error and go to sleep */
  971. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: Incremental protocol: "
  972. "event %s should not occur in state %s; going to sleep\n",
  973. agmt_get_long_name(prp->agmt),
  974. event2name(EVENT_WINDOW_OPENED), state2name(current_state));
  975. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  976. }
  977. break;
  978. case STATE_SENDING_UPDATES:
  979. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES)");
  980. agmt_set_update_in_progress(prp->agmt, PR_TRUE);
  981. num_changes_sent = 0;
  982. last_start_time = current_time();
  983. agmt_set_last_update_start(prp->agmt, last_start_time);
  984. /*
  985. * We've acquired the replica, and are ready to send any
  986. * needed updates.
  987. */
  988. if (PROTOCOL_IS_SHUTDOWN(prp))
  989. {
  990. release_replica (prp);
  991. done = 1;
  992. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  993. agmt_set_last_update_end(prp->agmt, current_time());
  994. /* MAB: I don't find the following status correct. How do we know it has
  995. been stopped by an admin and not by a total update request, for instance?
  996. In any case, how is this protocol shutdown situation different from all the
  997. other ones that are present in this state machine? */
  998. /* richm: We at least need to let monitors know that the protocol has been
  999. shutdown - maybe they can figure out why */
  1000. agmt_set_last_update_status(prp->agmt, 0, 0, "Protocol stopped");
  1001. break;
  1002. }
  1003. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update started");
  1004. /* ONREPL - in this state we send changes no matter what other events occur.
  1005. This is because we can get because of the REPLICATE_NOW event which
  1006. has high priority. Is this ok? */
  1007. /* First, push new schema to the consumer if needed */
  1008. /* ONREPL - should we push schema after we examine the RUV? */
  1009. /*
  1010. * GGOOREPL - I don't see why we should wait until we've
  1011. * examined the RUV. The schema entry has its own CSN that is
  1012. * used to decide if the remote schema needs to be updated.
  1013. */
  1014. cons_schema_csn = agmt_get_consumer_schema_csn ( prp->agmt );
  1015. rc = conn_push_schema(prp->conn, &cons_schema_csn);
  1016. if ( cons_schema_csn != agmt_get_consumer_schema_csn ( prp->agmt ))
  1017. {
  1018. agmt_set_consumer_schema_csn ( prp->agmt, cons_schema_csn );
  1019. }
  1020. if (CONN_SCHEMA_UPDATED != rc && CONN_SCHEMA_NO_UPDATE_NEEDED != rc)
  1021. {
  1022. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1023. "%s: Warning: unable to replicate schema: rc=%d\n",
  1024. agmt_get_long_name(prp->agmt), rc);
  1025. /* But keep going */
  1026. }
  1027. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> examine_update_vector");
  1028. rc = examine_update_vector(prp, ruv);
  1029. /*
  1030. * Decide what to do next - proceed with incremental,
  1031. * backoff, or total update
  1032. */
  1033. switch (rc)
  1034. {
  1035. case EXAMINE_RUV_PARAM_ERROR:
  1036. /* this is really bad - we have NULL prp! */
  1037. next_state = STATE_STOP_FATAL_ERROR;
  1038. break;
  1039. case EXAMINE_RUV_PRISTINE_REPLICA:
  1040. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1041. "%s: Replica has no update vector. It has never been initialized.\n",
  1042. agmt_get_long_name(prp->agmt));
  1043. next_state = STATE_BACKOFF_START;
  1044. break;
  1045. case EXAMINE_RUV_GENERATION_MISMATCH:
  1046. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1047. "%s: Replica has a different generation ID than the local data.\n",
  1048. agmt_get_long_name(prp->agmt));
  1049. next_state = STATE_BACKOFF_START;
  1050. break;
  1051. case EXAMINE_RUV_REPLICA_TOO_OLD:
  1052. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1053. "%s: Replica update vector is too out of date to bring "
  1054. "into sync using the incremental protocol. The replica "
  1055. "must be reinitialized.\n", agmt_get_long_name(prp->agmt));
  1056. next_state = STATE_BACKOFF_START;
  1057. break;
  1058. case EXAMINE_RUV_OK:
  1059. /* update our csn generator state with the consumer's ruv data */
  1060. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> examine_update_vector OK");
  1061. object_acquire(prp->replica_object);
  1062. replica = object_get_data(prp->replica_object);
  1063. rc = replica_update_csngen_state (replica, ruv);
  1064. object_release (prp->replica_object);
  1065. replica = NULL;
  1066. if (rc == CSN_LIMIT_EXCEEDED) /* too much skew */
  1067. {
  1068. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1069. "%s: Incremental protocol: fatal error - too much time skew between replicas!\n",
  1070. agmt_get_long_name(prp->agmt));
  1071. next_state = STATE_STOP_FATAL_ERROR;
  1072. }
  1073. else if (rc != 0) /* internal error */
  1074. {
  1075. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1076. "%s: Incremental protocol: fatal internal error updating the CSN generator!\n",
  1077. agmt_get_long_name(prp->agmt));
  1078. next_state = STATE_STOP_FATAL_ERROR;
  1079. }
  1080. else
  1081. {
  1082. rc = send_updates(prp, ruv, &num_changes_sent);
  1083. if (rc == UPDATE_NO_MORE_UPDATES)
  1084. {
  1085. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_NO_MORE_UPDATES -> STATE_WAIT_CHANGES");
  1086. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update succeeded");
  1087. next_state = STATE_WAIT_CHANGES;
  1088. }
  1089. else if (rc == UPDATE_YIELD)
  1090. {
  1091. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_YIELD -> STATE_BACKOFF_START");
  1092. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update succeeded and yielded");
  1093. next_state = STATE_BACKOFF_START;
  1094. }
  1095. else if (rc == UPDATE_TRANSIENT_ERROR)
  1096. {
  1097. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_TRANSIENT_ERROR -> STATE_BACKOFF_START");
  1098. next_state = STATE_BACKOFF_START;
  1099. }
  1100. else if (rc == UPDATE_FATAL_ERROR)
  1101. {
  1102. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_FATAL_ERROR -> STATE_STOP_FATAL_ERROR");
  1103. next_state = STATE_STOP_FATAL_ERROR;
  1104. }
  1105. else if (rc == UPDATE_SCHEDULE_WINDOW_CLOSED)
  1106. {
  1107. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_SCHEDULE_WINDOW_CLOSED -> STATE_WAIT_WINDOW_OPEN");
  1108. /* ONREPL - I don't think we should check this. We might be
  1109. here because of replicate_now event - so we don't care
  1110. about the schedule */
  1111. next_state = STATE_WAIT_WINDOW_OPEN;
  1112. /* ONREPL - do we need to release the replica here ? */
  1113. conn_disconnect (prp->conn);
  1114. }
  1115. else if (rc == UPDATE_CONNECTION_LOST)
  1116. {
  1117. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_CONNECTION_LOST -> STATE_BACKOFF_START");
  1118. next_state = STATE_BACKOFF_START;
  1119. }
  1120. else if (rc == UPDATE_TIMEOUT)
  1121. {
  1122. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_TIMEOUT -> STATE_BACKOFF_START");
  1123. next_state = STATE_BACKOFF_START;
  1124. }
  1125. }
  1126. last_start_time = 0UL;
  1127. break;
  1128. }
  1129. if (NULL != ruv)
  1130. {
  1131. ruv_destroy(&ruv); ruv = NULL;
  1132. }
  1133. agmt_set_last_update_end(prp->agmt, current_time());
  1134. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  1135. /* If timed out, close the connection after released the replica */
  1136. release_replica(prp);
  1137. if (rc == UPDATE_TIMEOUT) {
  1138. conn_disconnect(prp->conn);
  1139. }
  1140. if (rc == UPDATE_NO_MORE_UPDATES && num_changes_sent > 0)
  1141. {
  1142. if (pausetime > 0)
  1143. {
  1144. /* richm - 20020219 - If we have acquired the consumer, and another master has gone
  1145. into backoff waiting for us to release it, we may acquire the replica sooner
  1146. than the other master has a chance to, and the other master may not be able
  1147. to acquire the consumer for a long time (hours, days?) if this server is
  1148. under a heavy load (see reliab06 et. al. system tests)
  1149. So, this sleep gives the other master(s) a chance to acquire the consumer
  1150. replica */
  1151. long loops = pausetime;
  1152. /* the while loop is so that we don't just sleep and sleep if an
  1153. event comes in that we should handle immediately (like shutdown) */
  1154. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1155. "%s: Pausing updates for %ld seconds to allow other suppliers to update consumer\n",
  1156. agmt_get_long_name(prp->agmt), pausetime);
  1157. while (loops-- && !(PROTOCOL_IS_SHUTDOWN(prp)))
  1158. {
  1159. DS_Sleep(PR_SecondsToInterval(1));
  1160. }
  1161. }
  1162. else if (num_changes_sent > 10)
  1163. {
  1164. /* wait for consumer to write its ruv if the replication was busy */
  1165. /* When asked, consumer sends its ruv in cache to the supplier. */
  1166. /* DS_Sleep ( PR_SecondsToInterval(1) ); */
  1167. }
  1168. }
  1169. break;
  1170. case STATE_STOP_FATAL_ERROR:
  1171. /*
  1172. * We encountered some sort of a fatal error. Suspend.
  1173. */
  1174. /* XXXggood update state in replica */
  1175. agmt_set_last_update_status(prp->agmt, -1, 0, "Incremental update has failed and requires administrator action");
  1176. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR)");
  1177. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1178. "%s: Incremental update failed and requires administrator action\n",
  1179. agmt_get_long_name(prp->agmt));
  1180. next_state = STATE_STOP_FATAL_ERROR_PART2;
  1181. break;
  1182. case STATE_STOP_FATAL_ERROR_PART2:
  1183. if (PROTOCOL_IS_SHUTDOWN(prp))
  1184. {
  1185. done = 1;
  1186. break;
  1187. }
  1188. /* MAB: This state is the FATAL state where we are supposed to get
  1189. as a result of a FATAL error on send_updates. But, as bug
  1190. states, send_updates was always returning TRANSIENT errors and never
  1191. FATAL... In other words, this code has never been tested before...
  1192. As of 01/16/01, this piece of code was in a very dangerous state. In particular,
  1193. 1) it does not catch any events
  1194. 2) it is a terminal state (once reached it never transitions to a different state)
  1195. Both things combined make this state to become a consuming infinite loop
  1196. that is useless after all (we are in a fatal place requiring manual admin jobs */
  1197. /* MAB: The following lines fix problem number 1 above... When the code gets
  1198. into this state, it should only get a chance to get out of it by an
  1199. EVENT_AGMT_CHANGED event... All other events should be ignored */
  1200. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  1201. {
  1202. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR): EVENT_AGMT_CHANGED received\n");
  1203. /* Chance to recover for the EVENT_AGMT_CHANGED event.
  1204. This is not mandatory, but fixes problem 2 above */
  1205. next_state = STATE_STOP_NORMAL_TERMINATION;
  1206. }
  1207. else
  1208. {
  1209. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR): Event received. Clearing it\n");
  1210. reset_events (prp);
  1211. }
  1212. protocol_sleep (prp, PR_INTERVAL_NO_TIMEOUT);
  1213. break;
  1214. case STATE_STOP_NORMAL_TERMINATION:
  1215. /*
  1216. * We encountered some sort of a fatal error. Return.
  1217. */
  1218. /* XXXggood update state in replica */
  1219. dev_debug("repl5_inc_run(STATE_STOP_NORMAL_TERMINATION)");
  1220. done = 1;
  1221. break;
  1222. }
  1223. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1224. "%s: State: %s -> %s\n",
  1225. agmt_get_long_name(prp->agmt),
  1226. state2name(current_state), state2name(next_state));
  1227. current_state = next_state;
  1228. } while (!done);
  1229. /* remove_protocol_callbacks(prp); */
  1230. prp->stopped = 1;
  1231. /* Cancel any linger timer that might be in effect... */
  1232. conn_cancel_linger(prp->conn);
  1233. /* ... and disconnect, if currently connected */
  1234. conn_disconnect(prp->conn);
  1235. }
  1236. /*
  1237. * Go to sleep until awakened.
  1238. */
  1239. static void
  1240. protocol_sleep(Private_Repl_Protocol *prp, PRIntervalTime duration)
  1241. {
  1242. PR_ASSERT(NULL != prp);
  1243. PR_Lock(prp->lock);
  1244. /* we should not go to sleep if there are events available to be processed.
  1245. Otherwise, we can miss the event that suppose to wake us up */
  1246. if (prp->eventbits == 0)
  1247. PR_WaitCondVar(prp->cvar, duration);
  1248. else
  1249. {
  1250. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1251. "%s: Incremental protocol: can't go to sleep: event bits - %x\n",
  1252. agmt_get_long_name(prp->agmt), prp->eventbits);
  1253. }
  1254. PR_Unlock(prp->lock);
  1255. }
  1256. /*
  1257. * Notify the protocol about some event. Signal the condition
  1258. * variable in case the protocol is sleeping. Multiple occurences
  1259. * of a single event type are not remembered (e.g. no stack
  1260. * of events is maintained).
  1261. */
  1262. static void
  1263. event_notify(Private_Repl_Protocol *prp, PRUint32 event)
  1264. {
  1265. PR_ASSERT(NULL != prp);
  1266. PR_Lock(prp->lock);
  1267. prp->eventbits |= event;
  1268. PR_NotifyCondVar(prp->cvar);
  1269. PR_Unlock(prp->lock);
  1270. }
  1271. /*
  1272. * Test to see if an event occurred. The event is cleared when
  1273. * read.
  1274. */
  1275. static PRUint32
  1276. event_occurred(Private_Repl_Protocol *prp, PRUint32 event)
  1277. {
  1278. PRUint32 return_value;
  1279. PR_ASSERT(NULL != prp);
  1280. PR_Lock(prp->lock);
  1281. return_value = (prp->eventbits & event);
  1282. prp->eventbits &= ~event; /* Clear event */
  1283. PR_Unlock(prp->lock);
  1284. return return_value;
  1285. }
  1286. static void
  1287. reset_events (Private_Repl_Protocol *prp)
  1288. {
  1289. PR_ASSERT(NULL != prp);
  1290. PR_Lock(prp->lock);
  1291. prp->eventbits = 0;
  1292. PR_Unlock(prp->lock);
  1293. }
  1294. /*
  1295. * Replay the actual update to the consumer. Construct an appropriate LDAP
  1296. * operation, attach the baggage LDAPv3 control that contains the CSN, etc.,
  1297. * and send the operation to the consumer.
  1298. */
  1299. ConnResult
  1300. replay_update(Private_Repl_Protocol *prp, slapi_operation_parameters *op, int *message_id)
  1301. {
  1302. ConnResult return_value = CONN_OPERATION_FAILED;
  1303. LDAPControl *update_control;
  1304. char *parentuniqueid;
  1305. LDAPMod **modrdn_mods = NULL;
  1306. char csn_str[CSN_STRSIZE]; /* For logging only */
  1307. if (message_id) {
  1308. /* if we get out of this function without setting message_id, it means
  1309. we didn't send an op, so no result needs to be processed */
  1310. *message_id = 0;
  1311. }
  1312. /* Construct the replication info control that accompanies the operation */
  1313. if (SLAPI_OPERATION_ADD == op->operation_type)
  1314. {
  1315. parentuniqueid = op->p.p_add.parentuniqueid;
  1316. }
  1317. else if (SLAPI_OPERATION_MODRDN == op->operation_type)
  1318. {
  1319. /*
  1320. * For modrdn operations, we need to send along modified attributes, e.g.
  1321. * modifytimestamp.
  1322. * And the superior_uniqueid !
  1323. */
  1324. modrdn_mods = op->p.p_modrdn.modrdn_mods;
  1325. parentuniqueid = op->p.p_modrdn.modrdn_newsuperior_address.uniqueid;
  1326. }
  1327. else
  1328. {
  1329. parentuniqueid = NULL;
  1330. }
  1331. if (create_NSDS50ReplUpdateInfoControl(op->target_address.uniqueid,
  1332. parentuniqueid, op->csn, modrdn_mods, &update_control) != LDAP_SUCCESS)
  1333. {
  1334. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1335. "%s: replay_update: Unable to create NSDS50ReplUpdateInfoControl "
  1336. "for operation with csn %s. Skipping update.\n",
  1337. agmt_get_long_name(prp->agmt), csn_as_string(op->csn, PR_FALSE, csn_str));
  1338. }
  1339. else
  1340. {
  1341. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1342. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1343. "%s: replay_update: Sending %s operation (dn=\"%s\" csn=%s)\n",
  1344. agmt_get_long_name(prp->agmt),
  1345. op2string(op->operation_type), REPL_GET_DN(&op->target_address),
  1346. csn_as_string(op->csn, PR_FALSE, csn_str));
  1347. }
  1348. /* What type of operation is it? */
  1349. switch (op->operation_type)
  1350. {
  1351. case SLAPI_OPERATION_ADD:
  1352. {
  1353. LDAPMod **entryattrs;
  1354. /* Convert entry to mods */
  1355. (void)slapi_entry2mods (op->p.p_add.target_entry,
  1356. NULL /* &entrydn : We don't need it */,
  1357. &entryattrs);
  1358. if (NULL == entryattrs)
  1359. {
  1360. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1361. "%s: replay_update: Cannot convert entry to LDAPMods.\n",
  1362. agmt_get_long_name(prp->agmt));
  1363. return_value = CONN_LOCAL_ERROR;
  1364. }
  1365. else
  1366. {
  1367. /* If fractional agreement, trim down the entry */
  1368. if (agmt_is_fractional(prp->agmt))
  1369. {
  1370. repl5_strip_fractional_mods(prp->agmt,entryattrs);
  1371. }
  1372. if (MODS_ARE_EMPTY(entryattrs)) {
  1373. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1374. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1375. "%s: replay_update: %s operation (dn=\"%s\" csn=%s) "
  1376. "not sent - empty\n",
  1377. agmt_get_long_name(prp->agmt),
  1378. op2string(op->operation_type),
  1379. REPL_GET_DN(&op->target_address),
  1380. csn_as_string(op->csn, PR_FALSE, csn_str));
  1381. }
  1382. return_value = CONN_OPERATION_SUCCESS;
  1383. } else {
  1384. return_value = conn_send_add(prp->conn, REPL_GET_DN(&op->target_address),
  1385. entryattrs, update_control, message_id);
  1386. }
  1387. ldap_mods_free(entryattrs, 1);
  1388. }
  1389. break;
  1390. }
  1391. case SLAPI_OPERATION_MODIFY:
  1392. /* If fractional agreement, trim down the mods */
  1393. if (agmt_is_fractional(prp->agmt))
  1394. {
  1395. repl5_strip_fractional_mods(prp->agmt,op->p.p_modify.modify_mods);
  1396. }
  1397. if (MODS_ARE_EMPTY(op->p.p_modify.modify_mods)) {
  1398. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1399. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1400. "%s: replay_update: %ss operation (dn=\"%s\" csn=%s) "
  1401. "not sent - empty\n",
  1402. agmt_get_long_name(prp->agmt),
  1403. op2string(op->operation_type),
  1404. REPL_GET_DN(&op->target_address),
  1405. csn_as_string(op->csn, PR_FALSE, csn_str));
  1406. }
  1407. return_value = CONN_OPERATION_SUCCESS;
  1408. } else {
  1409. return_value = conn_send_modify(prp->conn, REPL_GET_DN(&op->target_address),
  1410. op->p.p_modify.modify_mods, update_control, message_id);
  1411. }
  1412. break;
  1413. case SLAPI_OPERATION_DELETE:
  1414. return_value = conn_send_delete(prp->conn, REPL_GET_DN(&op->target_address),
  1415. update_control, message_id);
  1416. break;
  1417. case SLAPI_OPERATION_MODRDN:
  1418. /* XXXggood need to pass modrdn mods in update control! */
  1419. return_value = conn_send_rename(prp->conn, REPL_GET_DN(&op->target_address),
  1420. op->p.p_modrdn.modrdn_newrdn,
  1421. REPL_GET_DN(&op->p.p_modrdn.modrdn_newsuperior_address),
  1422. op->p.p_modrdn.modrdn_deloldrdn,
  1423. update_control, message_id);
  1424. break;
  1425. default:
  1426. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: replay_update: Unknown "
  1427. "operation type %lu found in changelog - skipping change.\n",
  1428. agmt_get_long_name(prp->agmt), op->operation_type);
  1429. }
  1430. destroy_NSDS50ReplUpdateInfoControl(&update_control);
  1431. }
  1432. if (CONN_OPERATION_SUCCESS == return_value)
  1433. {
  1434. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1435. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1436. "%s: replay_update: Consumer successfully sent operation with csn %s\n",
  1437. agmt_get_long_name(prp->agmt), csn_as_string(op->csn, PR_FALSE, csn_str));
  1438. }
  1439. }
  1440. else
  1441. {
  1442. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1443. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1444. "%s: replay_update: Consumer could not replay operation with csn %s\n",
  1445. agmt_get_long_name(prp->agmt), csn_as_string(op->csn, PR_FALSE, csn_str));
  1446. }
  1447. }
  1448. return return_value;
  1449. }
  1450. static PRBool
  1451. is_dummy_operation (const slapi_operation_parameters *op)
  1452. {
  1453. return (strcmp (op->target_address.uniqueid, START_ITERATION_ENTRY_UNIQUEID) == 0);
  1454. }
  1455. void
  1456. cl5_operation_parameters_done (struct slapi_operation_parameters *sop)
  1457. {
  1458. if(sop!=NULL) {
  1459. switch(sop->operation_type)
  1460. {
  1461. case SLAPI_OPERATION_BIND:
  1462. slapi_ch_free((void **)&(sop->p.p_bind.bind_saslmechanism));
  1463. if (sop->p.p_bind.bind_creds)
  1464. ber_bvecfree((struct berval**)&(sop->p.p_bind.bind_creds));
  1465. if (sop->p.p_bind.bind_ret_saslcreds)
  1466. ber_bvecfree((struct berval**)&(sop->p.p_bind.bind_ret_saslcreds));
  1467. sop->p.p_bind.bind_creds = NULL;
  1468. sop->p.p_bind.bind_ret_saslcreds = NULL;
  1469. break;
  1470. case SLAPI_OPERATION_COMPARE:
  1471. ava_done((struct ava *)&(sop->p.p_compare.compare_ava));
  1472. break;
  1473. case SLAPI_OPERATION_SEARCH:
  1474. slapi_ch_free((void **)&(sop->p.p_search.search_strfilter));
  1475. charray_free(sop->p.p_search.search_attrs);
  1476. slapi_filter_free(sop->p.p_search.search_filter,1);
  1477. break;
  1478. case SLAPI_OPERATION_MODRDN:
  1479. sop->p.p_modrdn.modrdn_deloldrdn = 0;
  1480. break;
  1481. case SLAPI_OPERATION_EXTENDED:
  1482. slapi_ch_free((void **)&(sop->p.p_extended.exop_oid));
  1483. if (sop->p.p_extended.exop_value)
  1484. ber_bvecfree((struct berval**)&(sop->p.p_extended.exop_value));
  1485. sop->p.p_extended.exop_value = NULL;
  1486. break;
  1487. default:
  1488. break;
  1489. }
  1490. }
  1491. operation_parameters_done(sop);
  1492. }
  1493. /* Helper to update the agreement state based on a the result of a replay operation */
  1494. static int
  1495. repl5_inc_update_from_op_result(Private_Repl_Protocol *prp, ConnResult replay_crc, int connection_error, char *csn_str, char *uniqueid, ReplicaId replica_id, int* finished, PRUint32 *num_changes_sent)
  1496. {
  1497. int return_value = 0;
  1498. /* Indentation is wrong here so we can get a sensible cvs diff */
  1499. if (CONN_OPERATION_SUCCESS != replay_crc)
  1500. {
  1501. /* Figure out what to do next */
  1502. if (CONN_OPERATION_FAILED == replay_crc)
  1503. {
  1504. /* Map ldap error code to return value */
  1505. if (!ignore_error_and_keep_going(connection_error))
  1506. {
  1507. return_value = UPDATE_TRANSIENT_ERROR;
  1508. *finished = 1;
  1509. }
  1510. else
  1511. {
  1512. agmt_inc_last_update_changecount (prp->agmt, replica_id, 1 /*skipped*/);
  1513. }
  1514. slapi_log_error(*finished ? SLAPI_LOG_FATAL : slapi_log_urp, repl_plugin_name,
  1515. "%s: Consumer failed to replay change (uniqueid %s, CSN %s): %s. %s.\n",
  1516. agmt_get_long_name(prp->agmt),
  1517. uniqueid, csn_str,
  1518. ldap_err2string(connection_error),
  1519. *finished ? "Will retry later" : "Skipping");
  1520. }
  1521. else if (CONN_NOT_CONNECTED == replay_crc)
  1522. {
  1523. /* We lost the connection - enter backoff state */
  1524. return_value = UPDATE_CONNECTION_LOST;
  1525. *finished = 1;
  1526. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1527. "%s: Consumer failed to replay change (uniqueid %s, CSN %s): "
  1528. "%s. Will retry later.\n",
  1529. agmt_get_long_name(prp->agmt),
  1530. uniqueid, csn_str,
  1531. connection_error ? ldap_err2string(connection_error) : "Connection lost");
  1532. }
  1533. else if (CONN_TIMEOUT == replay_crc)
  1534. {
  1535. return_value = UPDATE_TIMEOUT;
  1536. *finished = 1;
  1537. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1538. "%s: Consumer timed out to replay change (uniqueid %s, CSN %s): "
  1539. "%s.\n",
  1540. agmt_get_long_name(prp->agmt),
  1541. uniqueid, csn_str,
  1542. connection_error ? ldap_err2string(connection_error) : "Timeout");
  1543. }
  1544. else if (CONN_LOCAL_ERROR == replay_crc)
  1545. {
  1546. /*
  1547. * Something bad happened on the local server - enter
  1548. * backoff state.
  1549. */
  1550. return_value = UPDATE_TRANSIENT_ERROR;
  1551. *finished = 1;
  1552. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1553. "%s: Failed to replay change (uniqueid %s, CSN %s): "
  1554. "Local error. Will retry later.\n",
  1555. agmt_get_long_name(prp->agmt),
  1556. uniqueid, csn_str);
  1557. }
  1558. }
  1559. else
  1560. {
  1561. /* Positive response received */
  1562. (*num_changes_sent)++;
  1563. agmt_inc_last_update_changecount (prp->agmt, replica_id, 0 /*replayed*/);
  1564. }
  1565. return return_value;
  1566. }
  1567. /*
  1568. * Send a set of updates to the replica. Assumes that (1) the replica
  1569. * has already been acquired, (2) that the consumer's update vector has
  1570. * been checked and (3) that it's ok to send incremental updates.
  1571. * Returns:
  1572. * UPDATE_NO_MORE_UPDATES - all updates were sent succussfully
  1573. * UPDATE_TRANSIENT_ERROR - some non-permanent error occurred. Try again later.
  1574. * UPDATE_FATAL_ERROR - some bad, permanent error occurred.
  1575. * UPDATE_SCHEDULE_WINDOW_CLOSED - the schedule window closed on us.
  1576. */
  1577. static int
  1578. send_updates(Private_Repl_Protocol *prp, RUV *remote_update_vector, PRUint32 *num_changes_sent)
  1579. {
  1580. CL5Entry entry;
  1581. slapi_operation_parameters op;
  1582. int return_value;
  1583. int rc;
  1584. CL5ReplayIterator *changelog_iterator;
  1585. int message_id = 0;
  1586. result_data *rd = NULL;
  1587. *num_changes_sent = 0;
  1588. /*
  1589. * Iterate over the changelog. Retrieve each update,
  1590. * construct an appropriate LDAP operation,
  1591. * attaching the CSN, and send the change.
  1592. */
  1593. rc = cl5CreateReplayIterator(prp, remote_update_vector, &changelog_iterator);
  1594. if (CL5_SUCCESS != rc)
  1595. {
  1596. switch (rc)
  1597. {
  1598. case CL5_BAD_DATA: /* invalid parameter passed to the function */
  1599. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1600. "%s: Invalid parameter passed to cl5CreateReplayIterator\n",
  1601. agmt_get_long_name(prp->agmt));
  1602. return_value = UPDATE_FATAL_ERROR;
  1603. break;
  1604. case CL5_BAD_FORMAT: /* db data has unexpected format */
  1605. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1606. "%s: Unexpected format encountered in changelog database\n",
  1607. agmt_get_long_name(prp->agmt));
  1608. return_value = UPDATE_FATAL_ERROR;
  1609. break;
  1610. case CL5_BAD_STATE: /* changelog is in an incorrect state for attempted operation */
  1611. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1612. "%s: Changelog database was in an incorrect state\n",
  1613. agmt_get_long_name(prp->agmt));
  1614. return_value = UPDATE_FATAL_ERROR;
  1615. break;
  1616. case CL5_BAD_DBVERSION: /* changelog has invalid dbversion */
  1617. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1618. "%s: Incorrect dbversion found in changelog database\n",
  1619. agmt_get_long_name(prp->agmt));
  1620. return_value = UPDATE_FATAL_ERROR;
  1621. break;
  1622. case CL5_DB_ERROR: /* database error */
  1623. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1624. "%s: A changelog database error was encountered\n",
  1625. agmt_get_long_name(prp->agmt));
  1626. return_value = UPDATE_FATAL_ERROR;
  1627. break;
  1628. case CL5_NOTFOUND: /* we have no changes to send */
  1629. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1630. "%s: No changes to send\n",
  1631. agmt_get_long_name(prp->agmt));
  1632. return_value = UPDATE_NO_MORE_UPDATES;
  1633. break;
  1634. case CL5_MEMORY_ERROR: /* memory allocation failed */
  1635. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1636. "%s: Memory allocation error occurred\n",
  1637. agmt_get_long_name(prp->agmt));
  1638. return_value = UPDATE_FATAL_ERROR;
  1639. break;
  1640. case CL5_SYSTEM_ERROR: /* NSPR error occurred: use PR_GetError for furhter info */
  1641. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1642. "%s: An NSPR error (%d) occurred\n",
  1643. agmt_get_long_name(prp->agmt), PR_GetError());
  1644. return_value = UPDATE_TRANSIENT_ERROR;
  1645. break;
  1646. case CL5_CSN_ERROR: /* CSN API failed */
  1647. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1648. "%s: A CSN API failure was encountered\n",
  1649. agmt_get_long_name(prp->agmt));
  1650. return_value = UPDATE_TRANSIENT_ERROR;
  1651. break;
  1652. case CL5_RUV_ERROR: /* RUV API failed */
  1653. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1654. "%s: An RUV API failure occurred\n",
  1655. agmt_get_long_name(prp->agmt));
  1656. return_value = UPDATE_TRANSIENT_ERROR;
  1657. break;
  1658. case CL5_OBJSET_ERROR: /* namedobjset api failed */
  1659. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1660. "%s: A namedobject API failure occurred\n",
  1661. agmt_get_long_name(prp->agmt));
  1662. return_value = UPDATE_TRANSIENT_ERROR;
  1663. break;
  1664. case CL5_PURGED_DATA: /* requested data has been purged */
  1665. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1666. "%s: Data required to update replica has been purged. "
  1667. "The replica must be reinitialized.\n",
  1668. agmt_get_long_name(prp->agmt));
  1669. return_value = UPDATE_FATAL_ERROR;
  1670. break;
  1671. case CL5_MISSING_DATA: /* data should be in the changelog, but is missing */
  1672. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1673. "%s: Missing data encountered\n",
  1674. agmt_get_long_name(prp->agmt));
  1675. return_value = UPDATE_FATAL_ERROR;
  1676. break;
  1677. case CL5_UNKNOWN_ERROR: /* unclassified error */
  1678. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1679. "%s: An unknown error was ecountered\n",
  1680. agmt_get_long_name(prp->agmt));
  1681. return_value = UPDATE_TRANSIENT_ERROR;
  1682. break;
  1683. default:
  1684. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1685. "%s: An unknown error (%d) occurred "
  1686. "(cl5CreateReplayIterator)\n",
  1687. agmt_get_long_name(prp->agmt), rc);
  1688. return_value = UPDATE_TRANSIENT_ERROR;
  1689. }
  1690. }
  1691. else
  1692. {
  1693. int finished = 0;
  1694. ConnResult replay_crc;
  1695. char csn_str[CSN_STRSIZE];
  1696. /* Start the results reading thread */
  1697. rd = repl5_inc_rd_new(prp);
  1698. if (!prp->repl50consumer)
  1699. {
  1700. rc = repl5_inc_create_async_result_thread(rd);
  1701. if (rc) {
  1702. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name, "%s: repl5_inc_run: "
  1703. "repl5_tot_create_async_result_thread failed; error - %d\n",
  1704. agmt_get_long_name(prp->agmt), rc);
  1705. return_value = UPDATE_FATAL_ERROR;
  1706. }
  1707. }
  1708. memset ( (void*)&op, 0, sizeof (op) );
  1709. entry.op = &op;
  1710. do {
  1711. cl5_operation_parameters_done ( entry.op );
  1712. memset ( (void*)entry.op, 0, sizeof (op) );
  1713. rc = cl5GetNextOperationToReplay(changelog_iterator, &entry);
  1714. switch (rc)
  1715. {
  1716. case CL5_SUCCESS:
  1717. /* check that we don't return dummy entries */
  1718. if (is_dummy_operation (entry.op))
  1719. {
  1720. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1721. "%s: changelog iteration code returned a dummy entry with csn %s, "
  1722. "skipping ...\n",
  1723. agmt_get_long_name(prp->agmt), csn_as_string(entry.op->csn, PR_FALSE, csn_str));
  1724. continue;
  1725. }
  1726. replay_crc = replay_update(prp, entry.op, &message_id);
  1727. if (message_id)
  1728. {
  1729. rd->last_message_id_sent = message_id;
  1730. }
  1731. /* If we're talking to an old non-async replica, we need to pick up the response here */
  1732. if (CONN_OPERATION_SUCCESS != replay_crc)
  1733. {
  1734. int operation, error;
  1735. conn_get_error(prp->conn, &operation, &error);
  1736. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1737. /* Figure out what to do next */
  1738. if (CONN_OPERATION_FAILED == replay_crc)
  1739. {
  1740. /* Map ldap error code to return value */
  1741. if (!ignore_error_and_keep_going(error))
  1742. {
  1743. return_value = UPDATE_TRANSIENT_ERROR;
  1744. finished = 1;
  1745. }
  1746. else
  1747. {
  1748. agmt_inc_last_update_changecount (prp->agmt, csn_get_replicaid(entry.op->csn), 1 /*skipped*/);
  1749. }
  1750. slapi_log_error(finished ? SLAPI_LOG_FATAL : slapi_log_urp, repl_plugin_name,
  1751. "%s: Failed to send update operation to consumer (uniqueid %s, CSN %s): %s. %s.\n",
  1752. agmt_get_long_name(prp->agmt),
  1753. entry.op->target_address.uniqueid, csn_str,
  1754. ldap_err2string(error),
  1755. finished ? "Will retry later" : "Skipping");
  1756. }
  1757. else if (CONN_NOT_CONNECTED == replay_crc)
  1758. {
  1759. /* We lost the connection - enter backoff state */
  1760. return_value = UPDATE_CONNECTION_LOST;
  1761. finished = 1;
  1762. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1763. "%s: Failed to send update operation to consumer (uniqueid %s, CSN %s): "
  1764. "%s. Will retry later.\n",
  1765. agmt_get_long_name(prp->agmt),
  1766. entry.op->target_address.uniqueid, csn_str,
  1767. error ? ldap_err2string(error) : "Connection lost");
  1768. }
  1769. else if (CONN_TIMEOUT == replay_crc)
  1770. {
  1771. return_value = UPDATE_TIMEOUT;
  1772. finished = 1;
  1773. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1774. "%s: Timed out sending update operation to consumer (uniqueid %s, CSN %s): "
  1775. "%s.\n",
  1776. agmt_get_long_name(prp->agmt),
  1777. entry.op->target_address.uniqueid, csn_str,
  1778. error ? ldap_err2string(error) : "Timeout");
  1779. }
  1780. else if (CONN_LOCAL_ERROR == replay_crc)
  1781. {
  1782. /*
  1783. * Something bad happened on the local server - enter
  1784. * backoff state.
  1785. */
  1786. return_value = UPDATE_TRANSIENT_ERROR;
  1787. finished = 1;
  1788. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1789. "%s: Failed to send update operation to consumer (uniqueid %s, CSN %s): "
  1790. "Local error. Will retry later.\n",
  1791. agmt_get_long_name(prp->agmt),
  1792. entry.op->target_address.uniqueid, csn_str);
  1793. }
  1794. }
  1795. else
  1796. {
  1797. char *uniqueid = NULL;
  1798. ReplicaId replica_id = 0;
  1799. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1800. replica_id = csn_get_replicaid(entry.op->csn);
  1801. uniqueid = entry.op->target_address.uniqueid;
  1802. if (prp->repl50consumer && message_id)
  1803. {
  1804. int operation, error = 0;
  1805. conn_get_error(prp->conn, &operation, &error);
  1806. /* Get the response here */
  1807. replay_crc = repl5_inc_get_next_result(rd);
  1808. conn_get_error(prp->conn, &operation, &error);
  1809. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1810. return_value = repl5_inc_update_from_op_result(prp, replay_crc, error, csn_str, uniqueid, replica_id, &finished, num_changes_sent);
  1811. }
  1812. else if (message_id) {
  1813. /* Queue the details for pickup later in the response thread */
  1814. repl5_inc_operation *sop = NULL;
  1815. sop = repl5_inc_operation_new();
  1816. PL_strncpyz(sop->csn_str, csn_str, sizeof(sop->csn_str));
  1817. sop->ldap_message_id = message_id;
  1818. sop->operation_type = entry.op->operation_type;
  1819. sop->replica_id = replica_id;
  1820. PL_strncpyz(sop->uniqueid, uniqueid, sizeof(sop->uniqueid));
  1821. repl5_int_push_operation(rd,sop);
  1822. } else {
  1823. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1824. "%s: Skipping update operation with no message_id (uniqueid %s, CSN %s):\n",
  1825. agmt_get_long_name(prp->agmt),
  1826. entry.op->target_address.uniqueid, csn_str);
  1827. agmt_inc_last_update_changecount (prp->agmt, csn_get_replicaid(entry.op->csn), 1 /*skipped*/);
  1828. }
  1829. }
  1830. break;
  1831. case CL5_BAD_DATA:
  1832. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1833. "%s: Invalid parameter passed to cl5GetNextOperationToReplay\n",
  1834. agmt_get_long_name(prp->agmt));
  1835. return_value = UPDATE_FATAL_ERROR;
  1836. finished = 1;
  1837. break;
  1838. case CL5_NOTFOUND:
  1839. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1840. "%s: No more updates to send (cl5GetNextOperationToReplay)\n",
  1841. agmt_get_long_name(prp->agmt));
  1842. return_value = UPDATE_NO_MORE_UPDATES;
  1843. finished = 1;
  1844. break;
  1845. case CL5_DB_ERROR:
  1846. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1847. "%s: A database error occurred (cl5GetNextOperationToReplay)\n",
  1848. agmt_get_long_name(prp->agmt));
  1849. return_value = UPDATE_FATAL_ERROR;
  1850. finished = 1;
  1851. break;
  1852. case CL5_BAD_FORMAT:
  1853. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1854. "%s: A malformed changelog entry was encountered (cl5GetNextOperationToReplay)\n",
  1855. agmt_get_long_name(prp->agmt));
  1856. break;
  1857. case CL5_MEMORY_ERROR:
  1858. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1859. "%s: A memory allocation error occurred (cl5GetNextOperationToRepla)\n",
  1860. agmt_get_long_name(prp->agmt));
  1861. return_value = UPDATE_FATAL_ERROR;
  1862. break;
  1863. default:
  1864. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1865. "%s: Unknown error code (%d) returned from cl5GetNextOperationToReplay\n",
  1866. agmt_get_long_name(prp->agmt), rc);
  1867. return_value = UPDATE_TRANSIENT_ERROR;
  1868. break;
  1869. }
  1870. /* Check for protocol shutdown */
  1871. if (prp->terminate)
  1872. {
  1873. return_value = UPDATE_NO_MORE_UPDATES;
  1874. finished = 1;
  1875. }
  1876. if (*num_changes_sent >= MAX_CHANGES_PER_SESSION)
  1877. {
  1878. return_value = UPDATE_YIELD;
  1879. finished = 1;
  1880. }
  1881. PR_Lock(rd->lock);
  1882. /* See if the result thread has hit a problem */
  1883. if (!finished && rd->abort)
  1884. {
  1885. return_value = rd->result;
  1886. finished = 1;
  1887. }
  1888. PR_Unlock(rd->lock);
  1889. } while (!finished);
  1890. /* Terminate the results reading thread */
  1891. if (!prp->repl50consumer)
  1892. {
  1893. /* We need to ensure that we wait until all the responses have been recived from our operations */
  1894. if (return_value != UPDATE_CONNECTION_LOST) {
  1895. /* if connection was lost/closed, there will be nothing to read */
  1896. repl5_inc_waitfor_async_results(rd);
  1897. }
  1898. rc = repl5_inc_destroy_async_result_thread(rd);
  1899. if (rc) {
  1900. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name, "%s: repl5_inc_run: "
  1901. "repl5_tot_destroy_async_result_thread failed; error - %d\n",
  1902. agmt_get_long_name(prp->agmt), rc);
  1903. }
  1904. *num_changes_sent = rd->num_changes_sent;
  1905. }
  1906. repl5_inc_rd_destroy(&rd);
  1907. cl5_operation_parameters_done ( entry.op );
  1908. cl5DestroyReplayIterator(&changelog_iterator);
  1909. }
  1910. return return_value;
  1911. }
  1912. /*
  1913. * XXXggood this should probably be in the superclass, since the full update
  1914. * protocol is going to need it too.
  1915. */
  1916. static int
  1917. repl5_inc_stop(Private_Repl_Protocol *prp)
  1918. {
  1919. int return_value;
  1920. PRIntervalTime start, maxwait, now;
  1921. int seconds = 1200;
  1922. maxwait = PR_SecondsToInterval(seconds);
  1923. prp->terminate = 1;
  1924. event_notify(prp, EVENT_PROTOCOL_SHUTDOWN);
  1925. start = PR_IntervalNow();
  1926. now = start;
  1927. while (!prp->stopped && ((now - start) < maxwait))
  1928. {
  1929. DS_Sleep(PR_SecondsToInterval(1));
  1930. now = PR_IntervalNow();
  1931. }
  1932. if (!prp->stopped)
  1933. {
  1934. /* Isn't listening. Do something drastic. */
  1935. return_value = -1;
  1936. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1937. "%s: repl5_inc_stop: protocol does not stop after %d seconds\n",
  1938. agmt_get_long_name(prp->agmt), seconds);
  1939. }
  1940. else
  1941. {
  1942. return_value = 0;
  1943. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1944. "%s: repl5_inc_stop: protocol stopped after %d seconds\n",
  1945. agmt_get_long_name(prp->agmt),
  1946. PR_IntervalToSeconds(now-start));
  1947. }
  1948. return return_value;
  1949. }
  1950. static int
  1951. repl5_inc_status(Private_Repl_Protocol *prp)
  1952. {
  1953. int return_value = 0;
  1954. return return_value;
  1955. }
  1956. static void
  1957. repl5_inc_notify_update(Private_Repl_Protocol *prp)
  1958. {
  1959. event_notify(prp, EVENT_TRIGGERING_CRITERIA_MET);
  1960. }
  1961. static void
  1962. repl5_inc_update_now(Private_Repl_Protocol *prp)
  1963. {
  1964. event_notify(prp, EVENT_REPLICATE_NOW);
  1965. }
  1966. static void
  1967. repl5_inc_notify_agmt_changed(Private_Repl_Protocol *prp)
  1968. {
  1969. event_notify(prp, EVENT_AGMT_CHANGED);
  1970. }
  1971. static void
  1972. repl5_inc_notify_window_opened (Private_Repl_Protocol *prp)
  1973. {
  1974. event_notify(prp, EVENT_WINDOW_OPENED);
  1975. }
  1976. static void
  1977. repl5_inc_notify_window_closed (Private_Repl_Protocol *prp)
  1978. {
  1979. event_notify(prp, EVENT_WINDOW_CLOSED);
  1980. }
  1981. Private_Repl_Protocol *
  1982. Repl_5_Inc_Protocol_new(Repl_Protocol *rp)
  1983. {
  1984. repl5_inc_private *rip = NULL;
  1985. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)slapi_ch_malloc(sizeof(Private_Repl_Protocol));
  1986. prp->delete = repl5_inc_delete;
  1987. prp->run = repl5_inc_run;
  1988. prp->stop = repl5_inc_stop;
  1989. prp->status = repl5_inc_status;
  1990. prp->notify_update = repl5_inc_notify_update;
  1991. prp->notify_agmt_changed = repl5_inc_notify_agmt_changed;
  1992. prp->notify_window_opened = repl5_inc_notify_window_opened;
  1993. prp->notify_window_closed = repl5_inc_notify_window_closed;
  1994. prp->update_now = repl5_inc_update_now;
  1995. prp->replica_object = prot_get_replica_object(rp);
  1996. if ((prp->lock = PR_NewLock()) == NULL)
  1997. {
  1998. goto loser;
  1999. }
  2000. if ((prp->cvar = PR_NewCondVar(prp->lock)) == NULL)
  2001. {
  2002. goto loser;
  2003. }
  2004. prp->stopped = 0;
  2005. prp->terminate = 0;
  2006. prp->eventbits = 0;
  2007. prp->conn = prot_get_connection(rp);
  2008. prp->agmt = prot_get_agreement(rp);
  2009. prp->last_acquire_response_code = NSDS50_REPL_REPLICA_READY;
  2010. rip = (void *)slapi_ch_malloc(sizeof(repl5_inc_private));
  2011. rip->ruv = NULL;
  2012. rip->backoff = NULL;
  2013. rip->rp = rp;
  2014. prp->private = (void *)rip;
  2015. prp->replica_acquired = PR_FALSE;
  2016. prp->repl50consumer = 0;
  2017. prp->repl71consumer = 0;
  2018. prp->repl90consumer = 0;
  2019. return prp;
  2020. loser:
  2021. repl5_inc_delete(&prp);
  2022. return NULL;
  2023. }
  2024. static void
  2025. repl5_inc_backoff_expired(time_t timer_fire_time, void *arg)
  2026. {
  2027. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)arg;
  2028. PR_ASSERT(NULL != prp);
  2029. event_notify(prp, EVENT_BACKOFF_EXPIRED);
  2030. }
  2031. /*
  2032. * Examine the update vector and determine our course of action.
  2033. * There are 3 different possibilities, plus a catch-all error:
  2034. * 1 - no update vector (ruv is NULL). The consumer's replica is
  2035. * pristine, so it needs to be initialized. Return
  2036. * EXAMINE_RUV_PRISTINE_REPLICA.
  2037. * 2 - ruv is present, but its database generation ID doesn't
  2038. * match the local generation ID. This means that either
  2039. * the local replica must be reinitialized from the remote
  2040. * replica or vice-versa. Return
  2041. * EXAMINE_RUV_GENERATION_MISMATCH.
  2042. * 3 - ruv is present, and we have all updates needed to bring
  2043. * the replica up to date using the incremental protocol.
  2044. * return EXAMINE_RUV_OK.
  2045. * 4 - parameter error. Return EXAMINE_RUV_PARAM_ERROR
  2046. */
  2047. static int
  2048. examine_update_vector(Private_Repl_Protocol *prp, RUV *remote_ruv)
  2049. {
  2050. int return_value;
  2051. PR_ASSERT(NULL != prp);
  2052. if (NULL == prp)
  2053. {
  2054. return_value = EXAMINE_RUV_PARAM_ERROR;
  2055. }
  2056. else if (NULL == remote_ruv)
  2057. {
  2058. return_value = EXAMINE_RUV_PRISTINE_REPLICA;
  2059. }
  2060. else
  2061. {
  2062. char *local_gen = NULL;
  2063. char *remote_gen = ruv_get_replica_generation(remote_ruv);
  2064. Object *local_ruv_obj;
  2065. RUV *local_ruv;
  2066. Replica *replica;
  2067. PR_ASSERT(NULL != prp->replica_object);
  2068. replica = object_get_data(prp->replica_object);
  2069. PR_ASSERT(NULL != replica);
  2070. local_ruv_obj = replica_get_ruv (replica);
  2071. if (NULL != local_ruv_obj)
  2072. {
  2073. local_ruv = (RUV*)object_get_data (local_ruv_obj);
  2074. PR_ASSERT (local_ruv);
  2075. local_gen = ruv_get_replica_generation(local_ruv);
  2076. object_release (local_ruv_obj);
  2077. }
  2078. if (NULL == remote_gen || NULL == local_gen || strcmp(remote_gen, local_gen) != 0)
  2079. {
  2080. return_value = EXAMINE_RUV_GENERATION_MISMATCH;
  2081. }
  2082. else
  2083. {
  2084. return_value = EXAMINE_RUV_OK;
  2085. }
  2086. slapi_ch_free((void**)&remote_gen);
  2087. slapi_ch_free((void**)&local_gen);
  2088. }
  2089. return return_value;
  2090. }
  2091. /*
  2092. * When we get an error from an LDAP operation, we call this
  2093. * function to decide if we should just keep replaying
  2094. * updates, or if we should stop, back off, and try again
  2095. * later.
  2096. * Returns PR_TRUE if we shoould keep going, PR_FALSE if
  2097. * we should back off and try again later.
  2098. *
  2099. * In general, we keep going if the return code is consistent
  2100. * with some sort of bug in URP that causes the consumer to
  2101. * emit an error code that it shouldn't have, e.g. LDAP_ALREADY_EXISTS.
  2102. *
  2103. * We stop if there's some indication that the server just completely
  2104. * failed to process the operation, e.g. LDAP_OPERATIONS_ERROR.
  2105. */
  2106. static PRBool
  2107. ignore_error_and_keep_going(int error)
  2108. {
  2109. int return_value = PR_FALSE;
  2110. switch (error)
  2111. {
  2112. /* Cases where we keep going */
  2113. case LDAP_SUCCESS:
  2114. case LDAP_NO_SUCH_ATTRIBUTE:
  2115. case LDAP_UNDEFINED_TYPE:
  2116. case LDAP_CONSTRAINT_VIOLATION:
  2117. case LDAP_TYPE_OR_VALUE_EXISTS:
  2118. case LDAP_INVALID_SYNTAX:
  2119. case LDAP_NO_SUCH_OBJECT:
  2120. case LDAP_INVALID_DN_SYNTAX:
  2121. case LDAP_IS_LEAF:
  2122. case LDAP_INSUFFICIENT_ACCESS:
  2123. case LDAP_NAMING_VIOLATION:
  2124. case LDAP_OBJECT_CLASS_VIOLATION:
  2125. case LDAP_NOT_ALLOWED_ON_NONLEAF:
  2126. case LDAP_NOT_ALLOWED_ON_RDN:
  2127. case LDAP_ALREADY_EXISTS:
  2128. case LDAP_NO_OBJECT_CLASS_MODS:
  2129. return_value = PR_TRUE;
  2130. break;
  2131. /* Cases where we stop and retry */
  2132. case LDAP_OPERATIONS_ERROR:
  2133. case LDAP_PROTOCOL_ERROR:
  2134. case LDAP_TIMELIMIT_EXCEEDED:
  2135. case LDAP_SIZELIMIT_EXCEEDED:
  2136. case LDAP_STRONG_AUTH_NOT_SUPPORTED:
  2137. case LDAP_STRONG_AUTH_REQUIRED:
  2138. case LDAP_PARTIAL_RESULTS:
  2139. case LDAP_REFERRAL:
  2140. case LDAP_ADMINLIMIT_EXCEEDED:
  2141. case LDAP_UNAVAILABLE_CRITICAL_EXTENSION:
  2142. case LDAP_CONFIDENTIALITY_REQUIRED:
  2143. case LDAP_SASL_BIND_IN_PROGRESS:
  2144. case LDAP_INAPPROPRIATE_MATCHING:
  2145. case LDAP_ALIAS_PROBLEM:
  2146. case LDAP_ALIAS_DEREF_PROBLEM:
  2147. case LDAP_INAPPROPRIATE_AUTH:
  2148. case LDAP_INVALID_CREDENTIALS:
  2149. case LDAP_BUSY:
  2150. case LDAP_UNAVAILABLE:
  2151. case LDAP_UNWILLING_TO_PERFORM:
  2152. case LDAP_LOOP_DETECT:
  2153. case LDAP_SORT_CONTROL_MISSING:
  2154. case LDAP_INDEX_RANGE_ERROR:
  2155. case LDAP_RESULTS_TOO_LARGE:
  2156. case LDAP_AFFECTS_MULTIPLE_DSAS:
  2157. case LDAP_OTHER:
  2158. case LDAP_SERVER_DOWN:
  2159. case LDAP_LOCAL_ERROR:
  2160. case LDAP_ENCODING_ERROR:
  2161. case LDAP_DECODING_ERROR:
  2162. case LDAP_TIMEOUT:
  2163. case LDAP_AUTH_UNKNOWN:
  2164. case LDAP_FILTER_ERROR:
  2165. case LDAP_USER_CANCELLED:
  2166. case LDAP_PARAM_ERROR:
  2167. case LDAP_NO_MEMORY:
  2168. case LDAP_CONNECT_ERROR:
  2169. case LDAP_NOT_SUPPORTED:
  2170. case LDAP_CONTROL_NOT_FOUND:
  2171. case LDAP_NO_RESULTS_RETURNED:
  2172. case LDAP_MORE_RESULTS_TO_RETURN:
  2173. case LDAP_CLIENT_LOOP:
  2174. case LDAP_REFERRAL_LIMIT_EXCEEDED:
  2175. return_value = PR_FALSE;
  2176. break;
  2177. }
  2178. return return_value;
  2179. }
  2180. /* this function converts a state to its name - for debug output */
  2181. static const char*
  2182. state2name (int state)
  2183. {
  2184. switch (state)
  2185. {
  2186. case STATE_START: return "start";
  2187. case STATE_WAIT_WINDOW_OPEN: return "wait_for_window_to_open";
  2188. case STATE_WAIT_CHANGES: return "wait_for_changes";
  2189. case STATE_READY_TO_ACQUIRE: return "ready_to_acquire_replica";
  2190. case STATE_BACKOFF_START: return "start_backoff";
  2191. case STATE_BACKOFF: return "backoff";
  2192. case STATE_SENDING_UPDATES: return "sending_updates";
  2193. case STATE_STOP_FATAL_ERROR: return "stop_fatal_error";
  2194. case STATE_STOP_FATAL_ERROR_PART2: return "stop_fatal_error";
  2195. case STATE_STOP_NORMAL_TERMINATION: return "stop_normal_termination";
  2196. default: return "invalid_state";
  2197. }
  2198. }
  2199. /* this function convert s an event to its name - for debug output */
  2200. static const char*
  2201. event2name (int event)
  2202. {
  2203. switch (event)
  2204. {
  2205. case EVENT_WINDOW_OPENED: return "update_window_opened";
  2206. case EVENT_WINDOW_CLOSED: return "update_window_closed";
  2207. case EVENT_TRIGGERING_CRITERIA_MET: return "data_modified";
  2208. case EVENT_BACKOFF_EXPIRED: return "backoff_timer_expired";
  2209. case EVENT_REPLICATE_NOW: return "replicate_now";
  2210. case EVENT_PROTOCOL_SHUTDOWN: return "protocol_shutdown";
  2211. case EVENT_AGMT_CHANGED: return "agreement_changed";
  2212. default: return "invalid_event";
  2213. }
  2214. }
  2215. static const char*
  2216. op2string(int op)
  2217. {
  2218. switch (op) {
  2219. case SLAPI_OPERATION_ADD:
  2220. return "add";
  2221. case SLAPI_OPERATION_MODIFY:
  2222. return "modify";
  2223. case SLAPI_OPERATION_DELETE:
  2224. return "delete";
  2225. case SLAPI_OPERATION_MODRDN:
  2226. return "rename";
  2227. case SLAPI_OPERATION_EXTENDED:
  2228. return "extended";
  2229. }
  2230. return "unknown";
  2231. }