repl5_inc_protocol.c 73 KB

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