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