repl5_inc_protocol.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392
  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. replica = (Replica*) object_get_data(prp->replica_object);
  1811. if (replica)
  1812. {
  1813. rid = replica_get_rid(replica);
  1814. }
  1815. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1816. "%s: skipped updates was definitely too high (%d) update the subentry now\n",
  1817. agmt_get_long_name(prp->agmt), skipped_updates);
  1818. replica_subentry_update(agmt_get_replarea(prp->agmt), rid);
  1819. }
  1820. /* Terminate the results reading thread */
  1821. if (!prp->repl50consumer)
  1822. {
  1823. /* We need to ensure that we wait until all the responses have been received from our operations */
  1824. if (return_value != UPDATE_CONNECTION_LOST) {
  1825. rd->WaitForAsyncResults = agmt_get_WaitForAsyncResults(prp->agmt);
  1826. /* if connection was lost/closed, there will be nothing to read */
  1827. repl5_inc_waitfor_async_results(rd);
  1828. }
  1829. rc = repl5_inc_destroy_async_result_thread(rd);
  1830. if (rc) {
  1831. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name, "%s: repl5_inc_run: "
  1832. "repl5_tot_destroy_async_result_thread failed; error - %d\n",
  1833. agmt_get_long_name(prp->agmt), rc);
  1834. }
  1835. *num_changes_sent = rd->num_changes_sent;
  1836. }
  1837. PR_Lock(rd->lock);
  1838. if (rd->flowcontrol_detection) {
  1839. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1840. "%s: Incremental update flow control triggered %d times\n"
  1841. "You may increase %s and/or decrease %s in the replica agreement configuration\n",
  1842. agmt_get_long_name(prp->agmt),
  1843. rd->flowcontrol_detection,
  1844. type_nsds5ReplicaFlowControlPause,
  1845. type_nsds5ReplicaFlowControlWindow);
  1846. }
  1847. PR_Unlock(rd->lock);
  1848. repl5_inc_rd_destroy(&rd);
  1849. cl5_operation_parameters_done ( entry.op );
  1850. cl5DestroyReplayIterator(&changelog_iterator);
  1851. }
  1852. return return_value;
  1853. }
  1854. /*
  1855. * XXXggood this should probably be in the superclass, since the full update
  1856. * protocol is going to need it too.
  1857. */
  1858. static int
  1859. repl5_inc_stop(Private_Repl_Protocol *prp)
  1860. {
  1861. PRIntervalTime start, maxwait, now;
  1862. Replica *replica = NULL;
  1863. PRUint64 timeout;
  1864. int return_value;
  1865. if((timeout = agmt_get_protocol_timeout(prp->agmt)) == 0){
  1866. timeout = DEFAULT_PROTOCOL_TIMEOUT;
  1867. if(prp->replica_object){
  1868. object_acquire(prp->replica_object);
  1869. replica = object_get_data(prp->replica_object);
  1870. if((timeout = replica_get_protocol_timeout(replica)) == 0){
  1871. timeout = DEFAULT_PROTOCOL_TIMEOUT;
  1872. }
  1873. object_release(prp->replica_object);
  1874. }
  1875. }
  1876. maxwait = PR_SecondsToInterval(timeout);
  1877. prp->terminate = 1;
  1878. event_notify(prp, EVENT_PROTOCOL_SHUTDOWN);
  1879. start = PR_IntervalNow();
  1880. now = start;
  1881. while (!prp->stopped && ((now - start) < maxwait))
  1882. {
  1883. DS_Sleep(PR_MillisecondsToInterval(100));
  1884. now = PR_IntervalNow();
  1885. }
  1886. if (!prp->stopped)
  1887. {
  1888. /* Isn't listening. Do something drastic. */
  1889. return_value = -1;
  1890. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1891. "%s: repl5_inc_stop: protocol does not stop after %" NSPRIu64 " seconds\n",
  1892. agmt_get_long_name(prp->agmt), timeout);
  1893. }
  1894. else
  1895. {
  1896. return_value = 0;
  1897. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1898. "%s: repl5_inc_stop: protocol stopped after %d seconds\n",
  1899. agmt_get_long_name(prp->agmt),
  1900. PR_IntervalToSeconds(now-start));
  1901. }
  1902. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1903. if (NULL == prp->replica_object) {
  1904. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1905. "%s: repl5_inc_stop: protocol replica_object is NULL\n",
  1906. agmt_get_long_name(prp->agmt));
  1907. } else {
  1908. Replica *replica;
  1909. object_acquire(prp->replica_object);
  1910. replica = object_get_data(prp->replica_object);
  1911. if (NULL == replica) {
  1912. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1913. "%s: repl5_inc_stop: replica is NULL\n",
  1914. agmt_get_long_name(prp->agmt));
  1915. } else {
  1916. Object *ruv_obj = replica_get_ruv(replica);
  1917. if (NULL == ruv_obj) {
  1918. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1919. "%s: repl5_inc_stop: ruv_obj is NULL\n",
  1920. agmt_get_long_name(prp->agmt));
  1921. } else {
  1922. RUV *ruv;
  1923. object_acquire(ruv_obj);
  1924. ruv = (RUV*)object_get_data (ruv_obj);
  1925. if (NULL == ruv) {
  1926. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1927. "%s: repl5_inc_stop: ruv is NULL\n",
  1928. agmt_get_long_name(prp->agmt));
  1929. } else {
  1930. ruv_dump(ruv, "Database RUV", NULL);
  1931. }
  1932. object_release(ruv_obj);
  1933. }
  1934. }
  1935. object_release(prp->replica_object);
  1936. }
  1937. }
  1938. return return_value;
  1939. }
  1940. static int
  1941. repl5_inc_status(Private_Repl_Protocol *prp)
  1942. {
  1943. int return_value = 0;
  1944. return return_value;
  1945. }
  1946. static void
  1947. repl5_inc_notify_update(Private_Repl_Protocol *prp)
  1948. {
  1949. event_notify(prp, EVENT_TRIGGERING_CRITERIA_MET);
  1950. }
  1951. static void
  1952. repl5_inc_update_now(Private_Repl_Protocol *prp)
  1953. {
  1954. event_notify(prp, EVENT_REPLICATE_NOW);
  1955. }
  1956. static void
  1957. repl5_inc_notify_agmt_changed(Private_Repl_Protocol *prp)
  1958. {
  1959. event_notify(prp, EVENT_AGMT_CHANGED);
  1960. }
  1961. static void
  1962. repl5_inc_notify_window_opened (Private_Repl_Protocol *prp)
  1963. {
  1964. event_notify(prp, EVENT_WINDOW_OPENED);
  1965. }
  1966. static void
  1967. repl5_inc_notify_window_closed (Private_Repl_Protocol *prp)
  1968. {
  1969. event_notify(prp, EVENT_WINDOW_CLOSED);
  1970. }
  1971. Private_Repl_Protocol *
  1972. Repl_5_Inc_Protocol_new(Repl_Protocol *rp)
  1973. {
  1974. repl5_inc_private *rip = NULL;
  1975. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)slapi_ch_malloc(sizeof(Private_Repl_Protocol));
  1976. prp->delete = repl5_inc_delete;
  1977. prp->run = repl5_inc_run;
  1978. prp->stop = repl5_inc_stop;
  1979. prp->status = repl5_inc_status;
  1980. prp->notify_update = repl5_inc_notify_update;
  1981. prp->notify_agmt_changed = repl5_inc_notify_agmt_changed;
  1982. prp->notify_window_opened = repl5_inc_notify_window_opened;
  1983. prp->notify_window_closed = repl5_inc_notify_window_closed;
  1984. prp->update_now = repl5_inc_update_now;
  1985. prp->replica_object = prot_get_replica_object(rp);
  1986. if ((prp->lock = PR_NewLock()) == NULL)
  1987. {
  1988. goto loser;
  1989. }
  1990. if ((prp->cvar = PR_NewCondVar(prp->lock)) == NULL)
  1991. {
  1992. goto loser;
  1993. }
  1994. prp->stopped = 0;
  1995. prp->terminate = 0;
  1996. prp->eventbits = 0;
  1997. prp->conn = prot_get_connection(rp);
  1998. prp->agmt = prot_get_agreement(rp);
  1999. prp->last_acquire_response_code = NSDS50_REPL_REPLICA_READY;
  2000. rip = (void *)slapi_ch_malloc(sizeof(repl5_inc_private));
  2001. rip->ruv = NULL;
  2002. rip->backoff = NULL;
  2003. rip->rp = rp;
  2004. prp->private = (void *)rip;
  2005. prp->replica_acquired = PR_FALSE;
  2006. prp->repl50consumer = 0;
  2007. prp->repl71consumer = 0;
  2008. prp->repl90consumer = 0;
  2009. return prp;
  2010. loser:
  2011. repl5_inc_delete(&prp);
  2012. return NULL;
  2013. }
  2014. static void
  2015. repl5_inc_backoff_expired(time_t timer_fire_time, void *arg)
  2016. {
  2017. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)arg;
  2018. PR_ASSERT(NULL != prp);
  2019. event_notify(prp, EVENT_BACKOFF_EXPIRED);
  2020. }
  2021. /*
  2022. * Examine the update vector and determine our course of action.
  2023. * There are 3 different possibilities, plus a catch-all error:
  2024. * 1 - no update vector (ruv is NULL). The consumer's replica is
  2025. * pristine, so it needs to be initialized. Return
  2026. * EXAMINE_RUV_PRISTINE_REPLICA.
  2027. * 2 - ruv is present, but its database generation ID doesn't
  2028. * match the local generation ID. This means that either
  2029. * the local replica must be reinitialized from the remote
  2030. * replica or vice-versa. Return
  2031. * EXAMINE_RUV_GENERATION_MISMATCH.
  2032. * 3 - ruv is present, and we have all updates needed to bring
  2033. * the replica up to date using the incremental protocol.
  2034. * return EXAMINE_RUV_OK.
  2035. * 4 - parameter error. Return EXAMINE_RUV_PARAM_ERROR
  2036. */
  2037. static int
  2038. examine_update_vector(Private_Repl_Protocol *prp, RUV *remote_ruv)
  2039. {
  2040. int return_value;
  2041. PR_ASSERT(NULL != prp);
  2042. if (NULL == prp)
  2043. {
  2044. return_value = EXAMINE_RUV_PARAM_ERROR;
  2045. }
  2046. else if (NULL == remote_ruv)
  2047. {
  2048. return_value = EXAMINE_RUV_PRISTINE_REPLICA;
  2049. }
  2050. else
  2051. {
  2052. char *local_gen = NULL;
  2053. char *remote_gen = ruv_get_replica_generation(remote_ruv);
  2054. Object *local_ruv_obj;
  2055. RUV *local_ruv;
  2056. Replica *replica;
  2057. PR_ASSERT(NULL != prp->replica_object);
  2058. replica = object_get_data(prp->replica_object);
  2059. PR_ASSERT(NULL != replica);
  2060. local_ruv_obj = replica_get_ruv (replica);
  2061. if (NULL != local_ruv_obj)
  2062. {
  2063. local_ruv = (RUV*)object_get_data (local_ruv_obj);
  2064. PR_ASSERT (local_ruv);
  2065. local_gen = ruv_get_replica_generation(local_ruv);
  2066. object_release (local_ruv_obj);
  2067. }
  2068. if (NULL == remote_gen || NULL == local_gen || strcmp(remote_gen, local_gen) != 0)
  2069. {
  2070. return_value = EXAMINE_RUV_GENERATION_MISMATCH;
  2071. }
  2072. else
  2073. {
  2074. return_value = EXAMINE_RUV_OK;
  2075. }
  2076. slapi_ch_free((void**)&remote_gen);
  2077. slapi_ch_free((void**)&local_gen);
  2078. }
  2079. return return_value;
  2080. }
  2081. /*
  2082. * When we get an error from an LDAP operation, we call this
  2083. * function to decide if we should just keep replaying
  2084. * updates, or if we should stop, back off, and try again
  2085. * later.
  2086. * Returns PR_TRUE if we shoould keep going, PR_FALSE if
  2087. * we should back off and try again later.
  2088. *
  2089. * In general, we keep going if the return code is consistent
  2090. * with some sort of bug in URP that causes the consumer to
  2091. * emit an error code that it shouldn't have, e.g. LDAP_ALREADY_EXISTS.
  2092. *
  2093. * We stop if there's some indication that the server just completely
  2094. * failed to process the operation, e.g. LDAP_OPERATIONS_ERROR.
  2095. */
  2096. static PRBool
  2097. ignore_error_and_keep_going(int error)
  2098. {
  2099. int return_value = PR_FALSE;
  2100. switch (error)
  2101. {
  2102. /* Cases where we keep going */
  2103. case LDAP_SUCCESS:
  2104. case LDAP_NO_SUCH_ATTRIBUTE:
  2105. case LDAP_UNDEFINED_TYPE:
  2106. case LDAP_CONSTRAINT_VIOLATION:
  2107. case LDAP_TYPE_OR_VALUE_EXISTS:
  2108. case LDAP_INVALID_SYNTAX:
  2109. case LDAP_NO_SUCH_OBJECT:
  2110. case LDAP_INVALID_DN_SYNTAX:
  2111. case LDAP_IS_LEAF:
  2112. case LDAP_INSUFFICIENT_ACCESS:
  2113. case LDAP_NAMING_VIOLATION:
  2114. case LDAP_OBJECT_CLASS_VIOLATION:
  2115. case LDAP_NOT_ALLOWED_ON_NONLEAF:
  2116. case LDAP_NOT_ALLOWED_ON_RDN:
  2117. case LDAP_ALREADY_EXISTS:
  2118. case LDAP_NO_OBJECT_CLASS_MODS:
  2119. return_value = PR_TRUE;
  2120. break;
  2121. /* Cases where we stop and retry */
  2122. case LDAP_OPERATIONS_ERROR:
  2123. case LDAP_PROTOCOL_ERROR:
  2124. case LDAP_TIMELIMIT_EXCEEDED:
  2125. case LDAP_SIZELIMIT_EXCEEDED:
  2126. case LDAP_STRONG_AUTH_NOT_SUPPORTED:
  2127. case LDAP_STRONG_AUTH_REQUIRED:
  2128. case LDAP_PARTIAL_RESULTS:
  2129. case LDAP_REFERRAL:
  2130. case LDAP_ADMINLIMIT_EXCEEDED:
  2131. case LDAP_UNAVAILABLE_CRITICAL_EXTENSION:
  2132. case LDAP_CONFIDENTIALITY_REQUIRED:
  2133. case LDAP_SASL_BIND_IN_PROGRESS:
  2134. case LDAP_INAPPROPRIATE_MATCHING:
  2135. case LDAP_ALIAS_PROBLEM:
  2136. case LDAP_ALIAS_DEREF_PROBLEM:
  2137. case LDAP_INAPPROPRIATE_AUTH:
  2138. case LDAP_INVALID_CREDENTIALS:
  2139. case LDAP_BUSY:
  2140. case LDAP_UNAVAILABLE:
  2141. case LDAP_UNWILLING_TO_PERFORM:
  2142. case LDAP_LOOP_DETECT:
  2143. case LDAP_SORT_CONTROL_MISSING:
  2144. case LDAP_INDEX_RANGE_ERROR:
  2145. case LDAP_RESULTS_TOO_LARGE:
  2146. case LDAP_AFFECTS_MULTIPLE_DSAS:
  2147. case LDAP_OTHER:
  2148. case LDAP_SERVER_DOWN:
  2149. case LDAP_LOCAL_ERROR:
  2150. case LDAP_ENCODING_ERROR:
  2151. case LDAP_DECODING_ERROR:
  2152. case LDAP_TIMEOUT:
  2153. case LDAP_AUTH_UNKNOWN:
  2154. case LDAP_FILTER_ERROR:
  2155. case LDAP_USER_CANCELLED:
  2156. case LDAP_PARAM_ERROR:
  2157. case LDAP_NO_MEMORY:
  2158. case LDAP_CONNECT_ERROR:
  2159. case LDAP_NOT_SUPPORTED:
  2160. case LDAP_CONTROL_NOT_FOUND:
  2161. case LDAP_NO_RESULTS_RETURNED:
  2162. case LDAP_MORE_RESULTS_TO_RETURN:
  2163. case LDAP_CLIENT_LOOP:
  2164. case LDAP_REFERRAL_LIMIT_EXCEEDED:
  2165. return_value = PR_FALSE;
  2166. break;
  2167. }
  2168. return return_value;
  2169. }
  2170. /* this function converts a state to its name - for debug output */
  2171. static const char*
  2172. state2name (int state)
  2173. {
  2174. switch (state)
  2175. {
  2176. case STATE_START: return "start";
  2177. case STATE_WAIT_WINDOW_OPEN: return "wait_for_window_to_open";
  2178. case STATE_WAIT_CHANGES: return "wait_for_changes";
  2179. case STATE_READY_TO_ACQUIRE: return "ready_to_acquire_replica";
  2180. case STATE_BACKOFF_START: return "start_backoff";
  2181. case STATE_BACKOFF: return "backoff";
  2182. case STATE_SENDING_UPDATES: return "sending_updates";
  2183. case STATE_STOP_FATAL_ERROR: return "stop_fatal_error";
  2184. case STATE_STOP_FATAL_ERROR_PART2: return "stop_fatal_error";
  2185. case STATE_STOP_NORMAL_TERMINATION: return "stop_normal_termination";
  2186. default: return "invalid_state";
  2187. }
  2188. }
  2189. /* this function convert s an event to its name - for debug output */
  2190. static const char*
  2191. event2name (int event)
  2192. {
  2193. switch (event)
  2194. {
  2195. case EVENT_WINDOW_OPENED: return "update_window_opened";
  2196. case EVENT_WINDOW_CLOSED: return "update_window_closed";
  2197. case EVENT_TRIGGERING_CRITERIA_MET: return "data_modified";
  2198. case EVENT_BACKOFF_EXPIRED: return "backoff_timer_expired";
  2199. case EVENT_REPLICATE_NOW: return "replicate_now";
  2200. case EVENT_PROTOCOL_SHUTDOWN: return "protocol_shutdown";
  2201. case EVENT_AGMT_CHANGED: return "agreement_changed";
  2202. default: return "invalid_event";
  2203. }
  2204. }
  2205. static const char*
  2206. op2string(int op)
  2207. {
  2208. switch (op) {
  2209. case SLAPI_OPERATION_ADD:
  2210. return "add";
  2211. case SLAPI_OPERATION_MODIFY:
  2212. return "modify";
  2213. case SLAPI_OPERATION_DELETE:
  2214. return "delete";
  2215. case SLAPI_OPERATION_MODRDN:
  2216. return "rename";
  2217. case SLAPI_OPERATION_EXTENDED:
  2218. return "extended";
  2219. }
  2220. return "unknown";
  2221. }
  2222. void
  2223. repl5_set_backoff_min(Private_Repl_Protocol *prp, int min)
  2224. {
  2225. Replica *replica;
  2226. replica = (Replica *)object_get_data(prp->replica_object);
  2227. if(replica){
  2228. replica_set_backoff_min(replica, min);
  2229. }
  2230. }
  2231. void
  2232. repl5_set_backoff_max(Private_Repl_Protocol *prp, int max)
  2233. {
  2234. Replica *replica;
  2235. replica = object_get_data(prp->replica_object);
  2236. if(replica){
  2237. replica_set_backoff_max(replica, max);
  2238. }
  2239. }
  2240. int
  2241. repl5_get_backoff_min(Private_Repl_Protocol *prp)
  2242. {
  2243. Replica *replica;
  2244. replica = object_get_data(prp->replica_object);
  2245. if(replica){
  2246. return (int)replica_get_backoff_min(replica);
  2247. }
  2248. return PROTOCOL_BACKOFF_MINIMUM;
  2249. }
  2250. int
  2251. repl5_get_backoff_max(Private_Repl_Protocol *prp)
  2252. {
  2253. Replica *replica;
  2254. replica = object_get_data(prp->replica_object);
  2255. if(replica){
  2256. return (int)replica_get_backoff_max(replica);
  2257. }
  2258. return PROTOCOL_BACKOFF_MAXIMUM;
  2259. }