repl5_inc_protocol.c 87 KB

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