repl5_inc_protocol.c 74 KB

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