repl5_inc_protocol.c 87 KB

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