repl5_inc_protocol.c 73 KB

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