1
0

repl5_inc_protocol.c 85 KB

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