repl5_inc_protocol.c 84 KB

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