1
0

repl5_inc_protocol.c 86 KB

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