repl5_inc_protocol.c 73 KB

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