repl5_inc_protocol.c 73 KB

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