repl5_inc_protocol.c 75 KB

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