repl5_inc_protocol.c 75 KB

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